Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/aim/toc/ll.c
1074 views
1
/*
2
* Simple linked list library (replaces GList stuff)
3
* Yea, it isnt efficient, but its only meant to be used for buddy lists (n < 100) THAT AINT LARGE :)
4
*/
5
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include "ll.h"
10
11
/* Creation */
12
13
LL CreateLL() {
14
LL newlist;
15
LLE head;
16
newlist = (LL) malloc(sizeof(struct _ll));
17
head = (LLE) CreateLLE("head element",NULL,NULL);
18
if ( ! head )
19
return NULL;
20
newlist->head = head;
21
newlist->items = 0;
22
newlist->curr = head;
23
newlist->free_e = NULL;
24
return newlist;
25
}
26
27
LLE CreateLLE (char *key, void *data, LLE next) {
28
LLE newe;
29
newe = (LLE) malloc(sizeof(struct _lle));
30
if ( ! newe ) {
31
perror("MEM allocation errory!");
32
return NULL;
33
}
34
newe->key = (char *) malloc(strlen(key)+1);
35
strcpy(newe->key,key);
36
newe->data = data;
37
newe->next = next;
38
return newe;
39
}
40
41
void SetFreeLLE(LL List, void (*free_e)(void *)) {
42
List->free_e = free_e;
43
}
44
45
int AddToLL(LL List, char *key, void *data) {
46
LLE p = List->head;
47
LLE e;
48
while ( p->next != NULL ) {
49
p = p->next;
50
}
51
e = CreateLLE(key,data,NULL);
52
p->next = e;
53
List->items++;
54
return 1;
55
}
56
57
LLE FindInLL(LL List, char *key) {
58
LLE p = List->head->next;
59
while ( p != NULL ) {
60
/* debug_printf("p != null, key = '%s'",p->key); */
61
if ( ! strcasecmp(p->key,key) )
62
break;
63
p = p->next;
64
}
65
return p;
66
}
67
68
void *GetDataFromLLE(LLE e) {
69
if ( e == NULL )
70
return NULL;
71
else
72
return e->data;
73
}
74
75
/* Removing Items from List */
76
77
int RemoveFromLL(LL List, LLE e) {
78
LLE p = List->head;
79
LLE b = NULL;
80
while ( p != NULL && p != e ) {
81
b = p;
82
p = p->next;
83
}
84
if ( p == NULL )
85
return -1;
86
b->next = p->next;
87
FreeLLE(p, List->free_e);
88
List->items--;
89
return 1;
90
}
91
92
int RemoveFromLLByKey(LL List, char *key) {
93
LLE b = List->head;
94
LLE p = b->next;
95
while ( p != NULL ) {
96
if ( ! strcasecmp(p->key,key) )
97
break;
98
b = p;
99
p = p->next;
100
}
101
if ( p == NULL )
102
return -1;
103
b->next = p->next;
104
FreeLLE(p, List->free_e);
105
List->items--;
106
return 1;
107
}
108
109
/* For easy loop traversals */
110
111
LLE GetNextLLE(LL List) {
112
if ( List->curr != NULL )
113
List->curr = List->curr->next;
114
return List->curr;
115
}
116
117
void ResetLLPosition(LL List) {
118
List->curr = List->head;
119
}
120
121
/* Only Free the keys at the moment */
122
123
void FreeLLE(LLE e, void (*free_e)(void *)) {
124
if ( e->key != NULL )
125
free(e->key);
126
if ( free_e != NULL && e->data != NULL)
127
free_e(e->data);
128
free(e);
129
return;
130
}
131
132
void FreeLL(LL List) {
133
LLE e;
134
LLE n;
135
if ( List == NULL ) {
136
perror("SERIOUS ERROR: tried to free null list!");
137
return;
138
}
139
e = List->head->next;
140
free(List->head);
141
while ( e != NULL ) {
142
n = e->next;
143
FreeLLE(e, List->free_e);
144
e = n;
145
}
146
free(List);
147
return;
148
}
149
150