0% found this document useful (0 votes)
6 views2 pages

Priority With Deadline

Uploaded by

rahuljand44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Priority With Deadline

Uploaded by

rahuljand44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// You are using GCC

#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);

cout<<"Executed Tasks: "<<endl;


print(head);

You might also like