-->





queue implementation in c++ programming using array

Simple c++ queue using array implementation example

which uses function loops and conditional statement concepts. 
Queue size is fixed in a variable name 'total which is equal to 5 it can be changed as a requirement.
User select function from menu and enter input.
We have the following functions in queue

  1. Enqueue 
  2. Dequeue
  3. Is Empty (check if the queue is empty)
  4. Is Full  
  5. Show All
Prototypes are
void enqueue(int);
void dequeue();
int isempty();
int isfull();
void show();


C++ Queue Source Code


#include<iostream>
#include<windows.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;
 }
}


Run the program



Example input output
queue implementation in c++ using array
Input-output

See also: C++ Project Using Array Based Queue

This example may help beginner students to understand the concept of an array using a queue. It is recommended to change the code add validations etc.


0 comments:

Post a Comment