SlideShare a Scribd company logo
2
Most read
10
Most read
23
Most read
QUEUES IN C++
INTRODUCTION TO QUEUE
DEFINITION:
 A stack is a data structure that provides temporary storage of data in such
a way that the element stored first will be retrieved first.
 This method is also called FIFO – First In First Out.
EXAMPLE:
In real life we can think of queue as a queue of vehicles waiting at the petrol
pump, people waiting at the bus stop for the bus etc. The first person to
enter the queue is the first one to leave the queue. Similarly last person to
join the queue is the last person to leave the queue.
OPERATIONS ON QUEUE
 A queue is a linear data structure.
 It is controlled by two operations: insertion and deletion.
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 Insertion operation adds an element to the queue.
 Deletion operation removes an element from the queue.
 There are two variables FRONT and REAR. FRONT points to the
beginning of the filled queue and takes care of deletion operation. REAR
points to the end of the queue and takes care of insertion operation.
 These two operations implement the FIFO method .
IMPLEMENTATION OF QUEUE
Queue can be implemented in two ways:
 As an Array
 As a Linked List
QUEUE AS AN ARRAY
Array implementation of Queue uses
 an array to store data
 an integer type variable usually called the FRONT,
which points to the beginning of the array and an
integer variable REAR, which points to the end of
the filled array.
30
20
10
2
4
3
2
1
0REAR
0
FRONT
INSERTION OPERATION
 Initially when the queue is empty, FRONT and REAR can have any
integer value other than any valid index number of the array. Let
FRONT = -1; REAR=-1;
 The first element in the empty queue goes to the 0th position of the
array and both FRONT and REAR are initialized to value 0.
 After this every insertion operation increase the REAR by 1 and
inserts new data at that particular position.
 As arrays are fixed in length, elements can not be inserted beyond
the maximum size of the array. Pushing data beyond the maximum
size of the queue (i.e. when REAR = MAX SIZE -1) results in “data
overflow”.
INSERTION OPERATION EXPLAINED
insert 10
front = 0
rear = 0
DELETE OPERATION
The delete operation deletes the very first item from the queue.
Any attempt to delete an element from the empty queue (when
FRONT+REAR=-1) results in “data underflow” condition.
Each time the deletion operation is performed, the FRONT
variable is decremented by 1.
When there is only one element in the queue, deletion operation
of that element makes the queue empty and both FRONT and
REAR will be assigned the value -1.
DELETION OPERATION EXPLAINED
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS AN INTEGER ARRAY
#include<iostream.h>
#include<conio.h>
#define size 4
class queue
{
int data[size];
int front,rear;
public:
queue()
{ front=-1; rear=-1; }
void insert();
void deletion();
};
void queue::insert()
{ if(rear==size-1)
{ cout<<"n queue is full";
return;
}
else if(rear == -1)
{ rear++;
front++; }
else
rear++;
cout<<"Enter Data : ";
cin>>data[rear]; }
void queue::deletion()
{
if(front==-1)
{ cout<<"n Queue is empty";
return;
}
cout<<data[front]<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else
front++; }
void display()
{ for (int i=front;i<=rear; i++)
cout<< data[rear]; }
void main()
{
queue q;
int ch;
do
{
cout<<"n1. Insertn2. Deleten3.
Displayn4.QuitnEnterChoice(1-3) ";
cin>>ch;
switch(ch)
{
case 1: q.insert();break;
case 2: q.deletion();break;
case 3. q.display();
}
}while(ch!=3); }
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS AN ARRAY OF OBJECTS
#include<iostream.h>
#include<conio.h>
struct item
{ int ino;
char name[20]; };
class queue
{
item data[4];
int front,rear;
public:
queue()
{ front=-1; rear=-1; }
void insert();
void deletion();
};
void queue::insert()
{ if(rear==size-1)
{ cout<<"n queue is full";
return; }
else if(rear == -1)
{ rear++;
front++; }
else
rear++;
cout<<"Enter Data : ";
cin>>data[rear].ino>>
data[rear].name; }
void queue::deletion()
{
if(front==-1)
{ cout<<"n Queue is empty";
return; }
cout<<data[front].ino<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else
front++; }
void display()
{ for (int i=front;i<=rear; i++)
cout<< data[rear]].ino<<
data[rear].name; }
void main()
{
queue q;
int ch;
do
{
cout<<"n1. Insertn2. Deleten3.
Displayn4.QuitnEnterChoice(1-3) ";
cin>>ch;
switch(ch)
{ case 1: q.insert();break;
case 2: q.deletion();break;
case 3. q.display();
}
}while(ch!=3); }
APPLICATIONS OF QUEUE
A queue is an appropriate data structure on which information is
stored and then retrieved in FIFO order.The applications of queue
are as follows:
Queues find their use in CPU scheduling, printer spooling,
message queuing, computer networks etc.
In time sharing system queues help in scheduling of jobs.
DISADVANTAGE OF NORMAL QUEUE
The queue as an array suffers from one major drawback.
As arrays are fixed in size, elements can not be inserted beyond
the maximum size of the array, even though in reality there
might be empty slots in the beginning of the queue.
REAR
30 40 50 60
0 1 2 3 4 5
2 5FRONT
In this example, queue is considered
as full although there are two empty
spaces in the beginning of the queue.
CIRCULAR QUEUE AS AN ARRAY
Array implementation of Circular Queue uses
 an array to store data
 an integer type variable usually called the FRONT, which
points to the beginning of the filled array and an integer
variable REAR, which points to the end of the filled
array.
NOTE: When the number of additions makes REAR equal to the size
of the array, the next element is inserted in the first slot provided it is
free. Circular queue is full when all the slots of the array are
occupied.
50
40
30
60
2
4
3
2
1
0
REAR
0
FRONT
OPERATIONS ON CIRCULAR QUEUE
 A queue is a linear data structure.
 It is controlled by two operations: insertion and deletion.
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 There are two variables FRONT and REAR. FRONT points to the
beginning of the queue and takes care of deletion operation. REAR
points to the end of the queue and takes care of insertion operation.
 If REAR reaches the end of the queue then the next insertion
operation makes REAR=0 provided FRONT is not 0.
 If the FRONT reaches the end of the queue then the next deletion
operation makes FRONT=0 provided there are more elements in the
queue.
CIRCULAR QUEUE: INSERTION OPERATION
 Initially when the queue is empty, FRONT and REAR can have any
integer value other than any valid index number of the array. Let
FRONT = -1; REAR=-1;
 The first element in the empty queue goes to the 0th position of
the array and both FRONT and REAR are initialized to value 0.
 After this every insertion operation increases the REAR by 1 and
inserts new data at that particular position.
 If REAR reaches the end of the queue then the next insertion
operation makes REAR=0 provided FRONT is not 0.
 The queue is full when REAR=FRONT +1 or (FRONT=0 and
REAR=MAX-1). Insertion at this point results in “data overflow”.
CIRCULAR QUEUE: INSERTION OPERATION EXPLAINED
front=2
CIRCULAR QUEUE:DELETE OPERATION
The delete operation deletes the very first item from the queue.
Any attempt to delete an element from the empty queue(when
FRONT+REAR=-1) results in “data underflow” condition.
Each time the deletion operation is performed, the FRONT
variable is decremented by 1.
When there is only one element in the queue, deletion operation
of that element makes the queue empty and both FRONT and
REAR will be assigned the value -1.
When FRONT reaches the end of the queue then the next
deletion operation makes FRONT = 0.
CIRCULAR QUEUE: DELETION OPERATION EXPLAINED
PROGRAMTO ILLUSTRATE OPERATIONS ON CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#define size 4
class cqueue
{
int data[size];
int front,rear;
public:
cqueue()
{ front=-1;rear=-1; }
void insert();
void remove(); };
void cqueue::insert(int num)
{ if(rear==size-1&&
front==0 || front==rear+1)
{ cout<<"nCircular
queue is full";
return;
}
else if(rear==-1)
{ rear++;
front++; }
else if(rear==size-1)
rear=0;
else
rear++;
cout<<"Enter Data : ";
data[rear]=num;
}
void cqueue::remove()
{
if(front==-1)
{
cout<<"n Circular
Queue is empty";return;
}
cout<<data[front]<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else if(front==size-1)
front=0;
else
front++;
}
void cqueue::disp()
{if(front<rear)
for(int i= front;i<=rear;
i++)
cout<<data[i];
else
{for(int i=front;i<=size-1;
i++)
cout<<data[i];
for(int i= 0;i<=rear; i++)
cout<<data[i]; }}
void main()
{
cqueue cq;
int ch;
do
{
cout<<"n1. Insert n2.
Removen3. Displayn 4.
Quit n Enter Choice(1-4) ";
cin>>ch;
switch(ch)
{ case 1:
cq.insert();break;
case 2:
cq.remove();break;
case 3: cq.disp(); }
}while(ch!=4);}
QUEUE AS A LINKED LIST
Linked list implementation of queue uses:
 A linked list to store data
 A pointer FRONT pointing to the beginning of the queue and a pointer REAR
pointing to the end of the queue.
NOTE: Each node of a queue as a linked list has two parts : data part and link part and
is created with the help of self referential structure.
The data part stores the data and link part stores the address of the next node of the
linked list.
FRONT
NODE
DATA LINK
REAR
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue
{
node *rear,*front;
public:
queue()
{ rear=NULL;front=NULL;}
void qinsert();
void qdelete();
void qdisplay();
};
void queue::qinsert()
{
node *temp;
temp=new node;
cout<<"Data :";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
void queue::qdelete()
{
if(front!=NULL)
{ node *temp=front;
cout<<front->data
<<"deleted n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL; }
else
cout<<“empty Queue “;
}
void queue::qdisplay()
{
node *temp=front;
while(temp!=NULL)
{ cout<<temp->data;
temp=temp->next; } }
void main()
{
queue q1; char ch;
do
{
cout<< "i. insertn
d. Deletens. Display
n q. quit ";
cin>>ch;
switch(ch)
{
case 'i' :
q1.qinsert();break;
case 'd' :
q1.qdelete();break;
case 's' :
q1.qdisplay();
}
}while(ch!='q'); }
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST : Explanation
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue
{
node *rear,*front; // front point to the beginning of the queue and rear points to the end of the queue
public:
queue()
{ rear=NULL;front=NULL;} // Initializes front and rear to NULL
void qinsert();
void qdelete();
void qdisplay();
};
Self Referential Structure:These are special structures which
contains pointers to themselves.
Here, next is a pointer of type node itself.
Self Referential structures are needed to create Linked Lists.
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST : Explanation
void queue::qinsert()
{
node *temp; // pointer of type node
temp=new node; // new operator will create a new
//node and address of new node
// is stored in temp
cout<<"Data :";
cin>>temp->data; // adding data in the node
temp->next=NULL; // because new node is always
// added at the end
if(rear==NULL) // if queue is empty, new node
{ rear=temp; // becomes the first node and
front=temp; // both front and rear will point to
} //it
else
{ rear->next=temp; // If queue is not empty ,last
rear=temp; // node will contain address
} // of new node and rear will
} // point to new node
void queue::qdelete()
{
if(front!=NULL) // if queue is not empty
{ node *temp=front; // temp is a pointer
//containing address of
// first node
cout<<front->data <<"deleted n";
front=front->next; // front will now contain
// address of second node
delete temp; // delete operator will delete the node
// stored at temp
if(front==NULL) /*if front become NULL after
deletion, it means that there was only one node
in the queue and deletion of that node will
queue empty */
rear=NULL; }
else
cout<<“empty Queue “;}

More Related Content

PPSX
Data Structure (Queue)
PPTX
Queue - Data Structure - Notes
PPTX
Stacks in c++
PPTX
Queue and its operations
PPT
stack and queue array implementation in java.
PPT
Queue AS an ADT (Abstract Data Type)
PPTX
PPTX
The Stack And Recursion
Data Structure (Queue)
Queue - Data Structure - Notes
Stacks in c++
Queue and its operations
stack and queue array implementation in java.
Queue AS an ADT (Abstract Data Type)
The Stack And Recursion

What's hot (20)

PPTX
stack & queue
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PPTX
Stack and Queue
PPTX
STACKS IN DATASTRUCTURE
PPTX
linked list
PDF
PPTX
single linked list
PPTX
Data structure & its types
PPTX
Queue in Data Structure
PPT
Searching in c language
PPTX
Stacks IN DATA STRUCTURES
PPT
Queue Data Structure
PPT
SEARCHING AND SORTING ALGORITHMS
PPSX
Data structure stack&queue basics
PDF
Python list
PPTX
Stack_Data_Structure.pptx
PPTX
Binary Search Tree
PPTX
Linked List
PDF
sparse matrix in data structure
stack & queue
Conversion of Infix to Prefix and Postfix with Stack
Stack and Queue
STACKS IN DATASTRUCTURE
linked list
single linked list
Data structure & its types
Queue in Data Structure
Searching in c language
Stacks IN DATA STRUCTURES
Queue Data Structure
SEARCHING AND SORTING ALGORITHMS
Data structure stack&queue basics
Python list
Stack_Data_Structure.pptx
Binary Search Tree
Linked List
sparse matrix in data structure
Ad

Viewers also liked (20)

PPT
Queue data structure
PDF
PPTX
Presentation on queue
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PPT
Queue Data Structure
PPT
Notes DATA STRUCTURE - queue
PDF
Queues
PPT
358 33 powerpoint-slides_9-stacks-queues_chapter-9
PPTX
Stack and queue
PPT
Queue in Data Structure
PPTX
Data Structure -List Stack Queue
PPTX
queue & its applications
PPTX
Queue Data Structure (w/ php egs)
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
PPTX
Ppt presentation of queues
PDF
Queue as data_structure
PPTX
Data structures and algorithms
PPTX
Vector class in C++
PPT
Music magazine analysis
PDF
Double google-shopping-sales-in-1-hour
Queue data structure
Presentation on queue
5. Queue - Data Structures using C++ by Varsha Patil
Queue Data Structure
Notes DATA STRUCTURE - queue
Queues
358 33 powerpoint-slides_9-stacks-queues_chapter-9
Stack and queue
Queue in Data Structure
Data Structure -List Stack Queue
queue & its applications
Queue Data Structure (w/ php egs)
6. Linked list - Data Structures using C++ by Varsha Patil
Ppt presentation of queues
Queue as data_structure
Data structures and algorithms
Vector class in C++
Music magazine analysis
Double google-shopping-sales-in-1-hour
Ad

Similar to Queues in C++ (20)

PPT
Queues & ITS TYPES
PPTX
Unit 4 queue
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
PPT
Lec-07 Queues.ppt queues introduction to queue
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPSX
Queue by rajanikanth
PPTX
My lectures circular queue
PPTX
Queues_0748555555555555555555555526.pptx
PPT
queue (1).ppt queue notes and ppt in Data Structures
PPTX
Queue(lecture8).pptx
PPTX
PPTX
The presention is about the queue data structure
PPTX
PPTX
queue.pptx
PDF
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPT
Lecture 2d queues
PDF
05 queues
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
Queues & ITS TYPES
Unit 4 queue
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
Lec-07 Queues.ppt queues introduction to queue
Polynomialmotilalanehrunationalinstitute.pdf
Queue by rajanikanth
My lectures circular queue
Queues_0748555555555555555555555526.pptx
queue (1).ppt queue notes and ppt in Data Structures
Queue(lecture8).pptx
The presention is about the queue data structure
queue.pptx
Bca ii dfs u-2 linklist,stack,queue
Lecture 2d queues
05 queues
Bsc cs ii dfs u-2 linklist,stack,queue

More from Vineeta Garg (9)

PPTX
Pointers in c++
PPTX
Classes and objects1
PPTX
GUI programming
PPTX
Constructors and destructors
PPTX
Structured query language functions
PPTX
Structured query language constraints
PPTX
PPTX
Inheritance in c++
PPTX
Data file handling in c++
Pointers in c++
Classes and objects1
GUI programming
Constructors and destructors
Structured query language functions
Structured query language constraints
Inheritance in c++
Data file handling in c++

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Institutional Correction lecture only . . .
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Institutional Correction lecture only . . .
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
102 student loan defaulters named and shamed – Is someone you know on the list?
PPH.pptx obstetrics and gynecology in nursing
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

Queues in C++

  • 2. INTRODUCTION TO QUEUE DEFINITION:  A stack is a data structure that provides temporary storage of data in such a way that the element stored first will be retrieved first.  This method is also called FIFO – First In First Out. EXAMPLE: In real life we can think of queue as a queue of vehicles waiting at the petrol pump, people waiting at the bus stop for the bus etc. The first person to enter the queue is the first one to leave the queue. Similarly last person to join the queue is the last person to leave the queue.
  • 3. OPERATIONS ON QUEUE  A queue is a linear data structure.  It is controlled by two operations: insertion and deletion.  Insertion operation takes place from the rear end of the queue and deletion operation takes place from the front end of the queue.  Insertion operation adds an element to the queue.  Deletion operation removes an element from the queue.  There are two variables FRONT and REAR. FRONT points to the beginning of the filled queue and takes care of deletion operation. REAR points to the end of the queue and takes care of insertion operation.  These two operations implement the FIFO method .
  • 4. IMPLEMENTATION OF QUEUE Queue can be implemented in two ways:  As an Array  As a Linked List
  • 5. QUEUE AS AN ARRAY Array implementation of Queue uses  an array to store data  an integer type variable usually called the FRONT, which points to the beginning of the array and an integer variable REAR, which points to the end of the filled array. 30 20 10 2 4 3 2 1 0REAR 0 FRONT
  • 6. INSERTION OPERATION  Initially when the queue is empty, FRONT and REAR can have any integer value other than any valid index number of the array. Let FRONT = -1; REAR=-1;  The first element in the empty queue goes to the 0th position of the array and both FRONT and REAR are initialized to value 0.  After this every insertion operation increase the REAR by 1 and inserts new data at that particular position.  As arrays are fixed in length, elements can not be inserted beyond the maximum size of the array. Pushing data beyond the maximum size of the queue (i.e. when REAR = MAX SIZE -1) results in “data overflow”.
  • 7. INSERTION OPERATION EXPLAINED insert 10 front = 0 rear = 0
  • 8. DELETE OPERATION The delete operation deletes the very first item from the queue. Any attempt to delete an element from the empty queue (when FRONT+REAR=-1) results in “data underflow” condition. Each time the deletion operation is performed, the FRONT variable is decremented by 1. When there is only one element in the queue, deletion operation of that element makes the queue empty and both FRONT and REAR will be assigned the value -1.
  • 10. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS AN INTEGER ARRAY #include<iostream.h> #include<conio.h> #define size 4 class queue { int data[size]; int front,rear; public: queue() { front=-1; rear=-1; } void insert(); void deletion(); }; void queue::insert() { if(rear==size-1) { cout<<"n queue is full"; return; } else if(rear == -1) { rear++; front++; } else rear++; cout<<"Enter Data : "; cin>>data[rear]; } void queue::deletion() { if(front==-1) { cout<<"n Queue is empty"; return; } cout<<data[front]<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else front++; } void display() { for (int i=front;i<=rear; i++) cout<< data[rear]; } void main() { queue q; int ch; do { cout<<"n1. Insertn2. Deleten3. Displayn4.QuitnEnterChoice(1-3) "; cin>>ch; switch(ch) { case 1: q.insert();break; case 2: q.deletion();break; case 3. q.display(); } }while(ch!=3); }
  • 11. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS AN ARRAY OF OBJECTS #include<iostream.h> #include<conio.h> struct item { int ino; char name[20]; }; class queue { item data[4]; int front,rear; public: queue() { front=-1; rear=-1; } void insert(); void deletion(); }; void queue::insert() { if(rear==size-1) { cout<<"n queue is full"; return; } else if(rear == -1) { rear++; front++; } else rear++; cout<<"Enter Data : "; cin>>data[rear].ino>> data[rear].name; } void queue::deletion() { if(front==-1) { cout<<"n Queue is empty"; return; } cout<<data[front].ino<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else front++; } void display() { for (int i=front;i<=rear; i++) cout<< data[rear]].ino<< data[rear].name; } void main() { queue q; int ch; do { cout<<"n1. Insertn2. Deleten3. Displayn4.QuitnEnterChoice(1-3) "; cin>>ch; switch(ch) { case 1: q.insert();break; case 2: q.deletion();break; case 3. q.display(); } }while(ch!=3); }
  • 12. APPLICATIONS OF QUEUE A queue is an appropriate data structure on which information is stored and then retrieved in FIFO order.The applications of queue are as follows: Queues find their use in CPU scheduling, printer spooling, message queuing, computer networks etc. In time sharing system queues help in scheduling of jobs.
  • 13. DISADVANTAGE OF NORMAL QUEUE The queue as an array suffers from one major drawback. As arrays are fixed in size, elements can not be inserted beyond the maximum size of the array, even though in reality there might be empty slots in the beginning of the queue. REAR 30 40 50 60 0 1 2 3 4 5 2 5FRONT In this example, queue is considered as full although there are two empty spaces in the beginning of the queue.
  • 14. CIRCULAR QUEUE AS AN ARRAY Array implementation of Circular Queue uses  an array to store data  an integer type variable usually called the FRONT, which points to the beginning of the filled array and an integer variable REAR, which points to the end of the filled array. NOTE: When the number of additions makes REAR equal to the size of the array, the next element is inserted in the first slot provided it is free. Circular queue is full when all the slots of the array are occupied. 50 40 30 60 2 4 3 2 1 0 REAR 0 FRONT
  • 15. OPERATIONS ON CIRCULAR QUEUE  A queue is a linear data structure.  It is controlled by two operations: insertion and deletion.  Insertion operation takes place from the rear end of the queue and deletion operation takes place from the front end of the queue.  There are two variables FRONT and REAR. FRONT points to the beginning of the queue and takes care of deletion operation. REAR points to the end of the queue and takes care of insertion operation.  If REAR reaches the end of the queue then the next insertion operation makes REAR=0 provided FRONT is not 0.  If the FRONT reaches the end of the queue then the next deletion operation makes FRONT=0 provided there are more elements in the queue.
  • 16. CIRCULAR QUEUE: INSERTION OPERATION  Initially when the queue is empty, FRONT and REAR can have any integer value other than any valid index number of the array. Let FRONT = -1; REAR=-1;  The first element in the empty queue goes to the 0th position of the array and both FRONT and REAR are initialized to value 0.  After this every insertion operation increases the REAR by 1 and inserts new data at that particular position.  If REAR reaches the end of the queue then the next insertion operation makes REAR=0 provided FRONT is not 0.  The queue is full when REAR=FRONT +1 or (FRONT=0 and REAR=MAX-1). Insertion at this point results in “data overflow”.
  • 17. CIRCULAR QUEUE: INSERTION OPERATION EXPLAINED front=2
  • 18. CIRCULAR QUEUE:DELETE OPERATION The delete operation deletes the very first item from the queue. Any attempt to delete an element from the empty queue(when FRONT+REAR=-1) results in “data underflow” condition. Each time the deletion operation is performed, the FRONT variable is decremented by 1. When there is only one element in the queue, deletion operation of that element makes the queue empty and both FRONT and REAR will be assigned the value -1. When FRONT reaches the end of the queue then the next deletion operation makes FRONT = 0.
  • 19. CIRCULAR QUEUE: DELETION OPERATION EXPLAINED
  • 20. PROGRAMTO ILLUSTRATE OPERATIONS ON CIRCULAR QUEUE #include<iostream.h> #include<conio.h> #define size 4 class cqueue { int data[size]; int front,rear; public: cqueue() { front=-1;rear=-1; } void insert(); void remove(); }; void cqueue::insert(int num) { if(rear==size-1&& front==0 || front==rear+1) { cout<<"nCircular queue is full"; return; } else if(rear==-1) { rear++; front++; } else if(rear==size-1) rear=0; else rear++; cout<<"Enter Data : "; data[rear]=num; } void cqueue::remove() { if(front==-1) { cout<<"n Circular Queue is empty";return; } cout<<data[front]<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else if(front==size-1) front=0; else front++; } void cqueue::disp() {if(front<rear) for(int i= front;i<=rear; i++) cout<<data[i]; else {for(int i=front;i<=size-1; i++) cout<<data[i]; for(int i= 0;i<=rear; i++) cout<<data[i]; }} void main() { cqueue cq; int ch; do { cout<<"n1. Insert n2. Removen3. Displayn 4. Quit n Enter Choice(1-4) "; cin>>ch; switch(ch) { case 1: cq.insert();break; case 2: cq.remove();break; case 3: cq.disp(); } }while(ch!=4);}
  • 21. QUEUE AS A LINKED LIST Linked list implementation of queue uses:  A linked list to store data  A pointer FRONT pointing to the beginning of the queue and a pointer REAR pointing to the end of the queue. NOTE: Each node of a queue as a linked list has two parts : data part and link part and is created with the help of self referential structure. The data part stores the data and link part stores the address of the next node of the linked list. FRONT NODE DATA LINK REAR
  • 22. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST #include<iostream.h> #include<conio.h> struct node { int data; node *next; }; class queue { node *rear,*front; public: queue() { rear=NULL;front=NULL;} void qinsert(); void qdelete(); void qdisplay(); }; void queue::qinsert() { node *temp; temp=new node; cout<<"Data :"; cin>>temp->data; temp->next=NULL; if(rear==NULL) { rear=temp; front=temp; } else { rear->next=temp; rear=temp; } } void queue::qdelete() { if(front!=NULL) { node *temp=front; cout<<front->data <<"deleted n"; front=front->next; delete temp; if(front==NULL) rear=NULL; } else cout<<“empty Queue “; } void queue::qdisplay() { node *temp=front; while(temp!=NULL) { cout<<temp->data; temp=temp->next; } } void main() { queue q1; char ch; do { cout<< "i. insertn d. Deletens. Display n q. quit "; cin>>ch; switch(ch) { case 'i' : q1.qinsert();break; case 'd' : q1.qdelete();break; case 's' : q1.qdisplay(); } }while(ch!='q'); }
  • 23. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST : Explanation #include<iostream.h> #include<conio.h> struct node { int data; node *next; }; class queue { node *rear,*front; // front point to the beginning of the queue and rear points to the end of the queue public: queue() { rear=NULL;front=NULL;} // Initializes front and rear to NULL void qinsert(); void qdelete(); void qdisplay(); }; Self Referential Structure:These are special structures which contains pointers to themselves. Here, next is a pointer of type node itself. Self Referential structures are needed to create Linked Lists.
  • 24. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST : Explanation void queue::qinsert() { node *temp; // pointer of type node temp=new node; // new operator will create a new //node and address of new node // is stored in temp cout<<"Data :"; cin>>temp->data; // adding data in the node temp->next=NULL; // because new node is always // added at the end if(rear==NULL) // if queue is empty, new node { rear=temp; // becomes the first node and front=temp; // both front and rear will point to } //it else { rear->next=temp; // If queue is not empty ,last rear=temp; // node will contain address } // of new node and rear will } // point to new node void queue::qdelete() { if(front!=NULL) // if queue is not empty { node *temp=front; // temp is a pointer //containing address of // first node cout<<front->data <<"deleted n"; front=front->next; // front will now contain // address of second node delete temp; // delete operator will delete the node // stored at temp if(front==NULL) /*if front become NULL after deletion, it means that there was only one node in the queue and deletion of that node will queue empty */ rear=NULL; } else cout<<“empty Queue “;}