데이터 구조 02 대기 열

2302 단어
대열
먼저 나 와. (LIFO: First In First Out)
# -.- coding:utf-8 -.-
from __future__ import print_function


def checker(func):

    def wrapper(self, *args, **kwargs):
        if self.empty():
            return self.items
        return func(self, *args, **kwargs)

    return wrapper


class Queue(object):

    """  :   ,     """

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

    def size(self):
        """        ."""
        return len(self.items)

    def empty(self):
        """       : True ;       : False"""
        return not self.size()

    def append(self, item):
        """             ."""
        self.items.append(item)
        return self.items

    @checker
    def peek(self):
        """             ,       ."""
        return self.items[0]

    @checker
    def get(self):
        """             ,       ."""
        return self.items.pop(0)

    def __iter__(self):
        """       """
        return self

    def __next__(self):
        """Python3:      for  """
        if not self.empty():
            return self.get()
        raise StopIteration

    def next(self):
        """Python2:      for  """
        return self.__next__()

    def __str__(self):
        """     print    """
        return "{}".format(self.items)


if __name__ == '__main__':
    #         .
    queue = Queue()

    #            
    queue.append('a')
    queue.append('b')
    queue.append('c')
    queue.append('d')
    queue.append('e')

    #       
    print("      : ", queue)

    #            
    print("           : ", queue.get())

    #       
    print("      : ", queue)

    #       
    for enum, i in enumerate(queue):
        print("   {}   : ".format(enum), i)

    #     
    #       :  ['a', 'b', 'c', 'd', 'e']
    #            :  a
    #       :  ['b', 'c', 'd', 'e']
    #    0   :  b
    #    1   :  c
    #    2   :  d
    #    3   :  e

좋은 웹페이지 즐겨찾기