# C/C++ 로 배우는 자료구조론 연습문제 6.34
class DropOutStack
def initialize(size)
@top = -1
@memory = []
@stack_size = size
end
def is_full
return @top >= (@stack_size - 1)
end
def is_empty
return @top == -1
end
def push(data)
if is_full
shift_down
end
@top += 1
@memory[@top] = data
end
def pop
return nil if is_empty
data = @memory[@top]
@top -= 1
return data
end
private
def shift_down
return if is_empty
(0..@top).each do |idx|
@memory[idx] = @memory[idx+1]
end
@top -= 1
end
end
stack = DropOutStack.new(3)
puts stack.push(5)
puts stack.push(6)
puts stack.push(7)
puts stack.push(8)
puts stack.pop
puts stack.pop
puts stack.pop
puts stack.pop