SlideShare a Scribd company logo
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

More Related Content

What's hot

Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
Adam Mukharil Bachtiar
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Prakash Zodge
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
Bilal Amjad
 
Stack
StackStack
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
Pdr Patnaik
 
Stack
StackStack
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Introduction to ibm pc assembly language
Introduction to ibm pc assembly languageIntroduction to ibm pc assembly language
Introduction to ibm pc assembly language
warda aziz
 

What's hot (20)

Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
stack & queue
stack & queuestack & queue
stack & queue
 
Linked list
Linked listLinked list
Linked list
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
 
Stack
StackStack
Stack
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Vi editor
Vi   editorVi   editor
Vi editor
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
 
Stack
StackStack
Stack
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Introduction to ibm pc assembly language
Introduction to ibm pc assembly languageIntroduction to ibm pc assembly language
Introduction to ibm pc assembly language
 

Similar to Queue Data Structure

queue.pptx
queue.pptxqueue.pptx
queue.pptx
Dr.Shweta
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
soniya555961
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
Ddushb
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
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)
Self-Employed
 
Queue
QueueQueue
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
Queue
QueueQueue
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
MattFlordeliza1
 
Queues
Queues Queues
Queues
nidhisatija1
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
GanaviBY
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
MeghadriGhosh4
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
VeerannaKotagi1
 

Similar to 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
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 

More from Afaq Mansoor Khan

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

More from 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
 

Recently uploaded

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 

Recently uploaded (20)

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 

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