Python3创建类模拟双端循环队列

class DoubleCycleQuene:  # 双端循环队列类
    def __init__(self, n):  # 实例属性定义,n为定义队列时设置的最大长度
        self.__quene = []  # 用列表存放队列元素
        self.__max = n  # 列表长度
        self.__front = 0  # 代表列表头指针,队列头始终为0
        self.__rear = 0  # 代表队列尾指针

    def dcpush(self, location, num):  # 插入元素,location为插入位置,num为插入元素
        # location=0,从队头插;location=-1,从队尾插
        if len(self.__quene) < self.__max and location == 0:
            self.__quene.insert(0, num)
            print("插入成功!当前队列为:", self.__quene)
            self.__dcrear()
            return True
        elif len(self.__quene) < self.__max and location == -1:
            self.__quene.append(num)
            print("插入成功!当前队列为:", self.__quene)
            self.__dcrear()
            return True
        # 队满或参数错误,报错
        elif len(self.quene) == self.__max or (location != 0 and location != -1):
            print("队列已满或插入位置错误!")
            return False

    def dcpop(self, location):  # 删除元素,location为删除位置
        # location=0,从队头删;location=-1,从队尾删
        if len(self.__quene) > 0 and location == 0:
            self.__quene.pop(0)
            print("删除成功!当前队列为:", self.__quene)
            self.__dcrear()
            return True
        elif len(self.quene) > 0 and location == -1:
            self.__quene.pop()
            print("删除成功!当前队列为:", self.__quene)
            self.__dcrear()
            return True
            # 队空或参数错误,报错
        elif len(self.quene) == 0 or (location != 0 and location != -1):
            print("队列已空或插入位置错误!")
            return False

    def __dcrear(self):  # 队尾指针随队列长度变动
        self.__rear = len(self.__quene)
        if self.__rear == self.__max:  # 当队满的时候,队尾与队头重合
            self.__rear = 0

    def dclen(self):  # 返回队列长度
        return len(self.__quene)

    def dcmaxLen(self): #返回队列最大长度
        return self.__max

    def dcisEmpty(self):  # 返回队列是否为空
        if len(self.__quene) == 0:
            return True
        else:
            return False

    def dcisFull(self):  # 返回队列是否已满
        if len(self.__quene) == self.__max:
            return True
        else:
            return False

    def dcshow(self): #返回队列
        return self.__quene

新手一枚,如有错误,敬请指正,感谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值