Skip to content

Commit cf010cb

Browse files
authored
Update Queue.md
1 parent 39e3b99 commit cf010cb

File tree

1 file changed

+19
-0
lines changed
  • Basic Data Structures/Implementation of Queue in Javascript

1 file changed

+19
-0
lines changed

Basic Data Structures/Implementation of Queue in Javascript/Queue.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,22 @@ export default class Queue {
9797
}
9898
}
9999
```
100+
> **If the direction is reversed, we can replace unshift and pop with push and shift, respectively.**
101+
102+
```
103+
class Queue {
104+
constructor(...items) {
105+
this.reverse = false;
106+
this.queue = [...items];
107+
}
108+
109+
enqueue(...items) {
110+
return this.reverse
111+
? this.queue.push(...items)
112+
: this.queue.unshift(...items);
113+
}
114+
115+
dequeue() {
116+
return this.reverse ? this.queue.shift() : this.queue.pop();
117+
}
118+
}

0 commit comments

Comments
 (0)