You are on page 1of 13

# QUEUE@ C++ 12

Queue
Queue is a linear data structure in which the insertion and deletion
operations are performed at two different ends. The insertion is
performed at one end and deletion is performed at other end. In a queue
data structure, the insertion operation is performed at a position which is
known as 'rear' and the deletion operation is performed at a position
which is known as 'front'. In queue data structure, the insertion and
deletion operations are performed based on FIFO (First In First
Out) principle.

In a queue data structure, the insertion operation is performed using a
function called "enQueue()" and deletion operation is performed using a
function
called
"deQueue()".
Queue data structure can be defined as follows...
Queue data structure is a linear data structure in which the
operations are performed based on FIFO principle.
A queue can also be defined as
"Queue data structure is a collection of similar data items in
which insertion and deletion operations are performed based on
FIFO principle".
Example
Queue after inserting 25, 30, 51, 60 and 85.

@VKSLearningHub

Page 1

that queue can organize only limited number of elements. Whenever we want to delete a value from the queue.  Using Array  Using Linked List When a queue is implemented using array.. that queue can organize unlimited number of elements. Queue Using Array A queue data structure can be implemented using one dimensional array.. rear=0) 5 Now we decided to add an element 10 in queue| rear<=Max-1 |rear++ ( front=0 . rear=3) 10 @VKSLearningHub 15 Page 2 20 . They are as follows. front=-1 rear=-1 queue is empty Max=4 Now we decided to add an element 5 in queue| rear=front=-1 so ( front=0 .. rear=1) 5 10 Now we decided to add an element 15 in queue | rear<=Max-1 | rear++ ( front=0 . increment 'rear' value by one and then insert at that position..QUEUE@ C++ 12 Operations on a Queue The following operations are performed on a queue data structure..(To display the elements of the queue) Queue data structure can be implemented in two ways. Queue Operations using Array Queue data structure using array can be implemented as follows. rear=3) 5 10 15 20 Now we decided to add an element 5 in queue| rear==Max-1 | queue full 5 10 15 20 Now we decided to delete an element from queue |front++ ( front=1 .(To insert an element into the queue)  deQueue() . then increment 'front' value by one and then display the value at 'front' position as deleted element.(To delete an element from the queue)  display() . When a queue is implemented using linked list. Initially both ‘front’ and 'rear' are set to -1. queue implemented using array can store only fixed number of data values. Whenever. rear=2) 5 10 15 Now we decided to add an element 20 in queue | rear<=Max-1 | rear++ ( front=0 .  enQueue(value) . just define a one dimensional array of specific size and insert or delete the values into that array by using FIFO (First In First Out) principle with the help of variable 'front' and 'rear'. But. we want to insert a new value into the queue. The implementation of queue data structure using array is very simple..

then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function. We can use the following steps to insert an element into the queue. Step 2: Declare a class Queue with private : one dimensional array with above defined SIZE (int queue[SIZE]) & two other data member 'front' and 'rear' and public : functions which are used in queue implementation. rear=1) ‘Steps to create an empty queue. rear = -1) Step 4: Then implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on queue. deQueue() is a function used to delete an element from the queue... In a queue. The enQueue() function takes one integer value as parameter and inserts that value into the queue. then increment rear value by one (rear++) and set queue[rear] = value. @VKSLearningHub Page 3 . We can use the following steps to delete an element from the queue. rear=3) 15 20 Now we decided to delete an element from queue |front++ ( front=3 .QUEUE@ C++ 12 Now we decided to delete an element from queue |front++ ( front=2 . A constructor to initialize both 'front' and 'rear' with '1'. deQueue() .. Step 3: If it is NOT FULL. the new element is always inserted at rear position. rear=3) 20 Now we decided to delete an element from queue |front==MAX-1 ( front=-1 . (int front = -1. the element is always deleted from front position. enQueue(value) is a function used to insert a new element into the queue.Deleting a value from the Queue In a queue data structure. Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with specific value. In a queue. (rear == SIZE-1) Step 2: If it is FULL. Step 1: Check whether queue is FULL. The deQueue() function does not take any value as parameter..

double fees. if it TRUE. }. then increment the front value by one (front + +). rear.. Then display queue[front] as deleted element. ~queue() { delete []Queue. Repeat the same until 'i' value is equal to rear (i <= rear) Program to implement Queue using Array #include <iostream. then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function..Displays the elements of a Queue We can use the following steps to display the elements of a queue. Then check whether both front and rear are equal (front == rear). Step 4: Display 'queue[i]' value and increment 'i' value by one (i++). } void enQueue().h> #include <stdio. then set both front and rear to '-1' (front = rear = -1). char name[20]. Step 3: If it is NOT EMPTY. then define an integer variable 'i' and set 'i = front+1'. int front. void display(). (front == rear) Step 2: If it is EMPTY. then display "Queue is EMPTY!!!" and terminate the function. void deQueue(). } @VKSLearningHub Page 4 . Step 1: Check whether queue is EMPTY. Step 3: If it is NOT EMPTY. struct student { int roll. class queue { student Queue[SIZE}. (front == rear) Step 2: If it is EMPTY. display() .h> const SIZE=10.QUEUE@ C++ 12 Step 1: Check whether queue is EMPTY. public: queue() { front=rear=-1.

fees<<" inserted\n". k++){ @VKSLearningHub Page 5 .name<<'.'<<t. cin>>t.'<<t. void queue:: enQueue(). else if (rear==SIZE-1) rear=0. cin>>t. if (front==rear) front=rear=-1. cout<<"Fees? ". } } void queue::display() { if (front==-1) cout<<"Queue Empty\n".roll<<'. else { cout<<"Displaying Queue\n".fees<<" deleted\n". Queue[rear]=t.name). cout<<"Roll? ". for (int k=front. student t. cout<<t.roll. cout<<"Name? ".'<<t.fees.roll<<'. else if (front==SIZE-1) front=0.name<<'. else { student t=Queue[front].'<<t.QUEUE@ C++ 12 }. gets(t. { if (front==0 && rear==SIZE-1 || front==rear+1) cout<<"Queue Overflow\n". else front++. else rear++. k!=rear+1. else { if (rear==-1) front=rear=0. } } void queue::deletion() { if (front==-1) cout<<"Queue Underflow\n". cout<<t.

cout<<"2. } } while (ch!=0). break. The Queue implemented using linked list can organize as many data values as we want. Display\n". int ch. A queue data structure can be implemented using linked list data structure. Exit\n". cout<<"0. the amount of data must be specified in the beginning itself. do { cout<<"1. Delete\n".fees<<endl. break. the last inserted node is always pointed by 'rear' and the first node is always pointed by 'front'. cout<<"Choice[0-3]? ". cin>>ch. The queue which is implemented using linked list can work for unlimited number of values.roll<<' '<<t.name<<' '<<t. In linked list implementation of a queue. switch (ch) { case 1: obj. case 3: obj.QUEUE@ C++ 12 k%=SIZE. That means.insertion(). queue using linked list can work for variable size of data (No need to fix the size at beginning of the implementation). cout<<t. case 2: obj. break. Insert\n". That means.deletion(). It will work for only fixed number of data. student t=Queue[k]. cout<<"3. Queue using array is not suitable when we don't know the size of data which we are going to use.} } } void main() { queue obj(10). } Queue using Linked List The major problem with the queue implemented using array is. Example @VKSLearningHub Page 6 .display().

Step 1: Check whether queue is Empty (front == NULL).. deQueue() . Step 2: Define a 'Node' structure with two members data and next.. 22 and 50. And declare all the user defined functions. we need to set the following things before implementing actual operations. Step 1: Create a newNode with given value and set 'newNode → next' to NULL.. define a Node pointer 'temp' and set it to 'front'. Step 4: If it is Not Empty then. Step 2: If it is Empty then. display() .. Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL.. @VKSLearningHub Page 7 . 15.Displaying the elements of Queue We can use the following steps to display the elements (nodes) of a queue..Deleting an Element from Queue We can use the following steps to delete a node from the queue.Inserting an element into the Queue We can use the following steps to insert a new node into the queue. Step 1: Include all the header files which are used in the program. Step 1: Check whether queue is Empty (front == NULL).QUEUE@ C++ 12 In above example. Step 2: Check whether queue is Empty (rear == NULL) Step 3: If it is Empty then. then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the function Step 3: If it is Not Empty then. set front = newNode and rear = newNode. display 'Queue is Empty!!!' and terminate the function. The order of elements inserted is 10. Step 4: Implement the main method by displaying Menu of list of operations and make suitable function calls in the main method to perform user selected operation. Operations To implement queue using linked list. Step 4: Then set 'front = front → next' and delete 'temp' (free(temp)). set rear → next = newNode and rear = newNode. Step 2: If it is Empty. enQueue(value) . the last inserted node is 50 and it is pointed by 'rear' and the first inserted node is 10 and it is pointed by 'front'.

cout<<p->roll<<'. Step 5: Finally! Display 'temp → data ---> NULL'. Repeat the same until 'temp' reaches to 'rear' (temp → next !=NULL). *rear. class queue { node *front. p->next=NULL.QUEUE@ C++ 12 Step 3: If it is Not Empty then. } void insertion(). if (p==NULL) cout<<"Queue Overflow\n". public: queue() { front=rear=NULL. ~queue().'<<p->name<<'.'<<p->roll<<" Inserted\n". cout<<"Fees? ". else rear=rear->next=p. void deletion(). cin>>p->fees. gets(p->name). define a Node pointer 'temp' and initialize with front. char name[20]. cout<<"Name? ". cin>>p->roll.h> #include <stdio. void display(). Step 4: Display 'temp → data --->' and move it to the next node. double fees. }. Program for Queue Using Linked List #include <iostream. if (rear==NULL) rear=front=p. }.h> struct node { int roll. node* next. else { cout<<"Roll? ". void queue::insertion() { node* p=new node. @VKSLearningHub Page 8 .

else { node* p=front.'<<p->fees<<" deleted\n". do @VKSLearningHub Page 9 .'<<p->fees<<endl. front=front->next. while (p!=NULL) { cout<<p->roll<<'.'<<p->name<<'.QUEUE@ C++ 12 } } void queue::deletion() { if (front==NULL) cout<<"Queue Underflow\n". int ch. } } } queue::~queue() { while (front!=NULL) { node* p=front. cout<<"Displaying Queue\n". delete p. } } void main() { queue obj. p=p->next. front=front->next. cout<<p->roll<<'. else { node* p=front. } } void queue::display() { if (front==NULL) cout<<"Queue Empty\n". if (front==NULL) rear=NULL.'<<p->name<<'. delete p.

cout<<"0. cout<<"Choice[0-3]? ".QUEUE@ C++ 12 { cout<<"1. cin>>ch.. break. } } while (ch!=0). Exit\n". cout<<"3. } Circular Queue In a normal Queue Data Structure. break. case 2: obj. @VKSLearningHub Page 10 . Display Queue\n". switch (ch) { case 1: obj.. For example consider the queue below. break. case 3: obj. But once if queue becomes full.display(). we can insert elements until queue becomes full. Delete from Queue\n".. we can not insert the next element until all the elements are deleted from the queue. After inserting all the elements into the queue. cout<<"2.insertion().deletion(). Insert into Queue\n". Now consider the following situation after deleting three elements from the queue..

This is the major problem in normal queue data structure..QUEUE@ C++ 12 This situation also says that Queue is Full and we can not insert the new element because. (int front = -1. In above situation. What is Circular Queue? 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. we first perform the following steps before we implement actual operations. Step 3: Create a one dimensional array with above defined SIZE (int cQueue[SIZE]) Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. rear = -1) Step 5: Implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on circular queue. Implementation of Circular Queue To implement a circular queue data structure using array. Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with specific value. Graphical representation of a circular queue is as follows.Inserting value into the Circular Queue @VKSLearningHub Page 11 . Step 2: Declare all user defined functions used in circular queue implementation. even though we have empty positions in the queue we can not make use of them to insert new element. 'rear' is still at last position.. enQueue(value) . To overcome this problem we use circular queue data structure.

then set both front and rear to '-1' (front = rear = -1). @VKSLearningHub Page 12 . The enQueue() function takes one integer value as parameter and inserts that value into the circular queue.1 and rear are equal (front -1 ==rear). Step 4: Increment rear value by one (rear++). Then check whether both front . We can use the following steps to insert an element into the circular queue.. deQueue() .1 && front != 0 if it is TRUE. In a circular queue.. Step 3: If it is NOT FULL. if it is TRUE. Step 4: Check whether 'front <= rear'. if it is TRUE. We can use the following steps to delete an element from the circular queue. display() . Step 1: Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front == rear+1)) Step 2: If it is FULL. the new element is always inserted at rear position. The deQueue() function doesn't take any value as parameter. (front == -1) Step 2: If it is EMPTY. Repeat the same until 'i <= rear' becomes FALSE. if it TRUE. then display queue[front] as deleted element and increment the front value by one (front ++). then set front = 0.Displays the elements of a Circular Queue We can use the following steps to display the elements of a circular queue. then define an integer variable 'i' and set 'i = front'. Step 3: If it is NOT EMPTY. set queue[rear] = value and check 'front == -1' if it is TRUE.. then display "Queue is EMPTY!!!" and terminate the function. then check rear == SIZE .Deleting a value from the Circular Queue In a circular queue.. Step 1: Check whether queue is EMPTY. Then check whether front == SIZE. (front == -1 && rear == -1) Step 2: If it is EMPTY. In a circular queue.QUEUE@ C++ 12 In a circular queue. then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function. enQueue() is a function which is used to insert an element into the circular queue. Step 1: Check whether queue is EMPTY... deQueue() is a function used to delete an element from the circular queue. then display 'queue[i]' value and increment 'i' value by one (i++). Step 3: If it is NOT EMPTY. the element is always deleted from front position. then set front = 0. then set rear = -1. then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function.

then display 'queue[i]' value and increment 'i' value by one (i++). Program to implement Queue using Array Write a function in C++ to perform insert operation in a static circular Queue containing Book’s information (represented with the help of an array of structure BOOK). Repeat the same until'i <= SIZE . Step 7: Again display 'cQueue[i]' value and increment i value by one (i+ +). // Book Title }.1' becomes FALSE.QUEUE@ C++ 12 Step 5: If 'front <= rear' is FALSE. Repeat the same until 'i <= rear' becomes FALSE. // Book Accession Number char Title[20]. @VKSLearningHub Page 13 . Step 6: Set i to 0. struct BOOK { long Accno.