以下操作请编写程序1.py上传
请定义类myDeque,实现队列的相关操作,要求:
1.编写构造方法,初始化队列,定义队列空间maxsize
2.编写析构方法,删除队列
3.编写显示队列的方法,显示队列的所有元素
4.编写显示队列元素数量的方法
5.编写测试队空的方法, isEmpty()
6.编写测试队满的方法,isFull()
7.编写入队方法(右侧入队)
8.编写出队方法(左侧出队)
使用该类定义对象,并调用相应的方法完成测试。
class myDeque(object):
#1.编写构造方法,初始化队列,定义队列空间maxsize
def __init__(self,maxsize=10):
self._content = []
self._size = maxsize
self._current = 0
#2.编写析构方法,删除队列
def __del__(self):
del self._content
#3.编写显示队列的方法,显示队列的所有元素
def dequeShow(self):
if self._content:
print(self._content)
else:
print('The queue is empty')
#4.编写显示队列元素数量的方法
def len(self):
print (self._current)
#5.编写测试队空的方法, isEmpty()
def isEmpty(self):
if not self._content:
print ('True')
else:
print ('False')
#6.编写测试队满的方法,isFull()
def isFull(self):
if self._current == self._size:
print ('True')
else:
print ('False')
#7.编写入队方法(右侧入队)
def appendRight(self,v):
if self._current < self._size:
self._content.append(v)
self._current = self._current + 1
else:
print('The queue is full!')
#8.编写出队方法(左侧出队)
def popLeft(self):
if self._content:
self._current = self._current - 1
return self._content.pop(0)
else:
print('The queue is empty!')
#使用该类定义对象,并调用相应的方法完成测试
if __name__=='__main__':
q = myDeque()
q.appendRight('hello')
q.appendRight('world')
q.appendRight('python')
q.dequeShow()
q.isEmpty()
q.popLeft()
q.popLeft()
q.popLeft()
q.isEmpty()
q.dequeShow()
q.appendRight('7')
q.appendRight('7')
q.appendRight('5')
q.appendRight(8)
q.len()
q.dequeShow()
q.appendRight(2)
q.appendRight(5)
q.appendRight('8')
q.appendRight('5')
q.appendRight('2')
q.len()
q.appendRight('1')
q.dequeShow()
q.len()
q.dequeShow()
q.isFull()
结果:
['hello', 'world', 'python']
False
True
The queue is empty
4
['7', '7', '5', 8]
9
['7', '7', '5', 8, 2, 5, '8', '5', '2', '1']
10
['7', '7', '5', 8, 2, 5, '8', '5', '2', '1']
True
>>>