【Python】通过List实现队列

本文详细介绍了使用Python如何通过list实现队列(先进先出和后进先出)以及双端队列的数据结构,包括基本操作如push、pop、size、clear等,并提供了实例代码和参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 队列

#!/usr/bin/env python
# -*- coding:utf-8 -*-

class Queue(object):
    """ list实现队列(左进右出) """

    def __init__(self):
        self._list = []

    def push(self, item):
        """ 在队尾插入元素 """
        self._list.insert(0, item)

    def pop(self):
        """ 删除并获取队首元素 """
        return self._list.pop()

    def size(self):
        """ 返回队列的大小 """
        return len(self._list)

    def clear(self):
        """ 清空队列 """
        self._list.clear()

    def is_empty(self):
        """ 判断队列是否为空 """
        return self._list == []

    def items(self):
        """ 返回队列中的所有元素 """
        return self._list


if __name__ == "__main__":
    q = Queue()  
    print(q.items())  # []
    q.push(1)
    print(q.items())  # [1]
    q.push(2)
    print(q.items())  # [2, 1]
    q.push(3)
    print(q.items())  # [3, 2, 1]
    q.push(4)
    print(q.items())  # [4, 3, 2, 1]
    q.pop()
    print(q.items())  # [4, 3, 2]
    q.pop()
    print(q.items())  # [4, 3]
    print(q.is_empty())  # False
    print(q.size())  # 2
    q.clear()
    print(q.items())  # []
    print(q.is_empty())  # True
#!/usr/bin/env python
# -*- coding:utf-8 -*-

class Queue(object):
    """ list实现队列(右进左出) """

    def __init__(self):
        self._list = []

    def push(self, item):
        """ 在队尾插入元素 """
        self._list.append(item)

    def pop(self):
        """ 删除并获取队首元素 """
        return self._list.pop(0)

    def size(self):
        """ 返回队列的大小 """
        return len(self._list)

    def clear(self):
        """ 清空队列 """
        self._list.clear()

    def is_empty(self):
        """ 判断队列是否为空 """
        return self._list == []

    def items(self):
        """ 返回队列中的所有元素 """
        return self._list


if __name__ == "__main__":
    q = Queue()
    print(q.items())  # []
    q.push(1)
    print(q.items())  # [1]
    q.push(2)
    print(q.items())  # [1, 2]
    q.push(3)
    print(q.items())  # [1, 2, 3]
    q.push(4)
    print(q.items())  # [1, 2, 3, 4]
    q.pop()
    print(q.items())  # [2, 3, 4]
    q.pop()
    print(q.items())  # [3, 4]
    print(q.is_empty())  # False
    print(q.size())  # 2
    q.clear()
    print(q.items())  # []
    print(q.is_empty())  # True

2. 双端队列

class DQueue(object):
    """ list实现双端队列 """

    def __init__(self):
        self._list = []

    def lpush(self, item):
        """ 在列左侧插入元素 """
        self._list.insert(0, item)

    def rpush(self, item):
        """ 在队列右侧插入元素 """
        self._list.append(item)

    def lpop(self):
        """ 删除并获取队列左侧第一个元素 """
        return self._list.pop(0)

    def rpop(self):
        """ 删除并获取队列右侧第一个元素 """
        return self._list.pop()

    def size(self):
        """ 返回队列的大小 """
        return len(self._list)

    def clear(self):
        """ 清空队列 """
        self._list.clear()

    def is_empty(self):
        """ 判断队列是否为空 """
        return self._list == []

    def items(self):
        """ 返回队列中的所有元素 """
        return self._list


if __name__ == "__main__":
    q = DQueue()
    print(q.items())  # []
    q.lpush(1)
    print(q.items())  # [1]
    q.lpush(2)
    print(q.items())  # [2, 1]
    q.rpush(3)
    print(q.items())  # [2, 1, 3]
    q.rpush(4)
    print(q.items())  # [2, 1, 3, 4]
    q.lpop()
    print(q.items())  # [1, 3, 4]
    q.rpop()
    print(q.items())  # [1, 3]
    print(q.is_empty())  # False
    print(q.size())  # 2
    q.clear()
    print(q.items())  # []
    print(q.is_empty())  # True

Reference

[1]: https://www.cnblogs.com/Wu13241454771/p/13691513.html
[2]: https://blog.youkuaiyun.com/sixkery/article/details/83149599
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值