Class: Zadt::StackQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStackQueue

Returns a new instance of StackQueue.



7
8
9
10
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 7

def initialize
  @in = Stack.new
  @out = Stack.new
end

Class Method Details

.helpObject



12
13
14
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 12

def self.help
  Zadt::ADT::show_stackqueue_help_message
end

Instance Method Details

#dequeueObject



28
29
30
31
32
33
34
35
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 28

def dequeue
  if @out.empty?
    @in.length.times do
      @out.push(@in.pop)
    end
  end
  @out.pop
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 50

def empty?
  @in.empty? && @out.empty?
end

#enqueue(val) ⇒ Object



24
25
26
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 24

def enqueue(val)
  @in.push(val)
end

#helpObject



16
17
18
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 16

def help
  StackQueue.help
end

#lengthObject



46
47
48
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 46

def length
  @in.length + @out.length
end

#peekObject



37
38
39
40
41
42
43
44
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 37

def peek
  if @out.empty?
    @in.length.times do
      @out.push(@in.pop)
    end
  end
  @out.peek
end

#showObject



20
21
22
# File 'lib/zadt/AbstractDataTypes/StackQueue/StackQueue.rb', line 20

def show
  @out.show.reverse + @in.show
end