Python: first in first print

I am starting to work on python and I have a problem with this program:

The program below is Last In First Out (LIFO). I want to make a First in First Out (FIFO) program.

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

Here is the 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

NOTE: The “rainbow” must be at the bottom of the “arc” or in the FIFO (Figure below LIFO)

I am thinking of adding a new def, like setPrevious to a NodeList. But I do not know how to do this. (to be honest, I'm really new to these self.head = none stuffs. I used to write self.items = [])

Any help and advice would be appreciated! Thanks!

+4
source share
2 answers

, 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
+21

, , , , , , ( ) , , , , , ( , ).

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 empty, the program will stall if you try to get from it (that 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_ ()

+1

All Articles