C++ Program to Implement Priority Queue using class

By | 09.01.2017

Priority Queue using class


Write a C++ Program to Implement Priority Queue using class. Here’s simple C++ Program to Implement Priority Queue using class in C++ Programming Language.


What is Queue ?


Queue is also an abstract data type or a linear data structure, in which the first element is inserted from one end called REAR, and the deletion of existing element takes place from the other end called as FRONT.

This makes queue as FIFO data structure, which means that element inserted first will also be removed first.


Basic Operations : :


  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue.
  • peek() − Gets the element at the front of the queue without removing it.
  • isfull() − Checks if the queue is full.
  • isempty() − Checks if the queue is empty.

Below is the source code for C++ Program to Implement Priority Queue using class which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to Implement Priority Queue using class  */

#include<iostream>

using namespace std;

        const int MAX=5;

 struct data
                  {
                   int val,p,o;
                  }d[MAX];

        class pqueue
        {
                  int front,rear;
                public:


                 pqueue()
                 {
                        front=rear=-1;
                 }
                 void insert(data d1);
                 data deletion();
                 void display();
        };
        void pqueue :: insert(data d1)
        {
                if(rear==MAX-1)
                   cout<<"\nPriority Queue is Full";
                else
                {
                   rear++;
                   d[rear]=d1;
                   if(front==-1)
                          front=0;
                   data temp;
                   for(int i=front;i<=rear;i++)
                         for(int j=i+1;j<=rear;j++)
                         {
                                 if(d[i].p > d[j].p)
                                 {
                                        temp=d[i];
                                        d[i]=d[j];
                                        d[j]=temp;
                                 }
                                 else
                                 {
                                         if(d[i].p==d[j].p)
                                         {
                                                 if(d[i].o > d[j].o)
                                                 {
                                                          temp=d[i];
                                                          d[i]=d[j];
                                                          d[j]=temp;
                                                 }
                                         }
                                 }
                         }
                }
        }
        data pqueue :: deletion()
        {
                data d1;
                if(front==-1)
                   cout<<"\nPriority Queue is Empty";
                else
                {
                   d1=d[front];
                   if(front==rear)
                          front=rear=-1;
                   else
                          front++;
                }
                return d1;
        }
        void pqueue :: display()
        {
                 if(front==-1)
                         cout<<"\nPriority Queue is Empty";
                 else
                 {
                        for(int i=front;i<=rear;i++)
                        {
                                cout<<"\nObject  :"<<i+1<<endl;
                                cout<<"\nValue ="<<d[i].val<<endl;
                                cout<<"\nPriority="<<d[i].p<<endl;
                                cout<<"\nOrder =  "<<d[i].o<<endl;
                        }
                 }
        }
        int main()
        {
          pqueue p1;

          data d1;
          char op;
          do
          {
                int ch;


                cout<<"\n----------Menu-------------\n";
                cout<<"\n1.Insertion\n2.Deletion\n3.Display\n4.Exit\n";
                cout<<"\nEnter your Choice<1..4> :: ";
                cin>>ch;
                switch(ch)
                {
                   case 1 :  cout<<"\nEnter Value :: ";
                                         cin>>d1.val;
                                         cout<<"\nEnter Priority :: ";
                                         cin>>d1.p;
                                         cout<<"\nEnter Order :: ";
                                         cin>>d1.o;
                                         p1.insert(d1);
                                         break;

                   case 2 :  d1=p1.deletion();
                                         cout<<"\nValue = "<<d1.val<<endl;
                                         cout<<"\nPriority = "<<d1.p<<endl;
                                         cout<<"\nOrder = "<<d1.o<<endl;
                                         break;

                   case 3 :  p1.display();
                                         break;
                  }
                  cout<<"\nDo You Want to Continue <Y/N> :: ";
                  cin>>op;
                }while(op=='Y' || op=='y');
        return 0;
          }

OUTPUT : :


/*  C++ Program to Implement Priority Queue using class  */

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 1

Enter Value :: 1

Enter Priority :: 1

Enter Order :: 1

Do You Want to Continue <Y/N> :: y

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 1

Enter Value :: 2

Enter Priority :: 2

Enter Order :: 2

Do You Want to Continue <Y/N> :: y

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 1

Enter Value :: 3

Enter Priority :: 1

Enter Order :: 0

Do You Want to Continue <Y/N> :: y

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 1

Enter Value :: 4

Enter Priority :: 2

Enter Order :: 1

Do You Want to Continue <Y/N> :: y

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 3

Object  :1

Value =3

Priority=1

Order =  0

Object  :2

Value =1

Priority=1

Order =  1

Object  :3

Value =4

Priority=2

Order =  1

Object  :4

Value =2

Priority=2

Order =  2

Do You Want to Continue <Y/N> :: y

----------Menu-------------

1.Insertion
2.Deletion
3.Display
4.Exit

Enter your Choice<1..4> :: 2

Value = 3

Priority = 1

Order = 0

Do You Want to Continue <Y/N> :: N

Process returned 0

Above is the source code and output for C++ Program to Implement Priority Queue using class which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments