SlideShare a Scribd company logo
Queues
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
Introduction to the Queue
• Like a stack, a queue (pronounced "cue") is a data structure that holds
a sequence of elements.
• A queue, however, provides access to its elements in first-in, first-out
(FIFO) order.
• The elements in a queue are processed like customers standing in a
grocery check-out line: the first customer in line is the first one
served.
Example Applications of Queues
• In a multi-user system, a queue is used to hold print jobs submitted
by users , while the printer services those jobs one at a time.
• Communications software also uses queues to hold information
received over networks and dial-up connections. Sometimes
information is transmitted to a system faster than it can be processed,
so it is placed in a queue when it is received.
Static and Dynamic Queues
• Just as stacks are implemented as arrays or linked lists, so are queues.
• Dynamic queues offer the same advantages over static queues that
dynamic stacks offer over static stacks.
Note: We’ll implement Stack and Queues using linked lists after studying Linked List data structures
Queue Operations
• Think of queues as having a front and a rear. This is illustrated in
Figure.
Queue Operations
• The two primary queue operations are
• enqueuing and
• dequeuing.
• To enqueue means to insert an element at te rear of a queue.
• To dequeue means to remove an element from the front of a queue.
Queue Operations
• Suppose we have an empty static integer queue that is capable of
holding a maximum of three values. With that queue we execute the
following enqueue operations.
Enqueue(3);
Enqueue(6);
Enqueue(9);
Queue Operations
• Figure illustrates the state of the queue after
each of the enqueue operations.
Queue Operations
• Now let's see how dequeue operations are
performed.
• Figure illustrates the state of the queue after
each of three consecutive dequeue operations.
• When the last deqeue operation is performed
in the illustration, the queue is empty. An
empty queue can be signified by setting both
front and rear indices to –1.
Contents of IntQueue.h
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
void dequeue(int &);
bool isEmpty(void);
bool isFull(void);
void clear(void);
};
Contents of IntQueue.cpp
#include <iostream.h>
#include "IntQueue.h“
// ******Constructor*******
IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = 0;
rear = 0;
numItems = 0;
}
// *******Destructor*******
IntQueue::~IntQueue(void)
{
delete [] queueArray;
}
Contents of IntQueue.cpp
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout << "The queue is full.n";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
Contents of IntQueue.cpp
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
void IntQueue::dequeue(int &num)
{
if (isEmpty())
cout << "The queue is empty.n";
else
{
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
num = queueArray[front];
// Update item count
numItems--;
}
}
Contents of IntQueue.cpp
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
bool status;
if (numItems)
status = false;
else
status = true;
return status;
}
Contents of IntQueue.cpp
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
bool status;
if (numItems < queueSize)
status = false;
else
status = true;
return status;
}
Contents of IntQueue.cpp
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear(void)
{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}
Program: 1
// This program demonstrates the IntQeue class
#include <iostream.h>
#include "intqueue.h“
void main(void)
{
IntQueue iQueue(5);
cout << "Enqueuing 5 items...n";
// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);
// Attempt to enqueue a 6th item.
cout << "Now attempting to enqueue again...n";
iQueue.enqueue(5);
Program: 1 (continued)
// Deqeue and retrieve all items in the queue
cout << "The values in the queue were:n";
while (!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
}
Program Output
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0

More Related Content

PPTX
U3.stack queue
PDF
Stack and Queue (brief)
PPT
03 stacks and_queues_using_arrays
PPTX
Queue Implementation Using Array & Linked List
PPT
Stack and queue
PPT
Stacks and queue
PPTX
My lecture stack_queue_operation
PPT
Stack queue
U3.stack queue
Stack and Queue (brief)
03 stacks and_queues_using_arrays
Queue Implementation Using Array & Linked List
Stack and queue
Stacks and queue
My lecture stack_queue_operation
Stack queue

What's hot (20)

PPT
Stacks, Queues, Deques
PPTX
Queue Data Structure (w/ php egs)
PPT
Stack Implementation
PPT
Queue Data Structure
PPT
Queue implementation
PPTX
Stack and its operations
PPT
Queue data structure
PPTX
stack & queue
PPT
Queue in Data Structure
PPTX
Stacks in c++
PPSX
Data structure stack&queue basics
PDF
Queues
PPT
PPTX
Stack and Queue
PPTX
Application of Stack - Yadraj Meena
PPT
Stacks and queues
PPTX
My lectures circular queue
PPT
Queue Data Structure
PPT
Stack, queue and hashing
PPT
Stacks, Queues, Deques
Queue Data Structure (w/ php egs)
Stack Implementation
Queue Data Structure
Queue implementation
Stack and its operations
Queue data structure
stack & queue
Queue in Data Structure
Stacks in c++
Data structure stack&queue basics
Queues
Stack and Queue
Application of Stack - Yadraj Meena
Stacks and queues
My lectures circular queue
Queue Data Structure
Stack, queue and hashing
Ad

Viewers also liked (11)

PPT
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
PPT
7 stacksqueues
PPT
16 Linear data structures
PPT
Lec8
PPTX
Linked stacks and queues
PPT
02 Arrays And Memory Mapping
PPT
Introductiont To Aray,Tree,Stack, Queue
PPTX
Data structures
PPTX
queue & its applications
PPTX
Binomial Heaps and Fibonacci Heaps
PDF
Preparation Data Structures 08 queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
7 stacksqueues
16 Linear data structures
Lec8
Linked stacks and queues
02 Arrays And Memory Mapping
Introductiont To Aray,Tree,Stack, Queue
Data structures
queue & its applications
Binomial Heaps and Fibonacci Heaps
Preparation Data Structures 08 queues
Ad

Similar to Data Structures - Lecture 6 [queues] (20)

PPTX
The presention is about the queue data structure
PPTX
Unit 4 queue
PPT
Algo>Queues
PPT
Queue AS an ADT (Abstract Data Type)
PDF
Queue ADT for data structure for computer
PPTX
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PPTX
stacks and queues for public
PPTX
@Chapter 4 DSA Part II.pptx
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
DS10-QUEUE0000000000000000000000000000000000000.pptx
PPTX
Queue(lecture8).pptx
PPTX
Data Structures_Linear Data Structures Queue.pptx
PPTX
Fundamentals of Data Structure and Queues
PDF
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
PPT
The Queue in Data structure and algorithm
PPT
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
PPT
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
PPT
Data structure.ppt
PDF
The presention is about the queue data structure
Unit 4 queue
Algo>Queues
Queue AS an ADT (Abstract Data Type)
Queue ADT for data structure for computer
DS ppt1.pptx.c programing. Engineering. Data structure
stacks and queues for public
@Chapter 4 DSA Part II.pptx
Lec-07 Queues.ppt queues introduction to queue
DS10-QUEUE0000000000000000000000000000000000000.pptx
Queue(lecture8).pptx
Data Structures_Linear Data Structures Queue.pptx
Fundamentals of Data Structure and Queues
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
The Queue in Data structure and algorithm
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
Data structure.ppt

More from Muhammad Hammad Waseem (20)

PDF
[ITP - Lecture 17] Strings in C/C++
PDF
[ITP - Lecture 16] Structures in C/C++
PDF
[ITP - Lecture 15] Arrays & its Types
PDF
[ITP - Lecture 14] Recursion
PDF
[ITP - Lecture 13] Introduction to Pointers
PDF
[ITP - Lecture 12] Functions in C/C++
PDF
[ITP - Lecture 11] Loops in C/C++
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
PDF
[ITP - Lecture 07] Comments in C/C++
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
PDF
[ITP - Lecture 05] Datatypes
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
PDF
[ITP - Lecture 03] Introduction to C/C++
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
PPTX
[OOP - Lec 20,21] Inheritance
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
[OOP - Lec 18] Static Data Member
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 14] Recursion
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 05] Datatypes
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[OOP - Lec 20,21] Inheritance
[OOP - Lec 19] Static Member Functions
[OOP - Lec 18] Static Data Member

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Classroom Observation Tools for Teachers
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPH.pptx obstetrics and gynecology in nursing
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Classroom Observation Tools for Teachers
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
RMMM.pdf make it easy to upload and study

Data Structures - Lecture 6 [queues]

  • 2. Introduction to the Queue • Like a stack, a queue (pronounced "cue") is a data structure that holds a sequence of elements. • A queue, however, provides access to its elements in first-in, first-out (FIFO) order. • The elements in a queue are processed like customers standing in a grocery check-out line: the first customer in line is the first one served.
  • 3. Example Applications of Queues • In a multi-user system, a queue is used to hold print jobs submitted by users , while the printer services those jobs one at a time. • Communications software also uses queues to hold information received over networks and dial-up connections. Sometimes information is transmitted to a system faster than it can be processed, so it is placed in a queue when it is received.
  • 4. Static and Dynamic Queues • Just as stacks are implemented as arrays or linked lists, so are queues. • Dynamic queues offer the same advantages over static queues that dynamic stacks offer over static stacks. Note: We’ll implement Stack and Queues using linked lists after studying Linked List data structures
  • 5. Queue Operations • Think of queues as having a front and a rear. This is illustrated in Figure.
  • 6. Queue Operations • The two primary queue operations are • enqueuing and • dequeuing. • To enqueue means to insert an element at te rear of a queue. • To dequeue means to remove an element from the front of a queue.
  • 7. Queue Operations • Suppose we have an empty static integer queue that is capable of holding a maximum of three values. With that queue we execute the following enqueue operations. Enqueue(3); Enqueue(6); Enqueue(9);
  • 8. Queue Operations • Figure illustrates the state of the queue after each of the enqueue operations.
  • 9. Queue Operations • Now let's see how dequeue operations are performed. • Figure illustrates the state of the queue after each of three consecutive dequeue operations. • When the last deqeue operation is performed in the illustration, the queue is empty. An empty queue can be signified by setting both front and rear indices to –1.
  • 10. Contents of IntQueue.h class IntQueue { private: int *queueArray; int queueSize; int front; int rear; int numItems; public: IntQueue(int); ~IntQueue(void); void enqueue(int); void dequeue(int &); bool isEmpty(void); bool isFull(void); void clear(void); };
  • 11. Contents of IntQueue.cpp #include <iostream.h> #include "IntQueue.h“ // ******Constructor******* IntQueue::IntQueue(int s) { queueArray = new int[s]; queueSize = s; front = 0; rear = 0; numItems = 0; } // *******Destructor******* IntQueue::~IntQueue(void) { delete [] queueArray; }
  • 12. Contents of IntQueue.cpp //******************************************** // Function enqueue inserts the value in num * // at the rear of the queue. * //******************************************** void IntQueue::enqueue(int num) { if (isFull()) cout << "The queue is full.n"; else { // Calculate the new rear position rear = (rear + 1) % queueSize; // Insert new item queueArray[rear] = num; // Update item count numItems++; } }
  • 13. Contents of IntQueue.cpp //********************************************* // Function dequeue removes the value at the * // front of the queue, and copies t into num. * //********************************************* void IntQueue::dequeue(int &num) { if (isEmpty()) cout << "The queue is empty.n"; else { // Move front front = (front + 1) % queueSize; // Retrieve the front item num = queueArray[front]; // Update item count numItems--; } }
  • 14. Contents of IntQueue.cpp //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * //********************************************* bool IntQueue::isEmpty(void) { bool status; if (numItems) status = false; else status = true; return status; }
  • 15. Contents of IntQueue.cpp //******************************************** // Function isFull returns true if the queue * // is full, and false otherwise. * //******************************************** bool IntQueue::isFull(void) { bool status; if (numItems < queueSize) status = false; else status = true; return status; }
  • 16. Contents of IntQueue.cpp //******************************************* // Function clear resets the front and rear * // indices, and sets numItems to 0. * //******************************************* void IntQueue::clear(void) { front = queueSize - 1; rear = queueSize - 1; numItems = 0; }
  • 17. Program: 1 // This program demonstrates the IntQeue class #include <iostream.h> #include "intqueue.h“ void main(void) { IntQueue iQueue(5); cout << "Enqueuing 5 items...n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Attempt to enqueue a 6th item. cout << "Now attempting to enqueue again...n"; iQueue.enqueue(5);
  • 18. Program: 1 (continued) // Deqeue and retrieve all items in the queue cout << "The values in the queue were:n"; while (!iQueue.isEmpty()) { int value; iQueue.dequeue(value); cout << value << endl; } } Program Output Enqueuing 5 items... Now attempting to enqueue again... The queue is full. The values in the queue were: 0