Table of Contents
This article is about queue implementation using array in C++.
Queue as an Abstract data Type
Queue is an ordered data structure to store datatypes in FIFO (First in First Out) order. That means the element which enters first is first to exit(processed). It’s like the normal queue in front of any ticket counter. Of course, the first person would be the first to get his ticket from the counter. Queue is a similar data type where insertion and deletion are done at two opposite ends, namely front and rear. Queue only hold similar datatypes.
The insertion process is named as enQueue
The deletion process is named as deQueue
EnQueue at the rear end
DeQueue at the front end
Operation on Queue
Type_t =any datatype
Basic operations:
enQueue(Type_t data): It inserts data of datatype Type_t to the queue
Type_t deQueue(): Removes the first element(at front end) from queue and returns it
Other operations:
Type_t queue_front(): Returns front element
bool isEmpty(): returns true if queue is empty, else returns false
int size(): Returns size of the queue
Example:
Let the elements inserted are
1, 2, 3, 4
C++ implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include<bits/stdc++.h> using namespace std; #define MAX_SIZE 1024 int arr[MAX_SIZE]; int front=0; int rear=0; void enQueue(int x){ arr[front++]=x; cout<<x<<" is enQueued\n"; } bool isEmpty(){ if(front==rear) return true; return false; } bool isFull(){ if(front==MAX_SIZE) return true; return false; } int deQueue(){ if(isEmpty()){ cout<<"Empty Queue\n"; return INT_MIN; } int val=arr[rear++]; return val; } int queue_front(){ if(isEmpty()){ cout<<"Empty Queue\n"; return INT_MIN; } return arr[front-1]; } int main() { //menu for operations //press 1 for push (with data) //press 2 for pop() //press 3 for front() //press 4 for size() //press 0 to exit() cout<<"press 1 for enQueue()\n"; cout<<"press 2 for deQueue()\n"; cout<<"press 3 for front()\n"; cout<<"press 4 for size()\n"; cout<<"press 0 for exit\n"; int choice; cout<<"press your choice\n"; cin>>choice; while(choice){ if(choice==1){ int data; cout<<"Enter element\n"; cin>>data; enQueue(data); } else if(choice==2){ int item=deQueue(); if(item==INT_MIN){} else cout<<"Dequeued element: "<<item<<endl; } else if(choice==3){ int item=queue_front(); if(item==INT_MIN){} else cout<<"Front element: "<<item<<endl; } else if(choice==4){ cout<<"Size is: "<<(front-rear)<<endl; } else cout<<"Invalid number, try again!\n"; cout<<"press your choice\n"; cin>>choice; } cout<<"Exiting...\n"; return 0; } |
Output:
<div class="content-box-yellow”>
press 1 for enQueue()
press 2 for deQueue()
press 3 for front()
press 4 for size()
press 0 for exit
press your choice
1
Enter element
1
1 is enQueued
press your choice
2
Dequeued element: 1
press your choice
2
Empty Queue
press your choice
1
Enter element
2
2 is enQueued
press your choice
1
Enter element
3
3 is enQueued
press your choice
3
Front element: 3
press your choice
4
Size is: 2
press your choice
5
Invalid number, try again!
press your choice
0
Exiting…