Ticker

6/recent/ticker-posts

Header Ads Widget

Queue implementation using array - bigfootcode

Queue implementation using array -Bigfootcode

Introduction

In this tutorial you will learn how to implement queue using array and also we perform insertion, deletion and displays queue .

Queue implementation using array -bigfootcode


Q) write a program to implement queue using an array

Code:-

#include <iostream>
using namespace std;

int main()
 {
int a[100],size=100,front=- 1, rear = - 1,option;

cout<<"List of operations on queue";
cout<<"___________________________\n";
cout<<"1)Press 1 to insert element to queue"<<endl;
cout<<"2)Press 2 to delete element from queue"<<endl;
cout<<"3)Press 3 to display elements of queue"<<endl;
cout<<"4) Exit"<<endl;
   
do {
 cout<<"Enter your choice : "<<endl;
 cin>>option;
      
switch (option) 
{
  case 1: int val;
          if(rear ==size- 1)
         cout<<"Queue Overflow"<<endl;
          else {
         if (front == - 1)
         front = 0;
         cout<<"Insert the element :"           <<endl;
         cin>>val;
         rear++;
         a[rear] = val;
         }
        
 break;
         
case 2:    if (front == - 1 || front > rear ){
      cout<<"Queue Underflow ";
   } else {
      cout<<"Element deleted is : "<< a[front] <<endl;
      front++;
   }
         break;
         case 3:    if (front == - 1)
   cout<<"Queue is empty"<<endl;
   else {
      cout<<"Queue elements are : ";
      for (int i = front; i <= rear; i++)
      cout<<a[i]<<" ";
         cout<<endl;
   }
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(option!=4);


   return 0;
}


Output:



List of operations on queue___________________________
1)Press 1 to insert element to queue
2)Press 2 to delete element from queue
3)Press 3 to display elements of queue
4) Exit
Enter your choice :
1
Insert the element :
3
Enter your choice :
1
Insert the element :
2
Enter your choice :
1
Insert the element :
2
Enter your choice :
1
Insert the element :
3
Enter your choice :
3
Queue elements are : 3 2 2 3
Enter your choice :
2
Element deleted is : 3
Enter your choice :
2
Element deleted is : 2
Enter your choice :

Post a Comment

0 Comments