Skip to content

Commit

Permalink
Queue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Oct 5, 2023
1 parent 56ce7b0 commit 2973c62
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/models/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
const next = Symbol("nextInQueue");

export default class Queue<T> {

head = null;
head = 0;
tail = 0;

store: Map<any,any> = new Map();

peek() {
return this.head;
return this.store[this.tail];
}

public enqueue(item: T) {
if (this.head) {
this.head[next] = item;
}
this.head = item;
this.store[this.tail] = item;
this.tail++;
}

public dequeue() {
if (!this.head) {
const { head, tail } = this;
const size = tail - head;
if (size <= 0) {
return void 0;
}
const item = this.store[head];
this.store.delete(head);
this.head++;
if (this.head === this.tail) {
this.head = 0;
this.tail = 0;
}
const { head } = this;
this.head = this.head[next];
delete head[next];
return head;
return item;
}

}

0 comments on commit 2973c62

Please sign in to comment.