【问题标题】:Python: First In First Out PrintPython:先进先出打印
【发布时间】:2013-10-07 08:14:34
【问题描述】:

我是 python 的初学者,我遇到了这个程序的问题:

下面的程序是后进先出 (LIFO)。我想做先进先出 (FIFO) 程序。

from NodeList import Node

class QueueLL:

    def __init__(self):
        self.head = None


    def enqueueQLL(self,item):
        temp = Node(str(item))
        temp.setNext(self.head) 
        self.head = temp
        length = max(len(node.data) for node in self.allNodes()) if self.head else 0
        print('\u2510{}\u250c'.format(' '*length))
        for node in self.allNodes():
            print('\u2502{:<{}}\u2502'.format(node.data, length))
        print('\u2514{}\u2518'.format('\u2500'*length))

这里是节点列表:

class Node:

    def __init__(self,initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

注意:“Rainbow”应位于“Arc”的底部或在 FIFO 中(下图为 LIFO)

我正在考虑在 NodeList 中添加一个新的 def,比如 setPrevious,但我不知道怎么做。 (说实话,我对这些 self.head = none 东西真的很陌生。我曾经写过 self.items = [])

任何帮助和提示将不胜感激!谢谢!

【问题讨论】:

    标签: python lifo


    【解决方案1】:

    除了学习目的之外,我不建议使用自定义数据结构来制作 LIFO 或 FIFO。毕竟内置的数据类型list就很好了。

    您可以使用append 方法添加项目并使用pop 删除它们。对于 LIFO,这看起来像这样:

    stack = list()
    stack.append(1)
    stack.append(2)
    stack.append(3)
    
    print stack.pop()  #3
    print stack.pop()  #2
    print stack.pop()  #1
    

    如果您为pop 提供整数参数,您可以指定要删除的元素。对于 FIFO,使用索引 0 作为第一个元素:

    stack = list()
    stack.append(1)
    stack.append(2)
    stack.append(3)
    
    print stack.pop(0)  #1
    print stack.pop(0)  #2
    print stack.pop(0)  #3
    

    【讨论】:

    • 我也想使用内置数据类型,但我不知道如何将其转换为 LinkList。
    • 为什么需要链表?
    • 我们班要求我使用链表实现一个队列(包括其他数据结构类型)。
    • list 适合堆栈,因为 .append().pop() 都很快。但是,它不是 FIFO 队列的最佳选择,因为 .pop(0) 很慢(它的复杂性不是 O(1))。使用 collections.deque 进行 FIFO。
    【解决方案2】:

    好吧,鉴于您的课程现在可能已经结束,并且您没有在问题本身中提及您的课程(或者它必须是一个链接列表),我将告诉您内置的简单方法现在就去做吧,这可能更适合您当前的情况(并且会帮助找到您问题的人)。

    import sys;
    if sys.version_info[0]>2: #Just making sure the program works with both Python 2.x and 3.x
        from queue import Queue
    else:
        from Queue import Queue
    
    q=Queue()
    q.put("first") #Put an item on the Queue.
    q.put("second")
    q.put("third")
    
    while not q.empty(): #If it's empty, the program will stall if you try to get from it (that's why we're checking)
        print(q.get()) #Get an item from the Queue
    

    这个输出

    first
    second
    third
    

    真的,不过,我不确定这比康斯坦丁纽斯的回答有什么优势,但由于它是一个包含的模块,我认为在某个地方一定有优势。我知道它们与线程模块中的线程一起使用。与队列相关的功能比我在这里提到的要多。

    要了解更多信息,请打开您的 Python 解释器并输入以下内容:

    from queue import Queue #or from Queue import Queue for 2.x
    help(Queue) #Press q to exit the help
    

    不要问我什么是阻塞,但是这个可能使用它在 Queue 类文档中的使用方式: http://en.wikipedia.org/wiki/Blocking_(computing)

    【讨论】:

    • 队列类型是线程保存的,而列表类型不是(这就是队列可以阻塞的原因)。在这里以 fifo 方式创建、填充和清空列表比使用 Queue 快 30 倍以上。因此,如果您不需要线程保护......
    【解决方案3】:
    class Box:
        def __init__(self,data):
            self.data=data
            self.next=None        
    class List:
        def __init__(self):
            self.head=None        
        def add(self,item):                
            temp=Box(item)                
            if self.head==None:
                self.head=temp
                self.prev=temp
            self.prev.next=temp
            self.prev=self.prev.next               
        def PrintList(self):
            while self.head!=None:
                print(self.head.data)
                self.head=self.head.next
    
    myList=List()
    myList.add("Vinoth")
    myList.add("Karthick")
    myList.add("Ganesh")
    myList.add("Malai")
    myList.add("Shan")
    myList.add("Saravana")
    myList.PrintList()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多