“ 队列”:
特殊的线性表,只允许在队列尾部插入元素,在队列头部删除元素。
特点:“先进先出(FIFO)”,“后入后出(LILO)”
class CustomQueue:
'''自定义(双端)队列'''
'''构造方法'''
def __init__(self,iterable=None,Maxlen=10):
if iterable == None:
self._content = []
self._current = 0
else:
self._content = list(iterable)
self._current = len(iterable)
self._size = Maxlen
if self._size < self._current:
self._size = self._current
'''析构方法'''
def __del__(self):
del self._content
'''测试队列是否已满'''
def isFull(self):
return self._current == self._size
'''测试队列是否为空'''
def isEmpty(self):
return not self._content
'''print打印显示'''
def __str__(self):
return '队列内含有元素:'+str(self._content)+'队列目前长度:'+str(len(self._content))+',队列规模:'+str(self._size)
'''复制__str__,直接对象名当表达式,遍历显示元素'''
__repr__ = __str__
'''队列置空'''
def clearqueue(self):
self._current = 0
self._content = 0
'''显示当前队列元素个数'''
def __len__(self):
return self._current
'''修改队列大小'''
def setqueuesize(self,size):
if size < self._current:
for i in range(size,self._current):
del self._content[i]
self._size = size
'''右侧方入队'''
def rightappend(self,x):
if self._current < self._size:
self._content.append(x)
self._current = self._current + 1
else :
print('队列已满,无法入队')
'''左侧方入队'''
def leftappend(self,x):
if self._current < self._size:
self,_content.insert(0,x)
self._current = self._current + 1
else :
print('队列已满,无法入队')
'''右侧方出队'''
def rightpop(self):
if self._content:
self._content.pop()
self._current = self._current - 1
else :
print('队列为空,无法出队')
'''左侧出队'''
def leftpop(self):
if self._content:
self._content.pop(0)
self._current = self._current - 1
else :
print('队列为空,无法出队')
'''全部元素翻转'''
def reverse(self):
return self._content.reverse()
'''队列循环移位'''
def rotate(self,x):
if abs(x) > self._current:
print('移位失败,移位数应小于当前队列长度')
else :
self._content = self._content[-x:] + self._content[:-x]
自定义队列测试:?
>>> %Run 'Custom queue.py'
>>> queue1 = CustomQueue([5,2,6,7,9,4,1])
>>> queue1.rightappend(8)
>>> queue1
队列内含有元素:[5, 2, 6, 7, 9, 4, 1, 8]队列目前长度:8,队列规模:10
>>> queue1.rotate(3)
>>> queue1.leftpop()
>>> queue1.setqueuesize(20)
>>> print(queue1)
队列内含有元素:[1, 8, 5, 2, 6, 7, 9]队列目前长度:7,队列规模:20
>>> queue1.isFull()
False
>>> queue1.clearqueue()
>>> queue1
<repr error: object of type 'int' has no len()>
“栈”:
?运算受限的线性表,仅允许在一段进行元素的插入删除操作,最后入栈的最先出,最先入栈的最后出。
特点:“先入后出(FILO)”,“后入先出(LIFO)”
class Customstack:
'''自定义栈'''
'''构造方法'''
def __init__(self,Maxlen=10):
self._content = []
self._current = 0
self._size = Maxlen
'''析构方法'''
def __del__(self):
del self._content
'''print()打印显示全部元素'''
def __str__(self):
return 'Customstack全部元素:'+str(self._content)+',全栈规模:'+str(self._size)
'''复制__str__,直接对象名当表达式,遍历显示元素'''
__repr__ = __str__
'''置空全栈'''
def clearstock(self):
self._content = []
self._current = 0
'''测试栈是否为空'''
def isempty(self):
return not self._content
'''测试栈是否已满'''
def isFull(self):
return self._current == self._size
'''修改栈容量'''
def setstocksize(self,size):
if self._current >size:
print('修改栈容量失败,请选大于{0}数值。'.format(self._size))
return
self._size = size
'''入栈'''
def pushstock(self,x):
if self._current < self._size:
self._current = self._current + 1
self._content.append(x)
else :
print('入栈失败,全栈已满。')
'''出栈'''
def popstock(self):
if self._content:
self._current = self._current - 1
return self._content.pop()
else :
print('入栈失败,全栈为空。')
?自定义栈调试:
>>> %Run Customstack.py
>>> stock1 = Customstack(3)
>>> stock1.pushstock(78)
>>> stock1.pushstock(23)
>>> stock1.pushstock(15)
>>> stock1
Customstack全部元素:[78, 23, 15],全栈规模:3
>>> stock1.isFull()
True
>>> stock1.popstock()
15
>>> print(stock1)
Customstack全部元素:[78, 23],全栈规模:3
>>> stock1.setstocksize(2)
修改栈容量失败,请选大于5数值。
>>> stock1.setstocksize(5)
>>> stock1.clearstock()
>>> stock1.isempty()
True
>>> stock1
Customstack全部元素:[],全栈规模:5
|