阿C
栏目管理员
查看个人网站
查看详细资料
TOP
初入江湖
class Stack def initialize(m) @stack=m end def push(m) @stack<<m end def pop() if @stack.empty? puts "the stack is empty" else puts @stack.delete_at(@stack.length-1) end end end aStack=Stack.new([]) aStack.push(1) aStack.push(2) aStack.pop()#=>2 aStack.push(3) aStack.pop()#=>3 aStack.pop()#=>1 aStack.pop()#=>the stack is empty class Queue def initialize(n) @queue=n end def push(n) @queue<<n end def pop() if @queue.empty? puts "the queue is empty" else puts @queue.delete_at(0) end end end aQueue=Queue.new([]) aQueue.push(1) aQueue.push(2) aQueue.pop()#=>1 aQueue.push(3) aQueue.pop()#=>2 aQueue.pop()#=>3 aQueue.pop()#=>the stack is empty