Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple Que using linked lists.
- * Pushed value can be anything.
- */
- class Queue {
- constructor() {
- this.first = null;
- this.last = null;
- this.length = 0;
- }
- push(value) {
- const node = {
- value,
- next: null,
- };
- const prevLast = this.last;
- if (this.last !== null) {
- prevLast.next = node;
- }
- this.last = node;
- if (this.first === null) {
- this.first = node;
- }
- this.length += 1;
- }
- shift() {
- if (this.length === 0) return null;
- const shiftedFirst = this.first;
- this.first = shiftedFirst.next;
- this.length -= 1;
- return shiftedFirst.value;
- }
- }
Add Comment
Please, Sign In to add comment