Python:先进先出打印

时间:2013-10-07 08:14:34

标签: python lifo

我是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))

这是NodeList:

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中添加类似setPrevious的新def,但我不知道如何。 (老实说,我对这些self.head = none东西真的很新。我曾经写过self.items = [])

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

3 个答案:

答案 0 :(得分:24)

除了学习目的,我不建议使用自定义数据结构来制作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

答案 1 :(得分:1)

好吧,看到你的课程现在已经结束了,你没有在问题本身中提到你的课程(或者它必须是一个链表),我只会告诉你内置的简单方法现在这样做,这可能与你目前的情况更相关(并将帮助那些找到你问题的人)。

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)

答案 2 :(得分:0)

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()