mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
Minor changes
This commit is contained in:
parent
caa5afd97d
commit
f0a01ee3da
1 changed files with 11 additions and 13 deletions
|
@ -7,19 +7,17 @@
|
||||||
class StackOverflowError < StandardError; end
|
class StackOverflowError < StandardError; end
|
||||||
|
|
||||||
class Stack
|
class Stack
|
||||||
def initialize(stack, limit)
|
def initialize(limit, stack = [])
|
||||||
@stack = []
|
@stack = stack
|
||||||
@limit = limit
|
@limit = limit
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :stack, :limit
|
attr_accessor :stack, :limit
|
||||||
|
|
||||||
def push(item)
|
def push(item)
|
||||||
if stack.count < limit
|
raise StackOverflowError unless stack.count < limit
|
||||||
|
|
||||||
stack << item
|
stack << item
|
||||||
else
|
|
||||||
raise StackOverflowError
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop
|
def pop
|
||||||
|
@ -30,11 +28,11 @@ class Stack
|
||||||
stack.last
|
stack.last
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_empty?
|
def empty?
|
||||||
stack.count == 0
|
stack.count.zero?
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_full
|
def full?
|
||||||
stack.count == limit
|
stack.count == limit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,9 +45,9 @@ class Stack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
stack = Stack.new([], 10)
|
stack = Stack.new(10, [])
|
||||||
|
|
||||||
puts stack.is_empty?
|
puts stack.empty?
|
||||||
# => true
|
# => true
|
||||||
|
|
||||||
stack.push(3)
|
stack.push(3)
|
||||||
|
@ -57,7 +55,7 @@ stack.push(5)
|
||||||
stack.push(7)
|
stack.push(7)
|
||||||
stack.push(9)
|
stack.push(9)
|
||||||
|
|
||||||
puts stack.is_full
|
puts stack.full?
|
||||||
# => false
|
# => false
|
||||||
|
|
||||||
puts stack.contains?(5)
|
puts stack.contains?(5)
|
||||||
|
|
Loading…
Reference in a new issue