Reputation: 129
I'm a beginner in Python.
The program below is a Last-In First-Out (LIFO). I want to make it First-in First-Out (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))
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" should be at the bottom of "Arc" or in FIFO (pic below is LIFO)
I'm thinking to put a new def like setPrevious
in the NodeList
but I don't know how: to be honest I'm really new on these self.head = none
stuffs. I used to write self.items = []
.
Any help and tips will be appreciated! Thank you!
Upvotes: 10
Views: 32786
Reputation: 1087
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()
Upvotes: 0
Reputation: 4749
Well, seeing as your class is probably over now and you didn't mention your class (or that it had to be a linked list) in the question itself, I'll just tell you the built-in easy way to do it, for now, which is probably more pertinent to your current situation (and will help people who find your question).
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
This outputs
first
second
third
Really, though, I'm not sure what advantages this has over Constantinius's answer, but since it's an included module, I would think there must be an advantage somewhere. I know they are used with threads from the threading module. There are more functions associated with Queues than I've mentioned here.
To learn more, open your Python interpreter and type this:
from queue import Queue #or from Queue import Queue for 2.x
help(Queue) #Press q to exit the help
Don't ask me what blocking is, but this may use the term how it's used in the Queue class documentation: http://en.wikipedia.org/wiki/Blocking_(computing)
Upvotes: 1
Reputation: 35069
Apart from learning purposes I would not advise using a custom data structure for making a LIFO or FIFO. The built in data-type list
is just fine after all.
You can add items using the append
method and remove them using pop
. For a LIFO this would look like this:
stack = list()
stack.append(1)
stack.append(2)
stack.append(3)
print stack.pop() #3
print stack.pop() #2
print stack.pop() #1
If you supply an integer argument for pop
you can specify which element to remove. For a FIFO use the index 0
for the first element:
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
Upvotes: 33