Python实现队列

使用Python的list快速实现一个队列

import copy


class Queue(object):
    def __init__(self, *arg):
        super(Queue, self).__init__()
        self.__queue = list(copy.copy(arg))
        self.__size = len(self.__queue)

    def enter(self, value):
        self.__size += 1
        self.__queue.append(value)

    def exit(self):
        if self.__size <= 0:
            return None
        else:
            value = self.__queue[0]
            self.__size -= 1
            del self.__queue[0]
            return value

    def __len__(self):
        return self.__size

    def empty(self):
        return self.__size <= 0

    def __str__(self):
        return "".join(["Queue(list=", str(self.__queue), ",size=", str(self.__size)])


if __name__ == '__main__':
    l = list()
    queue = Queue(l, 1, 2, 3, 4, 5)
    queue.enter(1)
    queue.exit()
    print(queue.__str__())

你可能感兴趣的:(Python实现队列)