-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.c
162 lines (149 loc) · 3.16 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
/**
* A linked list implementation including the functionality to:
* - initialize a Node
* - append a Node to a llist
* - delete a Node from a llist
* - free a llist's memory and that of its Nodes
* - print a llist's values in head-to-tail order
*/
#include <stdlib.h>
#include <stdio.h>
#include "llist.h"
/**
* Creates a new Node with the given value.
*
* @param value The integer value for the new Node
* @return A pointer to the new Node
*/
struct Node *
init_node(int value)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->value = value;
newNode->next = NULL;
return newNode;
}
/**
* Creates a new empty Linked List.
*
* @return A pointer to the new List
*/
struct Llist *
init_llist(void)
{
struct Llist *newLlist = malloc(sizeof(struct Llist));
newLlist->head = NULL;
newLlist->tail = NULL;
newLlist->size = 0;
return newLlist;
}
/**
* Adds the given node to the end of the given linked list.
*
* @param llist The pointer to the list
* @param newNode The pointer to the new Node to be added
*/
void
append_node(struct Llist *llist, struct Node *newNode)
{
if (llist->tail == NULL) // llist is empty
{
llist->head = newNode;
llist->tail = newNode;
}
else // llist is not empty
{
llist->tail->next = newNode;
llist->tail = newNode;
}
llist->size++;
}
/**
* Deletes the first Node found with the given value from the llist and
* frees its memory.
*
* @param llist The pointer to the linked list
* @param value The value of the Node to be deleted
*/
void
delete_node(struct Llist *llist, int value)
{
// printf("Trying to delete %d...", value);
struct Node *current = llist->head;
struct Node *prev = NULL;
// Find the unwanted value
while (current != NULL && current->value != value)
{
prev = current;
current = current->next;
}
if (current == NULL) // value not found
{
return;
}
if (prev == NULL) // value found at head
{
llist->head = current->next;
}
else
{
prev->next = current->next;
}
if (current == llist->tail) // value found at tail
{
llist->tail = prev;
}
// printf("Found %d and freeing its memory!\n", current->value);
free(current);
llist->size--;
if (llist->size == 0) // llist is now empty
{
llist->head = NULL;
llist->tail = NULL;
}
}
/**
* Frees the memory of the given linked list by first
* freeing the memory of each of its Nodes in order
* before freeing the memory of the llist itself.
*
* @param llist The pointer to the llist
*/
void
cleanup_llist(struct Llist *llist)
{
struct Node *current = llist->head;
struct Node *tmp;
while (current != NULL)
{
tmp = current->next;
free(current);
current = tmp;
}
free(llist);
}
/**
* Iterates over the given llist to print out its members.
*
* @param llist The pointer to the the llist
*/
void
print_llist(struct Llist *llist)
{
struct Node *current = llist->head;
if (current == NULL)
{
printf("Llist: NULL");
}
else
{
printf("Llist: (size: %d) ", llist->size);
while (current != NULL)
{
printf("%d > ", current->value);
current = current->next;
}
}
printf("\n");
fflush(stdout);
}