programming-examples/ruby/Data_Structures/deque_as_linked_list.rb
2019-11-15 12:59:38 +01:00

42 lines
761 B
Ruby

# This file contains the Ruby code from book of
# "Data Structures and Algorithms
# with Object-Oriented Design Patterns in Ruby"
# by Bruno R. Preiss.
#
# Copyright (c) 2004 by Bruno R. Preiss, P.Eng. All rights reserved.
class DequeAsLinkedList < QueueAsLinkedList
alias_method :queueHead, :head
include DequeMethods
def initialize
super
end
alias_method :head, :queueHead
def enqueueHead(obj)
@list.prepend(obj)
@count += 1
end
alias_method :dequeueHead, :dequeue
def tail
raise ContainerEmpty if @count == 0
@list.last
end
alias_method :enqueueTail, :enqueue
def dequeueTail
raise ContainerEmpty if @count == 0
result = @list.last
@list.extract(result)
@count -= 1
result
end
end