SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Queue Data Structure
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Notations
• Prefix, Infix and Postfix Notations
• Conversion of one type expression to another
• Evaluation of Prefix and Postfix Notations
Objectives Overview
• Introduction to Queue Data Structure
• Types of Queue Data Structures
• Circular QUEUE and its Operations
• Double Ended QUEUE and its operations
What is a queue?
• It is an ordered group of homogeneous items of elements.
• Queues have two ends:
▫ Elements are added at one end.
▫ Elements are removed from the other end.
• The element added first is also removed first (FIFO: First In, First Out).
Types of Queue
• Queue is an abstract data type which can be implemented
as a linear or circular list. It has a front and rear.
Types of Queue:
1. Simple Queue
2. Circular Queue
3. Dequeue (Double Ended Queue)
Operations on Queue
• Mainly the following four basic operations are performed
on queue:
• Enqueue: Adds an item to the queue. If the queue is full,
then it is said to be an Overflow condition.
• 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.
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
Queue Specification
• Definitions: (provided by the user)
▫ MAX_ITEMS: Max number of items that might be on
the queue
▫ ItemType: Data type of the items on the queue
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType)& item)
Real World Example
Queue Representation
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the queue.
• Preconditions: Queue has been initialized and is
not full.
• Postconditions: newItem is at rear of queue.
Enqueue Algorithm
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error
and exit.
• Step 3 − If the queue is not full, increment rear
pointer to point the next empty space.
• Step 4 − Add data element to the queue location,
where the rear is pointing.
• Step 5 − return success.
Enqueue Representation
Dequeue (ItemType& item)
• Function: Removes front item from queue and
returns it in item.
• Preconditions: Queue has been initialized and is not
empty.
• Postconditions: Front element has been removed
from queue and item is a copy of removed element.
Dequeue Algorithm
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow
error and exit.
• Step 3 − If the queue is not empty, access the data
where front is pointing.
• Step 4 − Increment front pointer to point to the next
available data element.
• Step 5 − Return success.
Dequeue Representation
Implementation issues
• Implement the queue as a circular structure.
• How do we know if a queue is full or empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Make front point to the element preceding the front element in the
queue (one memory location will be wasted).
Initialize front and rear
Queue is empty
now!!
rear == front
Queue overflow
• The condition resulting from trying to add an
element onto a full queue.
if(!q.IsFull())
q.Enqueue(item);
Queue underflow
• The condition resulting from trying to remove an
element from an empty queue.
if(!q.IsEmpty())
q.Dequeue(item);
Circular Queue
private:
int front;
int rear;
ItemType* items;
int maxQue;
};
Circular Queue is a linear data structure in which the
operations are performed based on FIFO (First In First Out)
principle and the last position is connected back to the first
position to make a circle. It is also called ‘Ring Buffer’.
Operations on Circular Queue
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
• enQueue(value) This function is used to insert an
element into the circular queue. In a circular queue,
the new element is always inserted at Rear position.
▫ Steps: Check whether queue is Full – Check ((rear ==
SIZE-1 && front == 0) || (rear == front-1)).
▫ If it is full then display Queue is full. If queue is not full
then, check if (rear == SIZE – 1 && front != 0) if it is
true then set rear=0 and insert element.
Operations on Circular Queue
• deQueue() This function is used to delete an
element from the circular queue. In a circular queue,
the element is always deleted from front position.
▫ Steps: Check whether queue is Empty means check
(front==-1).
▫ If it is empty then display Queue is empty.
▫ If queue is not empty then step 3
▫ Check if (front==rear) if it is true then set front=rear=-1
▫ else check if (front==size-1), if it is true then set
front=0 and return the element.
Circular Queue - Implementation
1. Initialize the queue, with size of the queue defined
(maxSize), and head and tail pointers.
2. enqueue: Check if the number of elements is equal to
maxSize - 1:
a) If Yes, then return Queue is full.
b) If No, then add the new data element to the location of
tail pointer and increment the tail pointer.
3. dequeue: Check if the number of elements in the
queue is zero:
a) If Yes, then return Queue is empty.
b) If No, then increment the head pointer.
4. Finding the size:
a) If, tail >= head, size = (tail - head) + 1
b) But if, head > tail, then size = maxSize - (head - tail) + 1
Performance
• Time Complexity: Time complexity of all operations
like enqueue(), dequeue(), isFull(), isEmpty(), front()
and rear() is O(1). There is no loop in any of the
operations.
•
Example: Recognizing Palindromes
• A palindrome is a string that reads the same
forward and backward.
Able was I ere I saw Elba
• We will read the line of text into both a stack
and a queue.
• Compare the contents of the stack and the
queue character-by-character to see if they
would produce the same string of characters.
Example: Recognizing Palindromes
Example: Recognizing Palindromes
#include <iostream.h>
#include <ctype.h>
#include "stack.h"
#include "queue.h“
int main()
{
StackType<char> s;
QueType<char> q;
char ch;
char sItem, qItem;
int mismatches = 0;
cout << "Enter string: " << endl;
while(cin.peek() != 'n') {
cin >> ch;
if(isalpha(ch)) {
if(!s.IsFull())
s.Push(toupper(ch));
if(!q.IsFull())
q.Enqueue(toupper(ch));
}
}
while( (!q.IsEmpty()) && (!s.IsEmpty()) ) {
s.Pop(sItem);
q.Dequeue(qItem);
if(sItem != qItem)
++mismatches;
}
if (mismatches == 0)
cout << "That is a palindrome" << endl;
else
cout << That is not a palindrome" << endl;
return 0;
}
Example: Recognizing Palindromes
Summary
• Introduction to Queue Data Structure
• Types of Queue Data Structures
• Circular QUEUE and its Operations
• Double Ended QUEUE and its operations
References
• https://www.geeksforgeeks.org/queue-data-
structure/
• https://www.cse.unr.edu/~bebis/CS308/Powe
rPoint/Queues.ppt
• https://www.tutorialspoint.com/data_structure
s_algorithms/dsa_queue.htm
• https://www.tutorialride.com/data-
structures/types-of-queue-in-data-structure.htm

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Queue
QueueQueue
Queue
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Race around and master slave flip flop
Race around and master slave flip flopRace around and master slave flip flop
Race around and master slave flip flop
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Polyphase
PolyphasePolyphase
Polyphase
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Frames
FramesFrames
Frames
 
Arithmetic and logic unit
Arithmetic and logic unitArithmetic and logic unit
Arithmetic and logic unit
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
Signals & systems
Signals & systems Signals & systems
Signals & systems
 
TIME RESPONSE ANALYSIS
TIME RESPONSE ANALYSISTIME RESPONSE ANALYSIS
TIME RESPONSE ANALYSIS
 
Heaps
HeapsHeaps
Heaps
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues ii
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 

Ähnlich wie Queue Data Structure (20)

queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Queue
QueueQueue
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
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Queue
QueueQueue
Queue
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Queues
Queues Queues
Queues
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 

Mehr von Afaq Mansoor Khan

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingAfaq Mansoor Khan
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in PakistanAfaq Mansoor Khan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAfaq Mansoor Khan
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An OverviewAfaq Mansoor Khan
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design DecisionsAfaq Mansoor Khan
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinAfaq Mansoor Khan
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - AsteroidsAfaq Mansoor Khan
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting AlgorithmsAfaq Mansoor Khan
 

Mehr von Afaq Mansoor Khan (20)

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
 
Quick sort
Quick sortQuick sort
Quick sort
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 

Kürzlich hochgeladen

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 

Kürzlich hochgeladen (20)

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 

Queue Data Structure

  • 1. Queue Data Structure Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Notations • Prefix, Infix and Postfix Notations • Conversion of one type expression to another • Evaluation of Prefix and Postfix Notations
  • 3. Objectives Overview • Introduction to Queue Data Structure • Types of Queue Data Structures • Circular QUEUE and its Operations • Double Ended QUEUE and its operations
  • 4. What is a queue? • It is an ordered group of homogeneous items of elements. • Queues have two ends: ▫ Elements are added at one end. ▫ Elements are removed from the other end. • The element added first is also removed first (FIFO: First In, First Out).
  • 5. Types of Queue • Queue is an abstract data type which can be implemented as a linear or circular list. It has a front and rear. Types of Queue: 1. Simple Queue 2. Circular Queue 3. Dequeue (Double Ended Queue)
  • 6. Operations on Queue • Mainly the following four basic operations are performed on queue: • Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition. • 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. • Front: Get the front item from queue. • Rear: Get the last item from queue.
  • 7. Queue Specification • Definitions: (provided by the user) ▫ MAX_ITEMS: Max number of items that might be on the queue ▫ ItemType: Data type of the items on the queue • Operations – MakeEmpty – Boolean IsEmpty – Boolean IsFull – Enqueue (ItemType newItem) – Dequeue (ItemType)& item)
  • 8.
  • 11. Enqueue (ItemType newItem) • Function: Adds newItem to the rear of the queue. • Preconditions: Queue has been initialized and is not full. • Postconditions: newItem is at rear of queue.
  • 12. Enqueue Algorithm • Step 1 − Check if the queue is full. • Step 2 − If the queue is full, produce overflow error and exit. • Step 3 − If the queue is not full, increment rear pointer to point the next empty space. • Step 4 − Add data element to the queue location, where the rear is pointing. • Step 5 − return success.
  • 14. Dequeue (ItemType& item) • Function: Removes front item from queue and returns it in item. • Preconditions: Queue has been initialized and is not empty. • Postconditions: Front element has been removed from queue and item is a copy of removed element.
  • 15. Dequeue Algorithm • Step 1 − Check if the queue is empty. • Step 2 − If the queue is empty, produce underflow error and exit. • Step 3 − If the queue is not empty, access the data where front is pointing. • Step 4 − Increment front pointer to point to the next available data element. • Step 5 − Return success.
  • 17. Implementation issues • Implement the queue as a circular structure. • How do we know if a queue is full or empty? • Initialization of front and rear. • Testing for a full or empty queue.
  • 18.
  • 19.
  • 20. Make front point to the element preceding the front element in the queue (one memory location will be wasted).
  • 23. Queue overflow • The condition resulting from trying to add an element onto a full queue. if(!q.IsFull()) q.Enqueue(item);
  • 24. Queue underflow • The condition resulting from trying to remove an element from an empty queue. if(!q.IsEmpty()) q.Dequeue(item);
  • 25. Circular Queue private: int front; int rear; ItemType* items; int maxQue; }; Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’.
  • 26. Operations on Circular Queue • Front: Get the front item from queue. • Rear: Get the last item from queue. • enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. ▫ Steps: Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)). ▫ If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
  • 27. Operations on Circular Queue • deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. ▫ Steps: Check whether queue is Empty means check (front==-1). ▫ If it is empty then display Queue is empty. ▫ If queue is not empty then step 3 ▫ Check if (front==rear) if it is true then set front=rear=-1 ▫ else check if (front==size-1), if it is true then set front=0 and return the element.
  • 28.
  • 29. Circular Queue - Implementation 1. Initialize the queue, with size of the queue defined (maxSize), and head and tail pointers. 2. enqueue: Check if the number of elements is equal to maxSize - 1: a) If Yes, then return Queue is full. b) If No, then add the new data element to the location of tail pointer and increment the tail pointer. 3. dequeue: Check if the number of elements in the queue is zero: a) If Yes, then return Queue is empty. b) If No, then increment the head pointer. 4. Finding the size: a) If, tail >= head, size = (tail - head) + 1 b) But if, head > tail, then size = maxSize - (head - tail) + 1
  • 30. Performance • Time Complexity: Time complexity of all operations like enqueue(), dequeue(), isFull(), isEmpty(), front() and rear() is O(1). There is no loop in any of the operations. •
  • 31. Example: Recognizing Palindromes • A palindrome is a string that reads the same forward and backward. Able was I ere I saw Elba • We will read the line of text into both a stack and a queue. • Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.
  • 33. Example: Recognizing Palindromes #include <iostream.h> #include <ctype.h> #include "stack.h" #include "queue.h“ int main() { StackType<char> s; QueType<char> q; char ch; char sItem, qItem; int mismatches = 0; cout << "Enter string: " << endl; while(cin.peek() != 'n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); } }
  • 34. while( (!q.IsEmpty()) && (!s.IsEmpty()) ) { s.Pop(sItem); q.Dequeue(qItem); if(sItem != qItem) ++mismatches; } if (mismatches == 0) cout << "That is a palindrome" << endl; else cout << That is not a palindrome" << endl; return 0; } Example: Recognizing Palindromes
  • 35. Summary • Introduction to Queue Data Structure • Types of Queue Data Structures • Circular QUEUE and its Operations • Double Ended QUEUE and its operations
  • 36. References • https://www.geeksforgeeks.org/queue-data- structure/ • https://www.cse.unr.edu/~bebis/CS308/Powe rPoint/Queues.ppt • https://www.tutorialspoint.com/data_structure s_algorithms/dsa_queue.htm • https://www.tutorialride.com/data- structures/types-of-queue-in-data-structure.htm