-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinked_list.c~
188 lines (169 loc) · 4.15 KB
/
linked_list.c~
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "linked_list.h"
List *create_list() {
List *new_list = malloc(sizeof(List));
Node *first = create_node();
new_list->first=first;
return new_list;
}
Node *create_node() {
Node *temp = malloc(sizeof(Node));
temp->next=NULL;
return temp;
}
void delete_list(List *list) {
Node *node = list->first;
Node *next;
while(node) {
next = node->next;
delete_node(node);
node=next;
}
free(list);
}
void remove_node(List *list, unsigned int pid) {
Node *node = list->first;
if(node->pid == pid) {
list->first = node->next;
free(node);
return;
}
while(node != NULL) {
if(node->next == NULL) break;
if(node->next->pid == pid) {
Node *temp = node->next;
node->next=node->next->next;
free(temp);
return;
}
node = node->next;
}
}
void delete_node(Node *node) {
free(node);
}
void append_list(List *list, unsigned int pid, int priority) {
Node *node = list->first;
while(node->next != NULL) { node=node->next; }
node->pid=pid;
node->priority=priority;
node->next=create_node();
}
/*
void insert(List *list, unsigned int pid, int priority) {
//Set up node to insert
Node *temp; Node *node;
temp=create_node();
temp->pid=pid;
temp->priority = priority;
Node *prev_node = malloc(sizeof(Node));
//Node is either first in the list, or has a higher priority than the first item on the list.
if(list->first == NULL || priority < list->first->priority) {
temp->next=list->first;
list->first = temp;
} else {
//Iterate through the nodes until you find one that has less priority, then put the Node in front of it.
node=list->first;
while(node->next != NULL) {
if(node->priority >= priority) {
prev_node->next=temp;
temp->next=node;
goto done;
}
prev_node=node;
node=node->next;
}
prev_node->next=temp;
temp->next=node;
}
done:
free(prev_node);
return;
}
Node *pop(List *list) {
Node *temp = list->first;
if(temp->next == NULL) {
Node *new_first = create_node();
list->first=new_first;
} else {
Node *new_first=temp->next;
list->first=new_first;
}
return temp;
}
*/
void insert(List *list, unsigned int pid, int priority) {
Node *temp; Node *node;
temp=create_node();
temp->pid=pid;
temp->priority = priority;
if(list->first == NULL || priority < list->first->priority) {
temp->next=list->first;
list->first = temp;
} else {
node=list->first;
Node *prev_node;
while(node->next != NULL) {
if(node->priority >= priority) {
prev_node->next=temp;
temp->next=node;
return;
}
prev_node=node;
node=node->next;
}
prev_node->next=temp;
temp->next=node;
}
}
Node *peek(List *list) { return list->first; }
Node *get_element(List *list, int element) {
Node *node = list->first;
int i = 0;
while(i < element && node != NULL) { node=node->next; i++; }
return node;
}
Node *find_pid(List *list, unsigned int pid) {
Node *node = list->first;
while(node->next != NULL) {
if(node->pid == pid) { return node; }
node=node->next;
}
return NULL;
}
int print_list(List *list) {
Node *node = list->first;
int i=0;
while(node->next != NULL) {
printf("Pid: %d\t Priority: %d\n",node->pid,node->priority);
node = node->next;
i++;
}
printf("\n");
return i;
}
int main(int argc, char *argv[]) {
List *list = create_list();
int i;
append_list(list,1,6);
print_list(list);
append_list(list,2,8);
insert(list,3,4);
insert(list,4,1);
insert(list,5,5);
insert(list,6,-2);
insert(list,7,10);
insert(list,8,-100);
print_list(list);
remove_node(list,8);
printf("------------------------------------\n");
print_list(list);
remove_node(list,4);
printf("------------------------------------\n");
print_list(list);
pop(list);
print_list(list);
}