-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdlist.c
More file actions
172 lines (156 loc) · 3.04 KB
/
Copy pathdlist.c
File metadata and controls
172 lines (156 loc) · 3.04 KB
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
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include "dlist.h"
#include "job_node.h"
#define FALSE 0
#define TRUE 1
typedef struct dlist_record{
job_node* head;
job_node* tail;
int size;
}dlist_record;
dlist dlist_new(){
dlist l = (dlist)malloc(sizeof(dlist_record));
l->head = NULL;
l->tail = NULL;
l->size =0;
return l;
}
int dlist_size(dlist l){
return l->size;
}
job_node* get_head(dlist dl) {
return dl->head;
}
job_node* get_tail(dlist dl) {
return dl->tail;
}
void dlist_push_end(dlist l, job_node* n){
if(l->size == 0) {
l->head = n;
l->tail = n;
n->prev = NULL;
n->next = NULL;
} else {
job_node* old_tail = l->tail;
old_tail->next = n;
n->prev = old_tail;
n->next = NULL;
l->tail = n;
}
l->size++;
}
void dlist_pop_end(dlist l){
job_node* n = l->tail;
if(n->prev){
l->tail = l->tail->prev;
}
l->size --;
delete_node(n);
}
job_node* dlist_peek_end(dlist l){
return l->tail;
}
void dlist_insert(dlist l, int index, job_node* n){
job_node* h = l-> head;
job_node* t = l-> tail;
bool cut_in_line = false;
if(index > (l->size)/2){
for(int i=l->size; i>index; i--){
t = t->prev;
cut_in_line=true;
}
insert_after(t,n);
if(!cut_in_line){
l->tail = t->next;
}
}else{
for(int i=0; i<index; i++){
h = h->next;
cut_in_line =true;
}
if(h){
insert_before(h,n);
if(!cut_in_line){
l->head = h->prev;
}
}else{
l->head = n;
l->tail = l->head;
}
}
l->size++;
}
job_node* dlist_get(dlist l, int index) {
if(l == NULL) {
return NULL;
}
job_node* t = l->head;
while(t != NULL) {
if(t->index == index) {
return t;
}
t = t->next;
}
return NULL;
}
void dlist_remove(dlist l, int n){
if(n <= (l->size)/2){
delete_node(nth_job(l->head,n));
}else{
delete_node(nth_job_prev(l->head,l->size-n-1));
}
l->size--;
}
int dlist_remove_bypid(dlist l, pid_t pid) {
if(l->size == 0) {
return FALSE;
}
job_node* t = l->head;
while(t != NULL) {
if(t->pid == pid) {
job_node* tprev = t->prev;
job_node* tnext = t->next;
if(tprev != NULL && tnext != NULL) {
tprev->next = tnext;
tnext->prev = tprev;
} else if (tprev == NULL && tnext == NULL) {
l->head = NULL;
l->tail = NULL;
l->size = 0;
free(t->original_input);
free(t);
return TRUE;
} else if(tprev == NULL) {
tnext->prev = NULL;
l->head = tnext;
} else if (tnext == NULL) {
tprev->next = NULL;
l->tail = tprev;
}
l->size--;
free(t->original_input);
free(t);
return TRUE;
}
t = t->next;
}
return FALSE;
}
job_node* dlist_get_bypid(dlist l, pid_t pid){
job_node* h = l->head;
return get_jobnode_bypid(h, pid);
}
void dlist_free(dlist l){
if(l == NULL) {
return;
} else if(l->size == 0) {
free(l);
return;
}
free_joblist(l->head);
free(l);
}