Class: DS::Stack

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

Overview

Class implements Stack data structure. Internaly uses array to store elements.

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



8
9
10
# File 'lib/ds/stacks/stack.rb', line 8

def initialize
  @store = []
end

Instance Method Details

#empty?Boolean

If stack is empty returns true otherwise returns false.

Returns:

  • (Boolean)


33
34
35
# File 'lib/ds/stacks/stack.rb', line 33

def empty?
  @store.empty?
end

#peekObject

Returns element from the top of the stack.



28
29
30
# File 'lib/ds/stacks/stack.rb', line 28

def peek
  @store.last
end

#popObject

Removes element from stack and returns it.



18
19
20
# File 'lib/ds/stacks/stack.rb', line 18

def pop
  @store.pop
end

#push(x) ⇒ Object

Adds element to the top of the stack.



23
24
25
# File 'lib/ds/stacks/stack.rb', line 23

def push(x)
  @store.push x
end

#sizeObject

Returns the stack size.



13
14
15
# File 'lib/ds/stacks/stack.rb', line 13

def size
  @store.size
end