Class: Zadt::MinMaxStackQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/MinMaxStackQueue/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/MinMaxStackQueue/MinMaxStackQueue.rb', line 8

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

Class Method Details

.helpObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 13

def self.help
  puts "Here are the functions for MinMaxStackQueue:"
  puts "#show"
  puts "#enqueue(value)"
  puts "#dequeue"
  puts "#min"
  puts "#max"
  puts "#length"
  puts "#empty?"
end

.methodsObject



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

def self.methods
  self.help
end

Instance Method Details

#dequeueObject



44
45
46
47
48
49
50
51
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 44

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

#empty?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 77

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

#enqueue(val) ⇒ Object



40
41
42
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 40

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

#helpObject



28
29
30
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 28

def help
  MinMaxStackQueue.help
end

#lengthObject



73
74
75
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 73

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

#maxObject



63
64
65
66
67
68
69
70
71
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 63

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

#methodsObject



32
33
34
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 32

def methods
  help
end

#minObject



53
54
55
56
57
58
59
60
61
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 53

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

#showObject



36
37
38
# File 'lib/zadt/AbstractDataTypes/MinMaxStackQueue/MinMaxStackQueue.rb', line 36

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