|
@@ -4,25 +4,18 @@ class Queue
|
|
|
constructor()
|
|
|
{
|
|
|
this.arr = [];
|
|
|
- this.pos = 0;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// uses the array x to fill the queue
|
|
|
makeFilled(x)
|
|
|
{
|
|
|
this.arr = x;
|
|
|
- this.offset = 0;
|
|
|
}
|
|
|
|
|
|
// Returns true if the queue is empty, and false otherwise.
|
|
|
isEmpty()
|
|
|
{
|
|
|
- return (this.arr.length == 0);
|
|
|
- }
|
|
|
-
|
|
|
- getPos()
|
|
|
- {
|
|
|
- return this.pos;
|
|
|
+ return (this.arr.length === 0);
|
|
|
}
|
|
|
|
|
|
// Enqueues x in the queue (to the end)
|
|
@@ -30,13 +23,13 @@ class Queue
|
|
|
{
|
|
|
this.arr.push(x);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// enqueues x to position i
|
|
|
insert(x, i)
|
|
|
{
|
|
|
this.arr.splice(i, 0, x);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// removes item at index i
|
|
|
remove(i)
|
|
|
{
|
|
@@ -47,20 +40,14 @@ class Queue
|
|
|
dequeue()
|
|
|
{
|
|
|
// if the queue is empty, throw
|
|
|
- if (this.arr.length == 0)
|
|
|
+ if (this.arr.length === 0)
|
|
|
{
|
|
|
throw "Queue empty!";
|
|
|
}
|
|
|
-
|
|
|
- // if the queue is at the end already, return undefined
|
|
|
- if (this.pos >= this.arr.length)
|
|
|
- {
|
|
|
- return undefined;
|
|
|
- }
|
|
|
|
|
|
// store the item at the front of the queue
|
|
|
- var item = this.arr[this.pos];
|
|
|
- ++this.pos;
|
|
|
+ const item = this.arr[0];
|
|
|
+ this.remove(0);
|
|
|
|
|
|
// return the dequeued item
|
|
|
return item;
|
|
@@ -70,20 +57,19 @@ class Queue
|
|
|
// queue is empty then undefined is returned.
|
|
|
peek()
|
|
|
{
|
|
|
- return (this.arr.length > 0 ? this.arr[this.pos] : undefined);
|
|
|
+ return (this.arr.length > 0 ? this.arr[0] : undefined);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// returns an array of all items in the queue (again, without dequeuing) from the current pos.
|
|
|
read()
|
|
|
{
|
|
|
- return this.arr.slice(this.pos);
|
|
|
+ return this.arr;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// Deletes all the data, resets to as on construction
|
|
|
reset()
|
|
|
{
|
|
|
this.arr = [];
|
|
|
- this.pos = 0;
|
|
|
}
|
|
|
}
|
|
|
|