Header Ads

How to Implement Queue using C++ | How to Insert Value in Queue | How to Delete Value in Queue using Array Data Structure C++


How to Implement Queue using C++. How to Insert Value in Queue. How to Delete Value in Queue. How to display Value of Queue 

 Program Source Code:


#include<iostream>

using namespace std;

class Queue{

private:

int rear,front,Q[10];

public:

Queue():rear{-1},front{-1}{

}

void InsertQueue(int n){

if(rear>=9)

    cout<<"Queue is Full"<<endl;

else{

rear = rear+1;

    Q[rear]=n;

}

if(front == -1){

front = 0;

}

    

}

void RemoveQueue(){

if(front==-1)

   cout<<"Queue is Empty"<<endl;

else

   cout<<"Item deleted from Queue and Item is :"<<Q[front]<<endl;

   front++;

}

void display(){

if(front == -1)

cout<<"Queue is Empty"<<endl;

else

for(int i = front; i<=rear; i++)

  cout<<Q[i]<<" ";

cout<<endl;

}

};

main(){

Queue obj1;

int num,op;

char choice;

do{

system("cls");

cout<<"1: Insert Value into Queue"<<endl;

cout<<"2: Delete value from Queue"<<endl;

cout<<"3: Display Value of Queue"<<endl;

cout<<"-----Enter Your Option------:";

cin>>op;

switch(op){

case 1:

cout<<"Enter Value to Insert in Queue:";

cin>>num;

obj1.InsertQueue(num);

break;

case 2:

obj1.RemoveQueue();

break;

case 3:

obj1.display();

break;

default:

cout<<"You Enter Invalid Number";

}

cout<<"Do You Want to Continue [Yes/No] :";

cin>>choice;

}while(choice=='Y'||choice=='y');

}

Program Output





Related Video






No comments

Powered by Blogger.