-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathList.c
69 lines (54 loc) · 1.23 KB
/
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
/* List.c */
#include "List.h"
List * listNew()
{
List * newList = (List *) malloc(sizeof(List));
newList->head = NULL;
newList->tail = NULL;
newList->quantity = 0;
return newList;
}
Node * nodeNew(int value)
{
Node * newNode = (Node *) malloc(sizeof(Node));
newNode->next = NULL;
newNode->value = value;
return newNode;
}
Node * nodeFind(List * list, int value)
{
Node * currentNode = list->head;
while (currentNode != NULL) {
if (currentNode->value == value) {
break;
}
currentNode = currentNode->next;
}
return currentNode;
}
void listInsert(List * list, int value)
{
if (nodeFind(list, value) == NULL) {
Node * newNode = nodeNew(value);
if (list->tail != NULL) {
list->tail->next = newNode;
}
list->tail = newNode;
if (list->head == NULL) {
list->head = newNode;
}
list->quantity++;
}
}
void listPrint(List * list)
{
Node * currentNode = list->head;
while (currentNode != NULL) {
printf("%d", currentNode->value);
currentNode = currentNode->next;
if (currentNode != NULL) {
printf(" ");
}
}
printf("\n");
}