Class: DS::Stack
- Inherits:
-
Object
- Object
- DS::Stack
- Defined in:
- lib/ds/stacks/stack.rb
Overview
Class implements Stack data structure. Internaly uses array or linked list to store elements.
Class Method Summary collapse
-
.create(*args) ⇒ Object
Creates stack internally implemented as List.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
If stack is empty returns true otherwise returns false.
-
#initialize(*args) ⇒ 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 on the top of the stack.
-
#size ⇒ Object
Returns the stack size.
Constructor Details
Class Method Details
Instance Method Details
#empty? ⇒ Boolean
If stack is empty returns true otherwise returns false.
39 40 41 |
# File 'lib/ds/stacks/stack.rb', line 39 def empty? store.empty? end |
#peek ⇒ Object
Returns element from the top of the stack.
34 35 36 |
# File 'lib/ds/stacks/stack.rb', line 34 def peek store.last end |
#pop ⇒ Object
Removes element from stack and returns it.
24 25 26 |
# File 'lib/ds/stacks/stack.rb', line 24 def pop store.pop unless empty? end |
#push(x) ⇒ Object
Adds element on the top of the stack.
29 30 31 |
# File 'lib/ds/stacks/stack.rb', line 29 def push(x) store.push x end |
#size ⇒ Object
Returns the stack size.
19 20 21 |
# File 'lib/ds/stacks/stack.rb', line 19 def size store.size end |