From caa5afd97d6b5b18873870980c0f0435fc9dcdc3 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 13:57:41 -0800 Subject: [PATCH 1/4] Add Stack data structure: simple stack operations --- data_structures/stacks/stack.rb | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 data_structures/stacks/stack.rb diff --git a/data_structures/stacks/stack.rb b/data_structures/stacks/stack.rb new file mode 100644 index 0000000..12c5b31 --- /dev/null +++ b/data_structures/stacks/stack.rb @@ -0,0 +1,76 @@ +# 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(stack, limit) + @stack = [] + @limit = limit + end + + attr_accessor :stack, :limit + + def push(item) + if stack.count < limit + stack << item + else + raise StackOverflowError + end + end + + def pop + stack.pop + end + + def peek + stack.last + end + + def is_empty? + stack.count == 0 + end + + def is_full + stack.count == limit + end + + def size + stack.count + end + + def contains?(item) + stack.include?(item) + end +end + +stack = Stack.new([], 10) + +puts stack.is_empty? +# => true + +stack.push(3) +stack.push(5) +stack.push(7) +stack.push(9) + +puts stack.is_full +# => false + +puts stack.contains?(5) +# => true + +puts stack.pop +# => 9 + +puts stack.peek +# => 7 + +puts stack.size +# => 3 + +puts stack.inspect +# => # From f0a01ee3daa7aeffde0e4ee31bb619f5103474f9 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 14:02:46 -0800 Subject: [PATCH 2/4] Minor changes --- data_structures/stacks/stack.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/data_structures/stacks/stack.rb b/data_structures/stacks/stack.rb index 12c5b31..c2f9784 100644 --- a/data_structures/stacks/stack.rb +++ b/data_structures/stacks/stack.rb @@ -7,19 +7,17 @@ class StackOverflowError < StandardError; end class Stack - def initialize(stack, limit) - @stack = [] + def initialize(limit, stack = []) + @stack = stack @limit = limit end attr_accessor :stack, :limit def push(item) - if stack.count < limit - stack << item - else - raise StackOverflowError - end + raise StackOverflowError unless stack.count < limit + + stack << item end def pop @@ -30,11 +28,11 @@ class Stack stack.last end - def is_empty? - stack.count == 0 + def empty? + stack.count.zero? end - def is_full + def full? stack.count == limit end @@ -47,9 +45,9 @@ class Stack end end -stack = Stack.new([], 10) +stack = Stack.new(10, []) -puts stack.is_empty? +puts stack.empty? # => true stack.push(3) @@ -57,7 +55,7 @@ stack.push(5) stack.push(7) stack.push(9) -puts stack.is_full +puts stack.full? # => false puts stack.contains?(5) From 80ecfe2270ae1add0f1f632401ecd9abf465065f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 14:07:08 -0800 Subject: [PATCH 3/4] Add StackOverflowError example --- data_structures/stacks/stack.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/data_structures/stacks/stack.rb b/data_structures/stacks/stack.rb index c2f9784..c45283d 100644 --- a/data_structures/stacks/stack.rb +++ b/data_structures/stacks/stack.rb @@ -72,3 +72,16 @@ puts stack.size puts stack.inspect # => # + +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 `
' From 97b8dd54cdcbaf45586470867c4cb8a4cf4a6cbd Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 27 Dec 2020 14:14:26 -0800 Subject: [PATCH 4/4] Update DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..46b6900 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -11,6 +11,9 @@ * Linked Lists * [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) + * Stacks + * [Stack](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/stacks/stack.rb) + ## Discrete Mathematics * [Euclidean Gcd](https://github.com/TheAlgorithms/Ruby/blob/master/discrete_mathematics/euclidean_gcd.rb)