0% found this document useful (0 votes)
4 views

Priority Queue

Uploaded by

rahuljand44
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Priority Queue

Uploaded by

rahuljand44
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<bits/stdc++.

h>
using namespace std;
class Node{
public:
int roll_no;
int priority;
Node* next;
Node(int data, int prio, Node*
next=NULL):roll_no(data),priority(prio),next(next){}
};
class Queue{
public:
Node* start;
Node* end;
Queue():start(nullptr),end(nullptr){}

void insert(int n, int m){


Node* temp = new Node(n,m);
if(start==NULL){
start = temp;
end=temp;
}else{

if(start->priority>=m){
temp->next=start;
start=temp;
return;
}

Node* front=start;
Node* prev=NULL;
while(front!=NULL){
if(front->priority>=m){
break;
}
prev=front;
front=front->next;
}
temp->next=front;
prev->next=temp;
if(front==NULL){
end=temp;
}

void pop(){
if(start==NULL){
cout<<"Queue is empty"<<endl;
return;
}
Node* temp=start;
start=start->next;
delete(temp);
if(start==NULL){
end=NULL;
}
}
void print(){
if(start == NULL){
cout<<"Queue is empty"<<endl;
return;
}
Node* temp=start;
while(temp!=NULL){
cout<<temp->roll_no<<" ";
temp=temp->next;
}
}
};
int main(){
int n;
cin>>n;
Queue* vc = new Queue();
for(int i=0;i<n;i++){
int p,q;
cin>>p;
cin>>q;
vc->insert(p,q);
}
cout<<"Priority Queue: ";
vc->print();

You might also like