Priority With Deadline
Priority With Deadline
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int priority;
int deadline;
string data;
Node* next;
Node(int priority, int deadline, string data, Node* next = NULL):
data(data),priority(priority),deadline(deadline),next(next){}
};
void insert(Node*& head, int p, int d, string str){
Node* newNode = new Node(p,d,str);
if(head==NULL){
head=newNode;
return;
}
Node* temp = head;
Node* prev=NULL;
while(temp!=NULL){
if(temp->priority<p || (temp->priority==p && temp->deadline>d)){
break;
}
prev=temp;
temp=temp->next;
}
if(prev==NULL){
newNode->next=head;
head=newNode;
return;
}
newNode->next=temp;
prev->next=newNode;
}
Node* reverse(Node* head){
if(head==NULL){
return NULL;
}
Node* temp=head;
Node* prev = NULL;
while(temp!=NULL){
Node* front = temp->next;
temp->next=prev;
prev=temp;
temp=front;
}
return prev;
}
void print(Node* head){
if(head==NULL){
cout<<"Queue is empty";
return;
}
while(head!=NULL){
cout<<head->data<<endl;
head=head->next;
}
}
int main(){
int n;
cin>>n;
Node* head = NULL;
for(int i=0;i<n;i++){
int p,d;
string str;
cin>>p>>d>>str;
insert(head,p,d,str);