Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. function Queue(){
  2. this._oldestIndex = 1;
  3. this._newestIndex = 1;
  4. this._storage = {};
  5. }
  6.  
  7. Queue.prototype.size = function(){
  8. return this._newestIndex - this._oldestIndex;
  9. };
  10.  
  11. Queue.prototype.enqueue = function(data){
  12. this._storage[this._newestIndex] = data;
  13. this._newestIndex++;
  14. };
  15.  
  16. Queue.prototype.dequeue = function(){
  17. var oldestIndex = this._oldestIndex,
  18. newestIndex = this._newestIndex,
  19. deletedData;
  20.  
  21. if(oldestIndex !== newestIndex){
  22. deletedData = this._storage[oldestIndex];
  23. delete this._storage[oldestIndex];
  24. this._oldestIndex++;
  25.  
  26. return deletedData;
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement