Priority Queue in Data Structures using C++

Program 1

// Priority Queue
#include<iostream>
#include<stdio.h>
#define clrscr() system("cls")
using namespace std;
class node
{
    public:
    int data;
    int p;
    node *add;
};
node *start=NULL,*new1,*temp;
void create();
void insert();
void display();
void delete1();
int main()
{
     int choice;
     clrscr();
   do
  {  
     cout<<"\n-----------------Priority Queue---------------------";
     cout<<"\n 1. Create";
     cout<<"\n 2. Display";
     cout<<"\n 3. Insert";
     cout<<"\n 4. Delete";
     cout<<"\n 5. Exit";
     cout<<"\n------------------------------------------------------";
     cout<<"\nEnter your choice";
     cin>>choice;
     switch(choice)
     {
        case 1:create();break;
        case 2:display();break;
        case 3:insert();break;
        case 4:delete1();break;
        case 5:break;
        default:cout<<"\nInvalid choice";
     }
  }while(choice!=5);  
}
void create()
{
    int n,pr;
    char ch;
    cout<<"\nEnter an element";
    cin>>n;
    cout<<"\n Enter priority of node";
    cin>>pr;
    start=new node;
    start->data=n;
    start->p=pr;
    start->add=NULL;
    temp=start;
    cout<<"\n Want to continue";
    cin>>ch;
    while(ch=='y' || ch=='Y')
    {
        cout<<"\n Enter next element";
        cin>>n;
         cout<<"\n Enter priority of node";
         cin>>pr;
         new1=new node;
         new1->data=n;
         new1->p=pr;
         new1->add=NULL;
         temp->add=new1;
         temp=temp->add;
        cout<<"\n Want to continue";
        cin>>ch;     
    }

}
void display()
{
     if(start==NULL)
       cout<<"Queue not found.....";
     else
     {
        cout<<"\nElement of Queue\n";
        temp=start;
        while(temp!=NULL)
        {
            cout<<" Data="<<temp->data <<" Priorty="<<temp->p;
            temp=temp->add;
        }
        // cout<<"\nPriority of Node\n";
        // temp=start;
        // while(temp!=NULL)
        // {
        //     cout<<"  "<<temp->p;
        //     temp=temp->add;
        // }
     }  
}
void insert()
{
     cout<<"This is insert";
}
void delete1()
{
     cout<<"This is delete";
     
}

 

courses
Image

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *