-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
60 lines (50 loc) · 1.25 KB
/
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
/**
* list.c
*
* Full Name:
* Course section:
* Description of the program: Various list operations
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"
#include "process.h"
// add a new node to the list
void insert(struct node **head, Process *newProcess) {
// add the new process to the list
struct node *newNode = malloc(sizeof(struct node));
newNode->process = newProcess;
newNode->next = *head;
*head = newNode;
}
// delete the selected now from the list
void delete(struct node **head, Process *process) {
struct node *temp;
struct node *prev;
temp = *head;
// special case - beginning of list
if (process->pid ==temp->process->pid) {
*head = (*head)->next;
}
else {
// interior or last element in the list
prev = *head;
temp = temp->next;
while ((process->pid !=temp->process->pid) ) {
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
}
}
// traverse the list
void traverse(struct node *head) {
struct node *temp;
temp = head;
while (temp != NULL) {
printf("[%d] [%d]\n", temp->process->pid, temp->process->cpu_time);
temp = temp->next;
}
}