1、使用python实现栈:
1 class stack(): 2 def __init__(self): 3 self.stack = [] 4 def empty(self): 5 return self.stack==[] 6 def push(self,data): 7 self.stack.append(data) 8 def pop(self): 9 if self.empty(): 10 return None; 11 else: 12 return self.stack.pop(-1) 13 def top(self): 14 if self.empty(): 15 return None 16 else: 17 return self.stack[-1] 18 def length(self): 19 return len(self.stack)
2、使用python实现队列:
class queue(): def __init__(self): self.queue = [] def empty(self): return self.queue == [] def enqueue(self,data): self.queue.append(data) def dequeue(self): if self.empty(): return None else: return self.queue.pop(0) #返回头信息和队列长度可以不实现 def head(self): if self.empty(): return None else: return self.queue[0] def length(self): return len(self.queue)