C++ programs using Queue |Queue programs in C++

1.  Write a C++ program to implement queue using array.

 

#include<iostream>

using namespace std;

int queue[5], n=5, front=-1, rear=-1;

void Insert() {

   int val;

   if (rear==n-1)

   cout<<“Queue Overflow”<<endl;

   else {

      if (front==-1)

      front= 0;

      cout<<“Insert the element in queue: “;

      cin>>val;

      rear++;

      queue[rear]=val;

   }

}

void Delete() {

   if (front ==-1||front>rear) {

      cout<<“Queue Underflow “;

      return ;

   } else {

cout<<“Element deleted from queue is : “<< queue[front] <<endl;

      front++;;

   }

}

void Display() {

   if (front ==-1)

   cout<<“Queue is empty”<<endl;

   else {

   cout<<“Queue elements are :”;

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

      cout<<queue[i]<<” “;

         cout<<endl;

   }

}

int main() {

   int ch;

   cout<<“1) Insert element to queue”<<endl;

   cout<<“2) Delete element from queue”<<endl;

   cout<<“3) Display all the elements of queue”<<endl;

   cout<<“4) Exit”<<endl;

   do {

      cout<<“Enter your choice : “;

      cin>>ch;

      switch(ch) {

         case 1: Insert();

         break;

         case 2: Delete();

         break;

         case 3: Display();

         break;

         case 4:
        cout<<“Exit”<<endl;

         break;

         default:
        cout<<“Invalid choice”<<endl;

      }

   } while(ch!=4);

   return 0;

}

Output:

 1) Insert element to queue

2) Delete element from queue

3) Display all the elements of queue

4) Exit

Enter your choice : 1

Insert the element in queue : 12

Enter your choice : 1

Insert the element in queue : 23

Enter your choice : 1

Insert the element in queue : 45

Enter your choice : 2

Element deleted from queue is : 12

Enter your choice : 3

Queue elements are : 23 45

Enter your choice : 4

Exit

 

2.  Write a C++ program to implement the queue data structure using class.

 

#include <iostream>

#define MAX_SIZE 5

using namespace std;

class Queue {

private:

int myqueue[MAX_SIZE],front,rear;

public:

Queue(){

front=-1;

rear=-1;

    }

bool isFull(){

if(front==0 && rear==MAX_SIZE – 1){

return true;

}

return false;

}

bool isEmpty(){

if(front==-1) return true;

else return false;

}

void enQueue(int value){

if(isFull()){

cout << endl<< “Queue is full!!”;

        } else {

if(front==-1) front = 0;

rear++;

myqueue[rear]=value;

cout<<value << ” “;

        }

    }

int deQueue(){

int value;

if(isEmpty()){

cout<<“Queue is empty!!”<<endl;

return(-1); }

else {

   value=myqueue[front];

   if(front>=rear) { //only one element in queue

front = -1;

rear = -1;

 } else {

front++;

   }

cout<<endl<<“Deleted=>”<<value<<“from
myqueue”;

return(value);

  }

    }

    /* Function to display elements of Queue */

void displayQueue() {

int i;

if(isEmpty()) {

cout<<endl<<“Queue is Empty!!”<< endl;

}

else {

cout<<endl<<“Front=”<< front;

cout<<endl<<“Queue elements: “;

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

cout<<myqueue[i]<< “\t”;

cout<<endl<<“Rear=”<<rear<<endl;

        }

    }

};

int main()

{

Queue myq;

myq.deQueue();        

cout<<“Queue created:”<<endl;

myq.enQueue(10);

myq.enQueue(20);

myq.enQueue(30);
myq.enQueue(40);

myq.enQueue(50);

//enqueue 60 => queue is full

myq.enQueue(60);

myq.displayQueue();

myq.deQueue();

myq.displayQueue();

return 0;

}

 

Output:

Queue is empty!!

Queue created:

10 20 30 40 50

Queue is full!!

Front = 0

Queue elements : 10    20      30     40      50

Rear = 4

Deleted =>10 from myqueue

Front = 1

Queue elements : 20    30      40      50

Rear = 4

 

55 thoughts on “C++ programs using Queue |Queue programs in C++”

  1. Great V I should definitely pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Excellent task..

    Reply
  2. I must point out my gratitude for your kindness giving support to men and women who absolutely need guidance on the matter. Your special dedication to passing the message across ended up being wonderfully advantageous and have enabled people just like me to reach their endeavors. Your entire valuable facts indicates a great deal a person like me and especially to my office colleagues. Best wishes; from all of us.

    Reply
  3. I am glad for writing to let you be aware of what a helpful encounter my wife’s daughter enjoyed studying your web site. She learned plenty of issues, which include how it is like to possess an awesome coaching heart to have many more smoothly know some complicated topics. You undoubtedly exceeded my expected results. Thanks for showing these practical, trustworthy, revealing and as well as unique tips about your topic to Julie.

    Reply
  4. I think other website proprietors should take this website as an model, very clean and great user friendly style and design, as well as the content. You’re an expert in this topic!

    Reply
  5. What’s Happening i’m new to this, I stumbled upon this I have found It absolutely helpful and it has helped me out loads. I hope to contribute & assist other users like its helped me. Great job.

    Reply
  6. Just about all of the things you state is supprisingly legitimate and it makes me wonder why I had not looked at this with this light previously. This particular piece really did turn the light on for me personally as far as this specific subject matter goes. Nevertheless there is actually one factor I am not really too cozy with and while I attempt to reconcile that with the core idea of your position, permit me observe what the rest of the readers have to say.Very well done.

    Reply
  7. Thanks for your marvelous posting! I seriously enjoyed reading it, you might be a great author.I will make sure to bookmark your blog and will eventually come back from now on. I want to encourage you continue your great work, have a nice morning!

    Reply
  8. I¦ve been exploring for a bit for any high quality articles or weblog posts in this kind of house . Exploring in Yahoo I ultimately stumbled upon this site. Reading this information So i¦m satisfied to exhibit that I’ve a very good uncanny feeling I found out exactly what I needed. I so much definitely will make sure to do not fail to remember this web site and provides it a glance on a relentless basis.

    Reply
  9. Hello there I am so thrilled I found your weblog, I really found you by accident, while I was searching on Digg for something else, Anyhow I am here now and would just like to say thank you for a tremendous post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.

    Reply
  10. I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great info I was looking for this info for my mission.

    Reply

Leave a Comment