Data Structures With Python Fifo Data Structure
Data Structures With Python Fifo Data Structure
A queue is a linear list in which data can only be inserted at one end, called the rear, and
deleted from the other end, called the front. These restrictions ensure that the data are
processed through the queue in the order in which they are received. In other words, a queue
is a first in–first out (FIFO) structure.
A queue is the same as a line. A line of people waiting for the bus at a bus station is a queue,
a list of calls put on hold to be answered by a telephone operator is a queue, and a list of
waiting jobs to be processed by a computer is a queue.
Figure shows two representations of a queue: one a queue of people and the other a computer
queue. Both people and data enter the queue at the rear and progress through the queue until
they arrive at the front. Once they are at the front of the queue, they leave the queue and are
served.
Music player software allows users the chance to add songs to a playlist. Upon hitting the
play button, all the songs in the main playlist are played one after the other. The sequential
playing of the songs can be implemented with queues because the first song to be queued is
the first song that is played.
A queue is a data structure that a linear collection of items in which access is restricted to a
first-in first-out basis. New items are inserted at the back and existing items are removed from
the front. The items are maintained in the order in which they are added to the structure.
class Queue:
# Creates an empty queue.
def __init__(self):
self.qList = list()
In the initialization method __init__, the items instance variable is set to [], which means the
queue is empty when created.
Enqueue operation
The enqueue operation or method uses the append method of the list class to insert items
(or data) at the front of the list:
Dequeue operation
The dequeue operation is used to remove items from the queue. With reference to the
introduction to the topic of queues, this operation captures the point where we serve the
customer who joined the queue first and also waited the longest:
The Python list class has a method called pop(). The pop method does the following:
1. Removes the last item from the list.
2. Returns the removed item from the list back to the user or code that called it.
The last item in the list is popped and saved in the data variable. In the last line of the method,
the data is returned.
A priority queue is a queue in which each item is assigned a priority and items with a higher
priority are removed before those with a lower priority, irrespective of when they were added.
Integer values are used for the priorities with a smaller integer value having a higher priority. A
bounded priority queue restricts the priorities to the integer values between zero and a
predefined upper limit whereas an unbounded priority queue places no limits on the range of
priorities.
When implementing the priority queue, however, the items cannot simply be added directly
to the list, but instead we must have a way to associate a priority with each item. This can be
accomplished with a simple storage class containing two fields: one for the priority and one for
the queue item.
For example:
class PriorityQEntry :
def __init__( self, item, priority ):
self.item = item
self.priority = priority
IMPLEMENTATION OF QUEUE
(Queue.py)
def __init__(self):
self.qList = list()
def isEmpty(self):
return len(self) == 0
def __len__(self):
return len(self.qList)
def dequeue(self):
assert not self.isEmpty(), "Cannot dequeue from an empty queue."
data=self.qList.pop(0)
return data
def display(self):
print(self.qList)
(Qtest.py)
OUTPUT
(PriorityQueue.py)
# Implementation of the unbounded Priority Queue ADT using a Python list
# with new items appended to the end.
class PriorityQueue:
def __init__(self):
self.qList = list()
def isEmpty(self):
return len(self) == 0
def __len__(self):
return len(self.qList)
def dequeue(self):
assert not self.isEmpty(), "Cannot dequeue from an empty queue."
return self.qList.pop(0)
def display(self):
for x in self.qList:
print (str(x.item)+"-"+str(x.priority))
class PriorityQEntry(object):
def __init__(self, item, priority):
self.item = item
self.priority = priority
(QueueTest.py)