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.csdn.net/sixkery/article/details/83149599
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐