coding=utf-8
一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底
class Stack():
def __init__(self,size):
self.size=size
self.top=0
self.val = []
def push(self,data):
if self.isfull():
return 'The Stack is full'
else:
self.val.append(data)
self.top=self.top+1
return self.val
def pop(self):
if self.isempty():
return 'The Stack is empty'
else:
self.val.pop(-1)
self.top=self.top-1
return self.val
def length(self):
return len(self.val)
def isfull(self):
if self.top==self.size:
return True
def isempty(self):
if self.top==0:
return True
def string(self):
return str(self.val)
第二部分:队列类
coding=utf-8
队列在表的前端进行删除操作(队头),后端进行插入操作(队尾)
class Queue():
def __init__(self,size):
self.size=size
self.tail=0
self.head=0
self.que = []
def append(self,data):
if self.isfull():
return 'The Queue is full'
else:
self.que.append(data)
self.tail=self.tail+1
return self.que
def pop(self):
if self.isempty():
return 'The Queue is empty'
else:
self.que.pop(0)
self.head=self.head+1
return self.que
def length(self):
return len(self.que)
def isfull(self):
if self.tail-self.head==self.size:
return True
def isempty(self):
if self.tail==self.head:
return True
def string(self):
return str(self.que)
第三部分堆栈和队列的调用截图
堆栈调用截图
队列调用截图