The document discusses the queue data structure, including its definition as a First In First Out (FIFO) linear structure, basic operations like enqueue and dequeue, implementations using lists and linked lists, advantages like efficient management of large amounts of data, disadvantages like inability to insert/delete from middle, and applications in areas like scheduling and queues. Key points covered include common queue operations, list and linked list implementations in Python with examples, and uses of queues in real world scenarios.
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
CD3291 2.6 Queue.pptx
1. 2.6 QUEUE ADT
i) Definition
ii) Basic Operation
iii) Implementation
iv) Advantage
v) Disadvantage
vi) Application Prepared by
Mrs. G. Mareeswari
Assistant Professor,
Department of Information Technology,
Ramco Institute of Technology,
Rajapalayam
2. Definition:
• Queue is a linear data structure that stores items in First In First Out
(FIFO) manner.
• With a queue the least recently added item is removed first.
• A good example of queue is any queue of consumers for a resource
where the consumer that came first is served first.
3. Basic Operation:
Enqueue:
Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition – Time Complexity : O(1)
Dequeue:
Removes an item from the queue. The items are popped in the same
order in which they are pushed. If the queue is empty, then it is said to be an
Underflow condition – Time Complexity : O(1)
• Front:
Get the front item from queue – Time Complexity : O(1)
• Rear:
Get the last item from queue – Time Complexity : O(1)
4. Implementation:
There are various ways to implement a queue in Python.
Some of them are
• Implementation of Queue ADT using list
• Implementation of Queue ADT using Linked list
5. Implementation of Queue ADT using list
• List is a Python’s built-in data structure that can be used as a queue.
• Instead of enqueue() and dequeue(), append() and pop() function is used.
• However, lists are quite slow for this purpose because inserting or deleting
an element at the beginning requires shifting all of the other elements by
one, requiring O(n) time.
i) Queue Creation:
class Queue:
def __init__(self, size):
self.size = size
self.queue = []
6. Operations:
ii) Insertion/ Enqueue:
def enqueue(self, item):
if len(self.queue) < self.size:
self.queue.append(item)
print("Enqueued item:", item)
else:
print("Queue is full")
iii) Deletion/ Dequeue:
def dequeue(self):
if len(self.queue) > 0:
item = self.queue.pop(0)
print("Dequeued item:", item)
else:
print("Queue is empty")
7. Operations:
iv) Peek():
def peek(self):
if len(self.queue) > 0:
print("Front item:", self.queue[0])
else:
print("Queue is empty")
v) Display()
def display(self):
if len(self.queue) > 0:
print("Queue:", self.queue)
else:
print("Queue is empty")
9. Implementation of Queue using linked list
• In a linked queue, each node of the queue consists of two parts i.e.
data part and the link part.
• Each element of the queue points to its immediate next element in
the memory.
• In the linked queue, there are two pointers maintained in the
memory i.e. front pointer and rear pointer.
• The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element
of the queue.
10. Operations in linked implementation of queue
i) Creation of Queue:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.front = None
self.rear = None
11. Operations in linked implementation of queue
ii) Enqueue():
def enqueue(self, item):
new_node = Node(item)
if self.rear is None:
self.front = new_node
self.rear = new_node
else:
self.rear.next = new_node
self.rear = new_node
print("Enqueued item:", item)
12. Operations in linked implementation of queue
iii) Dequeue():
def dequeue(self):
if self.front is None:
print("Queue is empty")
else:
item = self.front.data
self.front = self.front.next
if self.front is None:
self.rear = None
print("Dequeued item:", item)
13. Operations in linked implementation of queue
iv) Peek():
def peek(self):
if self.front is None:
print("Queue is empty")
else:
print("Front item:", self.front.data)
v) Display():
def display(self):
if self.front is None:
print("Queue is empty")
else:
current = self.front
queue_items = []
while current:
queue_items.append(current.data)
current = current.next
print("Queue:", queue_items)
15. Advantage of Queue ADT:
• A large amount of data can be managed efficiently with ease.
• Operations such as insertion and deletion can be performed with ease
as it follows the first in first out rule.
• Queues are useful when a particular service is used by multiple
consumers.
• Queues are fast in speed for data inter-process communication.
• Queues can be used in the implementation of other data structures.
16. Disadvantage of Queue ADT
• The operations such as insertion and deletion of elements from the
middle are time consuming.
• Limited Space.
• In a classical queue, a new element can only be inserted when the
existing elements are deleted from the queue.
• Searching an element takes O(N) time
17. Application of Queue ADT
• ATM Booth Line
• Ticket Counter Line
• Key press sequence on the keyboard
• CPU task scheduling
• Waiting time of each customer at call centers.