2018-08-01

2018-08-01  本文已影响0人  Jeese_zhao

Python部分

# coding:utf-8
# 首先获取节点,包含next指针和该节点位置上元素的值
class Node(object):
    def __init__(self, val):
        self.next = None
        self.val = val


class Queue(object):
    def __init__(self):
        self.first = None
        self.last = None

    # 进队操作
    def enter(self, n):
        # 实例节点
        n = Node(n)
        # 进队之前先判断队列是否为空,即判断first是否为None
        if self.first == None:
            # 此时last==first==n
            self.first = n
            self.last = self.first
        else:
            # 将last的指针设置为n,值设置为n
            self.last.next = n
            self.last = n

    # 出队
    def quit(self):
        if self.first == None:
            return None
        else:
            tmp = self.first.val
            self.first = self.first.next
        return tmp

    # 保存队列元素到列表
    def allQuit(self):
        Lists = []
        while self.first != None:
            Lists.append(self.first.val)
            self.first = self.first.next
        return Lists


if __name__ == '__main__':
    q = Queue()
    q.enter(1)
    q.enter(2)
    q.enter(3)
    # print(q.quit())
    # print(q.quit())
    # print(q.quit())
    # print(q.quit())

    print(q.allQuit())


上一篇下一篇

猜你喜欢

热点阅读