-
Notifications
You must be signed in to change notification settings - Fork 12
/
pointerlist.c
111 lines (98 loc) · 2.71 KB
/
pointerlist.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*=======================================================================
solunar
pointerlist.c
PointerList object defines a linked list of pointers
(c)2005-2012 Kevin Boone
=======================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "pointerlist.h"
#include "defs.h"
/*=======================================================================
PointerList_get_last
=======================================================================*/
PointerList *PointerList_get_last (PointerList *self)
{
if (!self) return NULL;
PointerList *s = self;
while (s->next)
{
s = s->next;
}
return s;
}
/*=======================================================================
PointerList_append
=======================================================================*/
PointerList *PointerList_append (PointerList *self, void *pointer)
{
PointerList *newp = (PointerList *) malloc (sizeof (PointerList));
newp->pointer = pointer;
newp->next = NULL;
if (self == NULL)
{
self = newp;
}
else
{
PointerList *end = PointerList_get_last (self);
end->next = newp;
}
return self;
}
/*=======================================================================
PointerList_get_length
=======================================================================*/
int PointerList_get_length (const PointerList *self)
{
int l = 0;
while (self)
{
l++;
self = self->next;
}
return l;
}
/*=======================================================================
PointerList_get_pointer
=======================================================================*/
void *PointerList_get_pointer (PointerList *self, int index)
{
int l = 0;
while (self)
{
if (index == l)
{
return self->pointer;
}
l++;
self = self->next;
}
fprintf (stderr,
"PointerList index %d out of range in get_pointer()\n", index);
return NULL;
}
/*=======================================================================
PointerList_get_const_pointer
=======================================================================*/
const void *PointerList_get_const_pointer (const PointerList *self, int index)
{
return (const PointerList *) PointerList_get_pointer
((PointerList *)self, index);
}
/*=======================================================================
PointerList_free
=======================================================================*/
void PointerList_free (PointerList *self, BOOL free_contents)
{
while (self)
{
if (free_contents)
{
if (self->pointer) free (self->pointer);
}
PointerList *next = self->next;
free (self);
self = next;
}
}