This repository was archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathso_linkedlist.c
More file actions
90 lines (67 loc) · 1.25 KB
/
so_linkedlist.c
File metadata and controls
90 lines (67 loc) · 1.25 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
#include "so_linkedlist.h"
#include "utils.h"
LinkedList createList(void)
{
LinkedList list = malloc(sizeof(struct linkedList));
DIE(list == NULL, "ENOMEM");
list->size = 0;
list->head = NULL;
return list;
}
int addElement(LinkedList list, void *data)
{
NodeList newNode, node;
if (list == NULL || data == NULL)
return 0;
newNode = calloc(1, sizeof(struct node));
newNode->next = NULL;
newNode->value = data;
node = list->head;
if (node == NULL) {
list->head = newNode;
list->size = 1;
return 0;
}
while (node->next != NULL)
node = node->next;
node->next = newNode;
list->size++;
return 0;
}
void *popElement(LinkedList list)
{
void *value;
NodeList auxNode;
DIE(list == NULL || list->size == 0, "empty list");
auxNode = list->head;
list->size--;
value = auxNode->value;
list->head = auxNode->next;
free(auxNode);
return value;
}
int getSize(LinkedList list)
{
if (list == NULL)
return -1;
return list->size;
}
void deleteList(LinkedList list)
{
NodeList auxNode;
if (list == NULL)
return;
while (list->size > 0) {
auxNode = list->head;
list->size--;
list->head = auxNode->next;
free(auxNode);
}
}
void destructList(LinkedList list)
{
if (list->size > 0)
deleteList(list);
free(list);
list = NULL;
}