通过python设计实现队列以及栈,复习一下数据结构
队列:先进先出
class Queue(object):
def __init__(self,size):
self.queue = []
self.size = size
def is_empty(self):
if len(self.queue) == 0:
return True
else:
return False
def is_full(self):
if len(self.queue) == self.size:
return True
else:
return False
def pop(self):
if self.is_empty():
raise Exception("QueueIsEmpty")
else:
top = self.queue[0]
self.queue.remove(top)
return top
def pull(self,data):
if self.is_full:
raise Exception("QueueOverFlow")
else:
self.queue.append(data)
def show(self):
print self.queue```
栈:先进后出,pop弹出栈顶元素
class Stack(object):
def init(self,size=10):
self.stack = []
self.size = size
self.top = -1
def is_full(self):
return self.top + 1 == 10
def is_empty(self):
return self.top == -1
def push(self,data):
if self.is_full():
raise Exception("StackOverFlow")
else:
self.stack.append(data)
self.top+=1
def pop(self):
if self.is_empty():
raise Exception("StackIsEmpty")
else:
self.stack.pop()
self.top -=1
def top(self):
if self.is_empty():
raise Exception("StackIsEmpty")
else:
return self.stack[self.top]
def show(self):
print self.stack