queues in c++
TRANSCRIPT
QUEUES IN C++
INTRODUCTION TO QUEUEDEFINITION:
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 QUEUEQueue can be implemented in two ways:
As an Array As a Linked List
QUEUE AS AN ARRAYArray 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
0FRONT
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 10front = 0rear = 0
DELETE OPERATIONThe 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
PROGRAM TO ILLUSTRATE OPERATIONS ON QUEUE AS AN INTEGER ARRAY
#include<iostream.h>#include<conio.h>#define size 4class 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. Insert\n2. Delete\n3.
Display\n4.Quit\nEnter Choice(1-3) "; cin>>ch; switch(ch) { case 1: q.insert();break; case 2: q.deletion();break;
case 3. q.display(); } }while(ch!=3); }
PROGRAM TO 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. Insert\n2. Delete\n3.
Display\n4.Quit\nEnter Choice(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 QUEUEA 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 QUEUEThe 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 600 1 23 4 5
2 5FRONTIn this example, queue is
considered as full although there are two empty spaces in the beginning of the queue.
CIRCULAR QUEUE AS AN ARRAYArray 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
REAR0
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 OPERATIONThe 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
PROGRAM TO ILLUSTRATE OPERATIONS ON CIRCULAR QUEUE#include<iostream.h>#include<conio.h>#define size 4class 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. Remove\n3. Display\n 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 LISTLinked 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
PROGRAM TO 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. insert\n
d. Delete\ns. 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'); }
PROGRAM TO 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 queuepublic: 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.
PROGRAM TO 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 “;}