mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
Changed queue from object type to array type and adjusted methods to reflect this change
This commit is contained in:
@ -9,41 +9,29 @@
|
|||||||
|
|
||||||
var Queue = function() {
|
var Queue = function() {
|
||||||
|
|
||||||
//This keeps track of where the end of the queue is
|
|
||||||
this.back = 0;
|
|
||||||
//This is the array representation of the queue
|
//This is the array representation of the queue
|
||||||
this.queue = {};
|
this.queue = [];
|
||||||
|
|
||||||
//Add a value to the end of the queue
|
//Add a value to the end of the queue
|
||||||
this.enqueue = function(item) {
|
this.enqueue = function(item) {
|
||||||
this.queue[this.back] = item;
|
this.queue[this.queue.length] = item;
|
||||||
this.back++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Removes the value at the front of the queue
|
//Removes the value at the front of the queue
|
||||||
this.dequeue = function() {
|
this.dequeue = function() {
|
||||||
if (this.back === 0) {
|
if (this.queue.length === 0) {
|
||||||
return "Queue is Empty";
|
return "Queue is Empty";
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = this.queue[this.front];
|
var result = this.queue[0];
|
||||||
delete this.queue[this.front];
|
this.queue.splice(0, 1); //remove the item at position 0 from the array
|
||||||
|
|
||||||
//Shift all the other items forward
|
|
||||||
for (var i = 1; i < this.back; i++) {
|
|
||||||
this.queue[i - 1] = this.queue[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
//clean up the leftover duplicated value at the back of the queue
|
|
||||||
delete this.queue[this.back];
|
|
||||||
this.back--;
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Return the length of the queue
|
//Return the length of the queue
|
||||||
this.length = function() {
|
this.length = function() {
|
||||||
return this.back;
|
return this.queue.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Return the item at the front of the queue
|
//Return the item at the front of the queue
|
||||||
@ -53,17 +41,7 @@ var Queue = function() {
|
|||||||
|
|
||||||
//List all the items in the queue
|
//List all the items in the queue
|
||||||
this.view = function() {
|
this.view = function() {
|
||||||
var str = "{"
|
console.log(this.queue);
|
||||||
//construct a single string to represent the items in the queue
|
|
||||||
for (var i = 0; i < this.back; i++) {
|
|
||||||
str += this.queue[i];
|
|
||||||
if (i !== this.back - 1) {
|
|
||||||
str += ", ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
str += "}";
|
|
||||||
|
|
||||||
console.log(str);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user