-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPriorityLinkedList.cpp
More file actions
128 lines (116 loc) · 2.85 KB
/
PriorityLinkedList.cpp
File metadata and controls
128 lines (116 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
using namespace std;
/*
highest num-high priority
tasks object.push() to input
tasks object.display() to display
tasks object.peek() to display task with highest priority
tasks object.pop() to pop the element with highest priority
*/
class TaskNode{
public:
string task;
int priority;
TaskNode* next;
TaskNode(){
task='x';
priority=0;
next=NULL;
}
TaskNode(string Task,int Priority){
task=Task;
priority=Priority;
next=NULL;
}
friend istream& operator>>(istream &in , TaskNode &p);
friend ostream& operator<<(ostream &out, const TaskNode &p);
};
class Tasks:public TaskNode
{
public:
TaskNode *head;
string peek();
void pop();
void push();
void display();
Tasks()
{
head=NULL;
}
};
ostream& operator<<(ostream &out, const TaskNode &p)
{
out<<"Task :"<<p.task<<endl;
out<<"Priority :"<<p.priority<<endl;
return out;
}
istream& operator>>(istream &in,TaskNode &p){
cout<<"Enter task To be assigned :"<<endl;
getchar();
getline(in,p.task);
cout<<"Press Enter to confirm !"<<endl;
getchar();
cout<<"Enter priority of the task :"<<endl;
in>>p.priority;
cout<<"Press Enter to confirm !"<<endl;
getchar();
return in;
}
string Tasks::peek(){
if(head->next!=NULL)
{
return head->task;
}
}
void Tasks::pop()
{
if(head->next!=NULL)
{
TaskNode* temp = head;
head=head->next;
delete temp;
}
}
void Tasks::push(){
TaskNode *node = new TaskNode();
cin>>*node;
// If head is null, this is the first TaskNode to be added
// so make head = newTaskNode
if (head == NULL) {
head = node;
return;
}
TaskNode *temp = head;
TaskNode *prev = NULL;
// search for first node having priority less than p
while (temp != NULL && temp->priority >node->priority ) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
// no TaskNode with priority less than this node (Case 1)
prev->next = node;
}
else {
if (prev == NULL) {
// all the nodes have priority less than p (Case 2)
// add this node at the starting
node->next = head;
head = node;
}
else {
// Case 3
// add this node before the node with priority less than p
node->next = temp;
prev->next = node;
}
}
}
void Tasks::display(){
TaskNode *curr=new TaskNode();
curr=head;
while(curr!=NULL ){
cout<<*curr<<endl;
curr=curr->next;
}
}