-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.cpp
112 lines (95 loc) · 1.6 KB
/
linkedlist.cpp
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
#include "linkedlist.hpp"
linkedlist::linkedlist(){
head=nullptr;
end=nullptr;
}
linkedlist::~linkedlist(){
patient *node=head;
while(node!=nullptr)
{
patient *next = node->next;
delete node;
node = next;
}
head=nullptr;
end=nullptr;
}
void linkedlist::enqueue(string a, int b, int c){
if(head == nullptr)
{
head = new patient(a, b, c);
end = head;
}
else
{
end->next = new patient(a, b, c);
end = end->next;
}
}
void linkedlist::dequeue(){
while(head!=nullptr)
{
patient *next = head->next;
delete head;
head = next;
}
head=nullptr;
end=nullptr;
}
void linkedlist::print(){
cout<<"Rank "<<"patient, "<<"Priority, "<<"Treatment"<<endl;
int i=1;
patient *node = head;
while(node!=nullptr)
{
cout<<i<<": "<<node->name<<", "<<node->priority<<", "<<node->treatment<<endl;
node = node->next;
i++;
}
}
void linkedlist::sort(){
patient *insert = head -> next;
head -> next = nullptr;
end = head;
while(insert != nullptr){
patient *tem = head;
patient *parent = nullptr;
patient *p = insert;
insert = insert -> next;
while(tem != nullptr){
if(p->priority == tem->priority){
if(p->treatment >= tem->treatment){
parent = tem;
tem = tem->next;
}
else{
break;
}
}
else{
if(p->priority > tem->priority){
parent = tem;
tem = tem->next;
}
else{
break;
}
}
}
if(parent == nullptr){
p -> next = head;
head = p;
}
else{
if(parent == end){
p -> next = nullptr;
end -> next = p;
end = p;
}
else{
p -> next = parent -> next;
parent -> next = p;
}
}
}
}