SlideShare a Scribd company logo
1 of 17
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
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.
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)
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
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 = []
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")
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")
Example
queue = Queue(5)
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.display()
queue.peek()
queue.dequeue()
queue.display()
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.display()
Output:
Enqueued item: 1
Enqueued item: 2
Enqueued item: 3
Queue: [1, 2, 3]
Front item: 1
Dequeued item: 1
Queue: [2, 3]
Dequeued item: 2
Dequeued item: 3
Queue is empty
Queue is empty
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.
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
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)
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)
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)
Example:
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.display()
queue.peek()
queue.dequeue()
queue.display()
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.display()
output:
Enqueued item: 1
Enqueued item: 2
Enqueued item: 3
Queue: [1, 2, 3]
Front item: 1
Dequeued item: 1
Queue: [2, 3]
Dequeued item: 2
Dequeued item: 3
Queue is empty
Queue is empty
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.
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
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.

More Related Content

What's hot

電商微服務架構設計與執行
電商微服務架構設計與執行電商微服務架構設計與執行
電商微服務架構設計與執行YC Liang
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript EngineKris Mok
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it WorksMike Dirolf
 
Introduction of Business Use-Case and Business Flowin Requirement Development
Introduction of Business Use-Case and Business Flowin Requirement DevelopmentIntroduction of Business Use-Case and Business Flowin Requirement Development
Introduction of Business Use-Case and Business Flowin Requirement DevelopmentKent Ishizawa
 
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCD
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCDOmeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCD
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCDJulien Sicot
 
카카오톡으로 여친 만들기 2013.06.29
카카오톡으로 여친 만들기 2013.06.29카카오톡으로 여친 만들기 2013.06.29
카카오톡으로 여친 만들기 2013.06.29Taehoon Kim
 
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Jemin Huh
 
Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Angel Boy
 
Django를 Django답게, Django로 뉴스 사이트 만들기
Django를 Django답게, Django로 뉴스 사이트 만들기Django를 Django답게, Django로 뉴스 사이트 만들기
Django를 Django답게, Django로 뉴스 사이트 만들기Kyoung Up Jung
 
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.Ted Vinke
 
Verteidigung Masterarbeit
Verteidigung MasterarbeitVerteidigung Masterarbeit
Verteidigung Masterarbeitwruge
 
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)Amazon Web Services Korea
 
The dark side of Akka and the remedy
The dark side of Akka and the remedyThe dark side of Akka and the remedy
The dark side of Akka and the remedykrivachy
 
채팅서버의 부하 분산 사례
채팅서버의 부하 분산 사례채팅서버의 부하 분산 사례
채팅서버의 부하 분산 사례John Kim
 
Evolution of film posters
Evolution of film postersEvolution of film posters
Evolution of film postersHollieRackley09
 

What's hot (16)

電商微服務架構設計與執行
電商微服務架構設計與執行電商微服務架構設計與執行
電商微服務架構設計與執行
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript Engine
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it Works
 
Introduction of Business Use-Case and Business Flowin Requirement Development
Introduction of Business Use-Case and Business Flowin Requirement DevelopmentIntroduction of Business Use-Case and Business Flowin Requirement Development
Introduction of Business Use-Case and Business Flowin Requirement Development
 
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCD
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCDOmeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCD
Omeka ou comment réaliser une bibliothèque numérique à l'échelle d'un SCD
 
카카오톡으로 여친 만들기 2013.06.29
카카오톡으로 여친 만들기 2013.06.29카카오톡으로 여친 만들기 2013.06.29
카카오톡으로 여친 만들기 2013.06.29
 
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
Spring boot 를 적용한 전사모니터링 시스템 backend 개발 사례
 
Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)
 
Django를 Django답게, Django로 뉴스 사이트 만들기
Django를 Django답게, Django로 뉴스 사이트 만들기Django를 Django답게, Django로 뉴스 사이트 만들기
Django를 Django답게, Django로 뉴스 사이트 만들기
 
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
 
Verteidigung Masterarbeit
Verteidigung MasterarbeitVerteidigung Masterarbeit
Verteidigung Masterarbeit
 
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)
AWS Batch를 통한 손쉬운 일괄 처리 작업 관리하기 - 윤석찬 (AWS 테크에반젤리스트)
 
The dark side of Akka and the remedy
The dark side of Akka and the remedyThe dark side of Akka and the remedy
The dark side of Akka and the remedy
 
채팅서버의 부하 분산 사례
채팅서버의 부하 분산 사례채팅서버의 부하 분산 사례
채팅서버의 부하 분산 사례
 
Evolution of film posters
Evolution of film postersEvolution of film posters
Evolution of film posters
 
cygwin安裝與vim
cygwin安裝與vimcygwin安裝與vim
cygwin安裝與vim
 

Similar to CD3291 2.6 Queue.pptx (20)

CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Circular queue
Circular queueCircular queue
Circular queue
 
Circular queue
Circular queueCircular queue
Circular queue
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
stack coding.pptx
stack coding.pptxstack coding.pptx
stack coding.pptx
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
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")
  • 8. Example queue = Queue(5) queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) queue.display() queue.peek() queue.dequeue() queue.display() queue.dequeue() queue.dequeue() queue.dequeue() queue.display() Output: Enqueued item: 1 Enqueued item: 2 Enqueued item: 3 Queue: [1, 2, 3] Front item: 1 Dequeued item: 1 Queue: [2, 3] Dequeued item: 2 Dequeued item: 3 Queue is empty 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)
  • 14. Example: queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) queue.display() queue.peek() queue.dequeue() queue.display() queue.dequeue() queue.dequeue() queue.dequeue() queue.display() output: Enqueued item: 1 Enqueued item: 2 Enqueued item: 3 Queue: [1, 2, 3] Front item: 1 Dequeued item: 1 Queue: [2, 3] Dequeued item: 2 Dequeued item: 3 Queue is empty Queue is empty
  • 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.