llist.c
3.17 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
/** Mark Przybocki
** Filename: LLIST.C
** Module 1 used to build liblist.a (library).
**
**/
#include "sctk.h"
static LList *getLList(void *);
static void freeLList(LList **);
void recur(LList **t, LList *f);
/*
* Function GETLNODE returns a pointer to a lnode which will
* contain the data element taken as input.
*/
static LList *getLList (void *pdata)
{
LList *ptemp; /* temporary lnode being set with data element */
/* ALLOCATE MEMORY for our TEMP LNODE */
ptemp = malloc(sizeof(LList));
if (ptemp != NULL) {
/* we have successfully allocated memory so set the LList */
ptemp -> data = pdata; /** DATA ELEMENT **/
ptemp -> next = NULL; /** NEXT LLIST NOT ASSIGNED YET **/
}
return ptemp; /** RETURNS either a Null LList or a set LList **/
}
/*
* Function FREELLIST deallocates space occupied by a LList and
* clears the LList from the list.
*/
static void freeLList (LList **p)
{
free(*p); /** DEALLOCATE SPACE **/
*p=NULL; /** Clear value of *p **/
}
/*
* Function INIT_LIST sets the starting point of a list NULL,
* which infact gives existence to a list.
*/
void LL_init (LList **s)
{
*s=NULL; /** creates a NULL list **/
}
void recur(LList **t, LList *f){
if (! LL_empty(f)){
recur(t,f->next);
LL_put_front(t,f->data);
}
}
void LL_copy (LList **t, LList **f)
{
LL_init(t);
recur(t,*f);
}
/*
* Function EMPTY test whether or not LLIST is empty.
*/
int LL_empty (LList *s)
{
return ( s==NULL ); /** Test for existing list **/
}
/*
* Function PUSH places one LList at the head (top) of the list.
*/
int LL_put_front (LList **s, void *pdata)
{
LList *ptemp; /** Temporary LList holder **/
/* Get space and set a LList */
ptemp = getLList(pdata);
/* Check for error */
if (ptemp == NULL)
return 0; /** FAILURE: Couldn't allocate space **/
/* Add to list */
ptemp -> next = *s;
/** Set the begining of the list **/
*s = ptemp;
return 1; /** SUCCESS **/
}
/*
* Function LL_put_tail places one LList at the tail (bottom) of the list.
*/
int LL_put_tail (LList **s, void *pdata)
{
LList *ptemp, *pt1 = *s; /** Temporary LList holder **/
if (*s == (LList *)0)
return LL_put_front(s,pdata);
/* Get space and set a LList */
ptemp = getLList(pdata);
/* Check for error */
if (ptemp == NULL)
return 0; /** FAILURE: Couldn't allocate space **/
/* traverse to the end of the list */
while (pt1->next != NULL)
pt1 = pt1->next;
/* Add to list */
pt1 -> next = ptemp;
ptemp->next = (LList *)NULL;
return 1; /** SUCCESS **/
}
/*
* Function LL_get_first removes a LList for the head (top) of list.
*/
int LL_get_first (LList **s, void **apdata)
{
LList *ptemp; /* Temporary LList holder */
if (LL_empty(*s) == 1)
return 0; /** list already empty nothing to pop **/
/* Set ptemp to the first LList */
ptemp = *s;
/* Reset to point to the following LList*/
*s = ptemp -> next;
/* Grab the data element */
*apdata = ptemp -> data;
/* Free the space of the popped LList */
freeLList (&ptemp);
return 1; /** Success **/
}