Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # Add an item to the back of the queue
  2. # @post: size = old.size+1
  3. # if !old.empty? then front == old.front
  4. # if old.empty? then front == item
  5. # @result: size
  6. def enter(item)
  7. if empty?
  8. @frontNode = @rearNode = Node.new(item,nil)
  9. else
  10. @rearNode = @rearNode.next = Node.new(item,nil)
  11. end
  12. @count += 1
  13. end
  14.  
  15. # Remove and return the item at the front of the queue
  16. # @pre: !empty?
  17. # @post: size == old.size-1
  18. def leave
  19. raise UnderflowError, "leave" if empty?
  20. @count -= 1
  21. frontItem = @frontNode.item;
  22. @frontNode = @frontNode.next;
  23. @rearNode = nil if empty?
  24. return frontItem
  25. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement