python中的list中的API函数就很像栈和队列的,所以实现栈和队列用到了list
栈:
class stack():
def __init__(self):
self.stack = []
def empty(self):
return self.stack==[]
def push(self,data):
self.stack.append(data)
def pop(self):
if self.empty():
return None;
else:
return self.stack.pop(-1)
def top(self):
if self.empty():
return None
else:
return self.stack[-1]
def length(self):
return len(self.stack)
队列:
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)
python真是个非常简约的语言,写起东西来很舒服呢。
接下来会写点有意思的东西,哟好好好好