Class: DS::Stack
- Inherits:
-
Object
- Object
- DS::Stack
- Defined in:
- lib/ds/stacks/stack.rb
Overview
Class implements Stack data structure. Internaly uses array to store elements.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
If stack is empty returns true otherwise returns false.
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
-
#peek ⇒ Object
Returns element from the top of the stack.
-
#pop ⇒ Object
Removes element from stack and returns it.
-
#push(x) ⇒ Object
Adds element to the top of the stack.
-
#size ⇒ Object
Returns the stack size.
Constructor Details
#initialize ⇒ Stack
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.
33 34 35 |
# File 'lib/ds/stacks/stack.rb', line 33 def empty? @store.empty? end |
#peek ⇒ Object
Returns element from the top of the stack.
28 29 30 |
# File 'lib/ds/stacks/stack.rb', line 28 def peek @store.last end |
#pop ⇒ Object
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 |
#size ⇒ Object
Returns the stack size.
13 14 15 |
# File 'lib/ds/stacks/stack.rb', line 13 def size @store.size end |