Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. /**
  2. * Simple Que using linked lists.
  3. * Pushed value can be anything.
  4. */
  5. class Queue {
  6. constructor() {
  7. this.first = null;
  8. this.last = null;
  9. this.length = 0;
  10. }
  11. push(value) {
  12. const node = {
  13. value,
  14. next: null,
  15. };
  16.  
  17. const prevLast = this.last;
  18. if (this.last !== null) {
  19. prevLast.next = node;
  20. }
  21.  
  22. this.last = node;
  23.  
  24. if (this.first === null) {
  25. this.first = node;
  26. }
  27.  
  28. this.length += 1;
  29. }
  30. shift() {
  31. if (this.length === 0) return null;
  32.  
  33. const shiftedFirst = this.first;
  34.  
  35. this.first = shiftedFirst.next;
  36. this.length -= 1;
  37.  
  38. return shiftedFirst.value;
  39. }
  40. }
Add Comment
Please, Sign In to add comment