-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
147 lines (108 loc) · 2.77 KB
/
linked_list.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "linked_list.h"
/**
* Allocate linked list in heap memory.
*
* @return pointer to empty linked list
*/
struct linked * initializeLinkedList() {
struct linked* list = (struct linked*) malloc(sizeof (struct linked));
if (list == NULL)
return NULL;
list->head = NULL;
return list;
}
/**
* Insert at end of list new allocated node, with given value.
*
* @param list pointer
* @param argsNum to insert
* @param ...
* @return boolean of action
*/
int addNode(struct linked * list, int argsNum, ...) {
int i;
va_list valist;
va_start(valist, argsNum);
struct node ** next = &(list->head);
for (i = 0; i < argsNum; i++) {
struct node * newNode = (struct node *) malloc(sizeof (struct node));
if (newNode == NULL)
return EXIT_FAILURE;
newNode->value = va_arg(valist, int);
newNode->next = NULL;
while (*next != NULL)
next = &(*next)->next;
* next = newNode;
list->size++;
}
/* clean memory reserved for valist */
va_end(valist);
return EXIT_SUCCESS;
}
/**
* Free from memory all the nodes previously allocated in list.
*
* @param list pointer
* @return boolean of action
*/
int clearNodes(struct linked * list) {
struct node * currentNode = list->head;
struct node * aux = NULL;
while (currentNode != NULL) {
aux = currentNode;
currentNode = currentNode->next;
free(aux);
}
list->head = NULL;
return EXIT_SUCCESS;
}
/**
* Free all the nodes and the list from the memory.
*
* @param list
* @return boolean
*/
int removeLinkedList(struct linked * list) {
clearNodes(list);
free(list);
return EXIT_SUCCESS;
}
/**
* Search and remove node, the comparison is made by address memory value.
*
* @param list pointer
* @param node pointer to remove
* @return boolean of action
*/
int removeNode(struct linked * list, struct node *toRemove) {
struct node ** next = &(list->head);
while (*next != NULL) {
if (*next == toRemove) {
* next = toRemove->next;
free(toRemove);
list->size--;
return EXIT_SUCCESS;
}
next = &(((*next)->next));
}
return EXIT_FAILURE;
}
/**
* Print all values from the list.
* @param pointer to list
*/
void printLinkedList(struct linked * list) {
struct node * currentNode = list->head;
while (currentNode) {
printf("%d\n", currentNode->value);
currentNode = currentNode->next;
}
}