-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.c
176 lines (149 loc) · 3.6 KB
/
llist.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
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
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include "llist.h"
/* create_list
* purpose: create a linked list structure and initialize to empty list
* inputs: none
* outputs: pointer to newly created and initialized linked list
*/
llist *create_llist()
{
llist *newlist = NULL;
newlist = (llist *)malloc(sizeof(llist));
newlist->head = NULL;
newlist->tail = NULL;
return newlist;
}
/* insert_head
* purpose: insert an item to become the first item in a linked list.
* inputs:
* list - pointer to llist struct in which to insert item
* item - a pointer to the item to be inserted into the linked list
* return: nothing
*/
void insert_head(llist *list, void *item)
{
node *fst = NULL;
fst = (node *)malloc(sizeof(node));
fst->item = item;
fst->next = list->head;
list->head = fst;
// if tail is NULL
if (list->tail == NULL)
list->tail = fst;
}
/* remove_head
* purpose: remove the item that is at the beginning of a linked list
* inputs:
* list - pointer to llist struct in which to insert item
* return:
*/
void remove_head(llist *list)
{
if (list->head == NULL)
return;
else if (list->head->next == NULL)
{
list->head->item = NULL;
list->tail->item = NULL;
list->head = NULL;
list->tail = NULL;
return;
}
// if more than one elements in the list, draw picture
list->head->item = NULL;
list->head = list->head->next;
}
/* peek_head
* purpose: return the first item in a linked list
* inputs:
* list - pointer to llist struct in which to insert item
* return:
* void * - the first item stored in the linked list
*/
void *peek_head(llist *list)
{
if (list->head == NULL)
return NULL;
else
return (list->head->item);
}
/* insert_tail
* purpose: insert an item to become the last item in a linked list.
* inputs:
* list - pointer to llist struct in which to insert item
* item - a pointer to the item to be inserted into the linked list
* return: nothing
*/
void insert_tail(llist *list, void *item)
{
// if original list is empty
if (list->head == NULL)
{
insert_head(list,item);
return;
}
// if the original list is not empty
node *last = NULL;
last = (node *)malloc(sizeof(node));
last->item = item;
last->next = NULL;
list->tail->next = last; // the old tail
list->tail = last; // now update tail to last
}
/* remove_tail
* purpose: remove the item that is at the beginning of a linked list
* inputs:
* list - pointer to llist struct in which to insert item
* return: nothing
*/
void remove_tail(llist *list)
{
// if llist empty
if (list->head == NULL)
return;
// if only one element
if (list->head->next == NULL)
{
list->head->item = NULL;
list->tail->item = NULL;
list->tail->next = NULL;
list->head = NULL;
list->tail = NULL;
return;
}
// if there are more than one element
list->tail->item = NULL;
list->tail = NULL;
// make tail pointer to previous one
node *tmp;
for (tmp = list->head; (tmp->next != NULL) && (tmp != NULL) &&
(tmp->next->next != NULL); tmp = tmp->next)
;
list->tail = tmp;
list->tail->next = NULL;
}
/* peek_tail
* purpose: return the last item in a linked list
* inputs:
* list - pointer to llist struct in which to insert item
* return:
* void * - the last item stored in the linked list (which is a pointer)
*/
void *peek_tail(llist *list)
{
if (list->tail == NULL)
return NULL;
else
return list->tail->item;
}
/* list_is_empty
* purpose: return a pseudo-boolean indicating whether or not the
* list is empty
* input: list
* output: 0 if not empty, nonzero if empty
*/
int list_is_empty(llist *list)
{
return ((list->head == NULL) && (list->tail == NULL));
}