python请定义类myDeque,实现队列的相关操作

以下操作请编写程序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
>>> 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值