Queue using array in C++ :Hi Programmer Hope You are Fine today we Share Some code About Array.Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).
C++ Program to Implement Queue using Array
We have following functions in queue
Enqueue
Dequeue
Is Empty (check if queue is empty)
Is Full
Show All
Front
Rear
Source Code
#include<iostream> #include<conio.h> using namespace std; // Queue for array void enqueue(int); void dequeue(); int isempty(); int isfull(); void array_call(); void show(); const int total = 5; int marks[total]; int rear = -1; //rear mean last element of queue int front = 0;//front mean first element of queue int counter = 0; int main() { array_call(); return 0; } // function of Array void array_call() { start: system("cls"); cout << "\t\t\t\t Welcome in Queue with Array"; int input; cout << "\n 1- Enqueu \n 2- Dequeu \n 3- Front\n 4- Show all data \n 5- Exit from Queue in Array \n"; cin >> input; switch (input) { case 1: {int z; z = isfull(); if (z) { int y;//y is input whiich you insert in queue cout << "Enter you number: "; cin >> y; enqueue(y); cout << " Number entered \n"; system("pause"); } else { cout << "Your Queue is full \n"; system("pause"); } goto start; break; } case 2: { int a; a = isempty(); if (a) { dequeue(); cout << "Number deleted \n "; system("pause"); } else { cout << "Your Queue is Empty \n"; system("pause"); } goto start; break; } case 3: { int a; a = isempty(); if (a) cout << "\n Your front value is " << marks[front]<<endl; else cout << "Your Queue is Empty"<<endl; system("pause"); goto start; break; } case 4: { int a; a = isempty(); if (a) show(); else cout << "Your Queue is Empty"<<endl; system(" pause"); goto start; } case 5: { break; } default: cout << "\n You enter invalid Number \n"; system("pause"); goto start; break; } } void enqueue(int x) { rear = (rear + 1) % total; marks[rear] = x; counter = counter + 1; } void dequeue() { front = (front + 1) % total; counter = counter - 1; } int isempty() { return(counter != 0); } int isfull() { return(counter != total); } void show() { int j = front; for (int i = 0; i < counter; i++) { cout << " " << marks[j]; j++; j = j%total; } }
Output of Code
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element24
inserted24
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element20
inserted20