Class: Zadt::MinMaxStackQueue

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMinMaxStackQueue

Returns a new instance of MinMaxStackQueue.



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

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

Class Method Details

.helpObject



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

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

Instance Method Details

#dequeueObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb', line 71

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

#enqueue(val) ⇒ Object



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

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

#helpObject



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

def help
  MinMaxStackQueue.help
end

#lengthObject



67
68
69
# File 'lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb', line 67

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

#maxObject



57
58
59
60
61
62
63
64
65
# File 'lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb', line 57

def max
  if @in.empty?
    @out.max
  elsif @out.empty?
    @in.max
  else
    [@in.max, @out.max].max
  end
end

#minObject



47
48
49
50
51
52
53
54
55
# File 'lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb', line 47

def min
  if @in.empty?
    @out.min
  elsif @out.empty?
    @in.min
  else
    [@in.min, @out.min].min
  end
end

#peekObject



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

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

#showObject



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

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