SlideShare a Scribd company logo
1 of 28
Queues
CS 308 – Data Structures
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).
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)
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.
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.
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 Implementation
template<class ItemType>
class QueueType {
public:
QueueType(int);
QueueType();
~QueueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
private:
int front;
int rear;
ItemType* items;
int maxQue;
};
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int
max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::
MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}
Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
item = items[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);
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
Case Study: Simulation
• Queuing System: consists of servers and
queues of objects to be served.
• Simulation: a program that determines how
long items must wait in line before being
served.
Case Study: Simulation (cont.)
• Inputs to the simulation:
(1) the length of the simulation
(2) the average transaction time
(3) the number of servers
(4) the average time between job arrivals
Case Study: Simulation (cont.)
• Parameters the simulation must vary:
(1) number of servers
(2) time between arrivals of items
• Output of simulation: average wait time.
Exercises (Chapt 4)
• 26, 29-34, 39-41, 46, 47.

More Related Content

Similar to QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt

Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
ASU Online
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1
ASU Online
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 

Similar to QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt (20)

@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1
 
stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
 
2.1 Queue.pptx
2.1 Queue.pptx2.1 Queue.pptx
2.1 Queue.pptx
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdfCEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Team 5
Team 5Team 5
Team 5
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 

Recently uploaded

School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
Kamal Acharya
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
MohammadAliNayeem
 

Recently uploaded (20)

School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
 
E-Commerce Shopping using MERN Stack where different modules are present
E-Commerce Shopping using MERN Stack where different modules are presentE-Commerce Shopping using MERN Stack where different modules are present
E-Commerce Shopping using MERN Stack where different modules are present
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdf
 
solid state electronics ktu module 5 slides
solid state electronics ktu module 5 slidessolid state electronics ktu module 5 slides
solid state electronics ktu module 5 slides
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
 
ANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdfANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdf
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 

QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt

  • 1. Queues CS 308 – Data Structures
  • 2. 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).
  • 3. 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)
  • 4. 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.
  • 5. 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.
  • 6. 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.
  • 7.
  • 8.
  • 9. Make front point to the element preceding the front element in the queue (one memory location will be wasted).
  • 11. Queue is empty now!! rear == front
  • 12. Queue Implementation template<class ItemType> class QueueType { public: QueueType(int); QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: int front; int rear; ItemType* items; int maxQue; };
  • 13. Queue Implementation (cont.) template<class ItemType> QueueType<ItemType>::QueueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; }
  • 14. Queue Implementation (cont.) template<class ItemType> QueueType<ItemType>::~QueueType() { delete [] items; }
  • 15. Queue Implementation (cont.) template<class ItemType> void QueueType<ItemType>:: MakeEmpty() { front = maxQue - 1; rear = maxQue - 1; }
  • 16. Queue Implementation (cont.) template<class ItemType> bool QueueType<ItemType>::IsEmpty() const { return (rear == front); } template<class ItemType> bool QueueType<ItemType>::IsFull() const { return ( (rear + 1) % maxQue == front); }
  • 17. Queue Implementation (cont.) template<class ItemType> void QueueType<ItemType>::Enqueue (ItemType newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; }
  • 18. Queue Implementation (cont.) template<class ItemType> void QueueType<ItemType>::Dequeue (ItemType& item) { front = (front + 1) % maxQue; item = items[front]; }
  • 19. Queue overflow • The condition resulting from trying to add an element onto a full queue. if(!q.IsFull()) q.Enqueue(item);
  • 20. Queue underflow • The condition resulting from trying to remove an element from an empty queue. if(!q.IsEmpty()) q.Dequeue(item);
  • 21. 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.
  • 23. 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)); } }
  • 24. 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
  • 25. Case Study: Simulation • Queuing System: consists of servers and queues of objects to be served. • Simulation: a program that determines how long items must wait in line before being served.
  • 26. Case Study: Simulation (cont.) • Inputs to the simulation: (1) the length of the simulation (2) the average transaction time (3) the number of servers (4) the average time between job arrivals
  • 27. Case Study: Simulation (cont.) • Parameters the simulation must vary: (1) number of servers (2) time between arrivals of items • Output of simulation: average wait time.
  • 28. Exercises (Chapt 4) • 26, 29-34, 39-41, 46, 47.