Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. var Queue = function (){
  2. this.oldStack = new Stack ();
  3. this.newStack = new Stack ();
  4. }
  5.  
  6. Queue.prototype.add = function (value){
  7. this.newStack.push(value);
  8. }
  9.  
  10. Queue.prototype.size = function (){
  11. return this.oldStack.size + this.newStack.size;
  12. }
  13.  
  14. Queue.prototype.sync = function (){
  15.  
  16. if (this.oldStack.isEmpty()){
  17.  
  18. while(!this.newStack.isEmpty()){
  19.  
  20. this.oldStack.push(this.newStack.pop());
  21. }
  22. }
  23. }
  24.  
  25. Queue.prototype.peek = function (){
  26. this.sync();
  27. return this.oldStack.peek();
  28. }
  29.  
  30. Queue.prototype.remove = function (){
  31. this.sync();
  32. return this.oldStack.pop();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement