Class: RData::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/rdata/stack.rb

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



15
16
17
18
# File 'lib/rdata/stack.rb', line 15

def initialize
    @stack = Array[]
    @top = 0
end

Instance Method Details

#is_empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rdata/stack.rb', line 38

def is_empty?
    (@top == 0) ? 'true' : 'false'
end

#popObject



29
30
31
32
33
34
35
36
# File 'lib/rdata/stack.rb', line 29

def pop
    if self.is_empty? == "true"
        raise '[underflow] Cannot pop data from an empty stack'
    else
        @top = @top - 1
        return @stack[@top + 1]
    end 
end

#push(x) ⇒ Object



24
25
26
27
# File 'lib/rdata/stack.rb', line 24

def push(x)
    @top = @top + 1
    @stack[@top] = x
end

#topObject



20
21
22
# File 'lib/rdata/stack.rb', line 20

def top
    return @stack[@top]
end