Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CHAPTER 10

FIFO Data Structure


● The First In First Out (Queue) Data structure – Example: Media player list, keyboard
buffer queue, printer queue etc. (to be used to explain concept of FIFO).
● Implementing the Queue and its operations using Python List.
● Priority Queues, Implementation.

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.

Some common examples include


• computer simulations.
• CPU process scheduling.
• shared printer management.
• Music Player
• Keyboard Buffer Queue

GOVERNMENT POLYTECHNIC, KARWAR 1


printing: jobs to be printed is stored on disk in a queue. The printer prints them in the
order it receives - first come first print. This allows users to work on other tasks after
sending the printing jobs to the queue.
Keyboard buffer queue: characters stored in a queue in a keyboard buffer.

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.

Define- Queue ADT

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.

• Queue(): Creates a new empty queue, which is a queue containing no items.


• isEmpty(): Returns a boolean value indicating whether the queue is empty.
• length (): Returns the number of items currently in the queue.
• enqueue( item ): Adds the given item to the back of the queue.
• dequeue(): Removes and returns the front item from the queue. An item cannot be
dequeued from an empty queue.

Implentation of Queue ADT

Figure : Abstract view of the queue after performing additional operations.

GOVERNMENT POLYTECHNIC, KARWAR 2


Let us implement a very simple queue using Python's list class. The operations that must be
performed on the queue are encapsulated in the Queue class:

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:

# Adds the given item to the queue.


def enqueue(self, item):
self.qList.append(item)

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.

# Removes and returns the first item in the queue.


def dequeue(self):
if self.qList.isEmpty():
print("Queue is Empty")
else:
self.data=self.qList.pop(0)

GOVERNMENT POLYTECHNIC, KARWAR 3


return self.data

Define -Priority Queue ADT

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.

• PriorityQueue(): Creates a new empty unbounded priority queue.


• BPriorityQueue( numLevels ): Creates a new empty bounded priority queue with
priority levels in the range from 0 to numLevels - 1.
• isEmpty(): Returns a boolean value indicating whether the queue is empty.
• length (): Returns the number of items currently in the queue.
• enqueue( item, priority ): Adds the given item to the queue by inserting it in the proper
position based on the given priority. The priority value must be within the legal range
when using a bounded priority queue.
• Dequeue(): Removes and returns the front item from the queue, which is the item with
the highest priority. The associated priority value is discarded. If two items have the
same priority, then those items are removed in a FIFO order. An item cannot be
dequeued from an empty queue.

Implentation of PriorityQueue ADT

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

GOVERNMENT POLYTECHNIC, KARWAR 4


PROGRAMS

IMPLEMENTATION OF QUEUE

(Queue.py)

# Implementation of the Queue ADT using a Python list.


class Queue:

def __init__(self):
self.qList = list()

def isEmpty(self):
return len(self) == 0

def __len__(self):
return len(self.qList)

def enqueue(self, item):


self.qList.append(item)

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)

from Queue import Queue


q=Queue()
print("Contents of Queue")
q.display()
print("Enque Operation")
q.enqueue(25)
q.enqueue(50)
q.enqueue(75)
q.enqueue(100)

GOVERNMENT POLYTECHNIC, KARWAR 5


print("Contents of Queue")
q.display()
print("Deque Operation")
print(q.dequeue()," is removed from queue")
print(q.dequeue()," is removed from queue")
print("Contents of Queue")
q.display()

OUTPUT

IMPLEMENTATION OF PRIORITY QUEUE

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

GOVERNMENT POLYTECHNIC, KARWAR 6


def enqueue(self, item, priority):

entry = PriorityQEntry(item, priority)


if self.__len__() == 0:
self.qList.append(entry)
else:
for x in range(0, len(self)):
if entry.priority >= self.qList[x].priority:
if x == (len(self)- 1):
self.qList.insert(x + 1, entry)
else:
continue
else:
self.qList.insert(x, entry)
return True

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)

from PriorityQueue import PriorityQueue


q=PriorityQueue()
print("Enque")
q.enqueue(25,3)
q.enqueue(50,2)
q.enqueue(75,1)
q.enqueue(100,6)
q.display()
print("Deque")
q.dequeue()
q.dequeue()
q.display()

GOVERNMENT POLYTECHNIC, KARWAR 7


OUTPUT

GOVERNMENT POLYTECHNIC, KARWAR 8

You might also like