-
Notifications
You must be signed in to change notification settings - Fork 32
/
list.c
96 lines (83 loc) · 2.4 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
/* your list function definitions */
void list_print(const Node *list) {
printf("*** List contents begin *** \n");
while (list != NULL) {
printf("%s\n", list->data);
list = list->next;
}
printf("*** List contents end *** \n");
}
void list_clear(Node *list) {
while (list != NULL) {
Node *tmp = list;
list = list->next;
free(tmp);
}
}
void insert(char *data, Node **head){
if ((*head) == NULL) {
Node *newnode = malloc(sizeof(Node));
strcpy(newnode->data,data);
//printf("what is new node data0: %s\n", newnode->data);
newnode->next = NULL;
*head = newnode;
}
else{
Node *curr = *head;
//printf("head in insert: %s\n", (*head)->data);
while(curr->next != NULL){
curr = curr->next;
}
Node *newnode = malloc(sizeof(Node));
newnode->next = NULL;
strcpy(newnode->data,data);
//printf("what is new node dat
curr->next = newnode;
//printf("new node data: %s\n", (curr->next)->data);
}
}
void add_process(pid_t id, char *a, char *b, Process **head){
if ((*head) == NULL){
Process *newprocess = malloc(sizeof(Process));
newprocess->p_id = id;
strcpy(newprocess->instr, a);
strcpy(newprocess->status, b);
newprocess->next = NULL;
*head = newprocess;
}
else{
Process *curr = *head;
while(curr->next != NULL){
curr = curr->next;
}
Process *newprocess = malloc(sizeof(Process));
newprocess->next = NULL;
newprocess->p_id = id;
strcpy(newprocess->instr, a);
strcpy(newprocess->status, b);
curr->next = newprocess;
}
}
void delete_process(pid_t id, Process **head){
Process *newprocess = *head;
if(newprocess != NULL){
if (newprocess->p_id == id && newprocess->next == NULL){
*head = (*head)->next;
free(newprocess);
return;
}
while(newprocess->next != NULL){
if ((newprocess->next)->p_id == id){
Process *temp = newprocess->next;
newprocess->next = (newprocess->next)->next;
free(temp);
return;
}
newprocess = newprocess->next;
}
}
}