SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
lilyMalar1
 

Tendances (20)

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
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
 
Stacks
StacksStacks
Stacks
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Performance analysis and randamized agoritham
Performance analysis and randamized agorithamPerformance analysis and randamized agoritham
Performance analysis and randamized agoritham
 

Similaire à Queue Data Structure

queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.pptQueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
GanaviBY
 

Similaire à 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
 

Plus de Afaq Mansoor Khan

Plus de 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
 

Dernier

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Dernier (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

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