-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.h
142 lines (127 loc) · 2.84 KB
/
linked_list.h
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define STOUT 1
#define STIN 0
#define MAX_ARG_LEN 1040
struct Node
{
int pid;
int job_id;
char *name;
struct Node *next;
char *status;
} Node;
struct Node* push(struct Node **head, int ppid, char *name_s[100], char *s, int j) {
struct Node *tmp;
if (head == NULL) {
tmp = (struct Node*)malloc(sizeof(struct Node));
tmp->pid = ppid;
tmp->name = *name_s;
tmp->status = s;
tmp->job_id = j;
*head = tmp;
} else {
tmp = (struct Node*) malloc(sizeof(struct Node));
tmp->pid = ppid;
tmp->name = *name_s;
tmp->status = s;
tmp->job_id = j;
tmp->next = *head;
*head = tmp;
}
return *head;
}
struct Node* pop(struct Node **head) {
struct Node *tmp;
if (head == NULL) {
return NULL;
perror("attempting to access empty list");
}
else {
tmp = *head;
if(tmp) {
if (tmp->next == NULL) {
*head = NULL;
}
else {
*head = tmp->next;
}
}
return tmp;
}
}
struct Node *remove_node(struct Node **head, int jobx) {
struct Node *prev;
struct Node *tmp;
struct Node *curr;
curr = *head;
if (head == NULL) {
perror("attempting to access empty list");
}
else if (curr->job_id == jobx) {
tmp = *head;
*head = NULL;
return tmp;
}
else {
while(curr->next != NULL) {
if (curr->next->job_id == jobx) {
prev = curr;
tmp = curr->next;
prev->next = curr->next->next;
return tmp;
}
curr = curr->next;
}
}
return NULL;
}
int get_ppid(struct Node *head, int pidx) {
int i = 0;
struct Node *tmp = head;
while(tmp != NULL) {
if (i == pidx) {
return tmp->pid;
}
tmp = tmp->next;
i++;
}
return -1;
}
int get_head_ppid(struct Node *head) {
return head->pid;
}
char *get_name(struct Node *head, int pidx) {
int i = 0;
struct Node *tmp = head;
while(tmp != NULL) {
if (i == pidx) {
return tmp->name;
}
tmp = tmp->next;
i++;
}
return NULL;
}
char *get_status(struct Node *head, int pidx) {
int i = 0;
struct Node *tmp = head;
while(tmp != NULL) {
if (i == pidx) {
return tmp->status;
}
tmp = tmp->next;
i++;
}
return NULL;
}
void print_list(struct Node* head){
struct Node* curr = head;
write(STOUT, "----------LIST-------\n", 22);
while(curr != NULL){
printf("%d\n", curr->pid);
curr=curr->next;
}
write(STOUT, "---------------------\n", 22);
}