-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklistlib.c
100 lines (83 loc) · 1.78 KB
/
linklistlib.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
//
// Created by Lorenzo Cardinali on 20/09/2021.
//
#include <stdio.h>
#include <stdlib.h>
#include "linklistlib.h"
/**
* Genera un nuovo nodo.
*
* @param value Valore da assegnare al nuovo nodo.
* @return Nodo creato.
*/
lklist *newNode(int value);
/**
* Rimuove la testa della lista.
*
* @param head Testa della lista.
* @return La nuova testa.
*/
lklist *remove_head(lklist *head);
lklist *add_node(lklist *head, byte value) {
if (head == NULL)
return newNode(value);
else
head->next = add_node(head->next, value);
return head;
}
lklist *remove_node(lklist *head, int index) {
if (index == 0)
return remove_head(head);
lklist *tmp, *iterator = head;
while (index != 1) {
iterator = iterator->next;
index--;
}
tmp = iterator->next->next;
free(iterator->next);
iterator->next = tmp;
return head;
}
byte get_node(lklist *head, int index) {
lklist *iterator = head;
while (index != 0) {
iterator = iterator->next;
index--;
}
return iterator->value;
}
lklist *delete_list(lklist *head) {
if (head != NULL) {
delete_list(head->next);
free(head);
}
return NULL;
}
void print_list(lklist *head) {
if (head != NULL) {
printf("%d -> ", head->value);
print_list(head->next);
} else
printf("NULL\n\n");
}
int countNodes(lklist *head) {
if (head == NULL)
return 0;
else
return 1 + countNodes(head->next);
}
/**
* # # # Funzioni di utilita' # # #
*/
lklist *newNode(int value) {
lklist *node = (lklist *) malloc(sizeof(lklist));
node->value = value;
node->next = NULL;
return node;
}
lklist *remove_head(lklist *head) {
lklist *tmp;
tmp = head->next;
free(head);
return tmp;
}