-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
58 lines (51 loc) · 1.08 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
/*
* list.c
*
* Created on: Jan 17, 2012
* Author: root
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libapkg_defs.h"
#include "list.h"
package_ptr alloc_package() {
package_ptr p;
p = (package_ptr) malloc(sizeof(struct package_t));
memset(p, 0, sizeof(struct package_t));
p->next = NULL;
return p;
}
void add_package(package_ptr *head, package_ptr p) {
p->next = *head;
*head = p ;
}
void release_package_list(package_ptr *head) {
package_ptr p;
while(*head) {
p = *head;
*head = (*head)->next;
free(p);
}
}
void delete_package(package_ptr *head, char *package) {
package_ptr back, front;
for(back=NULL,front=*head; front != NULL && strcmp(front->package, package); back=front, front=front->next);
if(front==NULL)
return;
else if(back==NULL) {
*head = front->next;
free(front);
front = NULL;
}
else {
back->next = front->next;
free(front);
front = NULL;
}
}
void list_packages(package_ptr head) {
package_ptr traverse;
for(traverse=head; traverse != NULL; traverse=traverse->next)
PRINT("%s\n", traverse->package);
}