mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-18 10:26:54 +01:00
Merge pull request #70 from vbrazo/add-stack-data-structure-example
Stack data structure: simple stack operations
This commit is contained in:
commit
e50f61049e
2 changed files with 89 additions and 0 deletions
|
@ -13,6 +13,8 @@
|
||||||
* Linked Lists
|
* Linked Lists
|
||||||
* [Double List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/double_list.rb)
|
* [Double List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/double_list.rb)
|
||||||
* [Single List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/single_list.rb)
|
* [Single List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/single_list.rb)
|
||||||
|
* Stacks
|
||||||
|
* [Stack](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/stacks/stack.rb)
|
||||||
* Tries
|
* Tries
|
||||||
* [Trie](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/tries/trie.rb)
|
* [Trie](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/tries/trie.rb)
|
||||||
|
|
||||||
|
|
87
data_structures/stacks/stack.rb
Normal file
87
data_structures/stacks/stack.rb
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
# A stack is an abstract data type that serves as a collection of
|
||||||
|
# elements with two principal operations: push() and pop(). push() adds an
|
||||||
|
# element to the top of the stack, and pop() removes an element from the top
|
||||||
|
# of a stack. The order in which elements come off of a stack are
|
||||||
|
# Last In, First Out (LIFO)
|
||||||
|
|
||||||
|
class StackOverflowError < StandardError; end
|
||||||
|
|
||||||
|
class Stack
|
||||||
|
def initialize(limit, stack = [])
|
||||||
|
@stack = stack
|
||||||
|
@limit = limit
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_accessor :stack, :limit
|
||||||
|
|
||||||
|
def push(item)
|
||||||
|
raise StackOverflowError unless stack.count < limit
|
||||||
|
|
||||||
|
stack << item
|
||||||
|
end
|
||||||
|
|
||||||
|
def pop
|
||||||
|
stack.pop
|
||||||
|
end
|
||||||
|
|
||||||
|
def peek
|
||||||
|
stack.last
|
||||||
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
stack.count.zero?
|
||||||
|
end
|
||||||
|
|
||||||
|
def full?
|
||||||
|
stack.count == limit
|
||||||
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
stack.count
|
||||||
|
end
|
||||||
|
|
||||||
|
def contains?(item)
|
||||||
|
stack.include?(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
stack = Stack.new(10, [])
|
||||||
|
|
||||||
|
puts stack.empty?
|
||||||
|
# => true
|
||||||
|
|
||||||
|
stack.push(3)
|
||||||
|
stack.push(5)
|
||||||
|
stack.push(7)
|
||||||
|
stack.push(9)
|
||||||
|
|
||||||
|
puts stack.full?
|
||||||
|
# => false
|
||||||
|
|
||||||
|
puts stack.contains?(5)
|
||||||
|
# => true
|
||||||
|
|
||||||
|
puts stack.pop
|
||||||
|
# => 9
|
||||||
|
|
||||||
|
puts stack.peek
|
||||||
|
# => 7
|
||||||
|
|
||||||
|
puts stack.size
|
||||||
|
# => 3
|
||||||
|
|
||||||
|
puts stack.inspect
|
||||||
|
# => #<Stack:0x00007fceed83eb40 @stack=[3, 5, 7], @limit=10>
|
||||||
|
|
||||||
|
stack.push(13)
|
||||||
|
stack.push(15)
|
||||||
|
stack.push(17)
|
||||||
|
stack.push(19)
|
||||||
|
stack.push(23)
|
||||||
|
stack.push(25)
|
||||||
|
stack.push(27)
|
||||||
|
# At this point, the stack is full
|
||||||
|
|
||||||
|
stack.push(29)
|
||||||
|
# => data_structures/stacks/stack.rb:18:in `push': StackOverflowError (StackOverflowError)
|
||||||
|
# from data_structures/stacks/stack.rb:83:in `<main>'
|
Loading…
Reference in a new issue