Repository navigation
Expand file tree
/
Copy pathDynamicList.c
More file actions
153 lines (140 loc) · 3.32 KB
/
Copy pathDynamicList.c
File metadata and controls
153 lines (140 loc) · 3.32 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
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
//
// Created by Benjamin on 2021. 04. 23..
//
#include "DynamicList.h"
DynamicList* createDL() {
DynamicList *node = (DynamicList*)calloc(1, sizeof(DynamicList));
node->next = NULL;
return node;
}
void fillList(DynamicList **front, int *array, int length) {
for(int i = 0; i < length; ++i) {
addLast(&(*front), array[i]);
}
}
void insertSorted(DynamicList **front, int value) {
DynamicList *new = createDL();
new->info = value;
if((*front) == NULL) {
(*front) = new;
return;
}
if((*front)->info > new->info) {
addFirst(&(*front), new->info);
return;
}
DynamicList *node = (*front), *help = NULL;
while(node != NULL && node->info < new->info) {
help = node;
node = node->next;
}
if(node == NULL) {
addLast(&(*front), new->info);
return;
}
new->next = node;
help->next = new;
}
void addFirst(DynamicList **front, int value) {
DynamicList *new = createDL();
new->info = value;
new->next = (*front);
(*front) = new;
}
void addLast(DynamicList **front, int value) {
DynamicList *new = createDL();
new->info = value;
new->next = NULL;
if((*front) == NULL) {
(*front) = new;
} else {
DynamicList *node = (*front);
while(node->next != NULL) {
node = node->next;
}
node->next = new;
}
}
int searchInList(DynamicList *front, int value) {
if(front == NULL) {
return 0;
}
DynamicList *node = front;
while(node->next != NULL) {
if(node->info == value) {
return 1;
} else {
node = node->next;
}
}
if(node->next == NULL && node->info != value) {
return 0;
}
}
void deleteFirst(DynamicList **front) {
if((*front) == NULL) {
printf("The list is empty!\n");
return;
}
DynamicList *node = (*front);
(*front) = (*front)->next;
freeNode(&node);
}
void deleteLast(DynamicList **front) {
if((*front) == NULL) {
printf("The list is empty!\n");
return;
}
DynamicList *node = (*front);
while (node->next->next != NULL) {
node = node->next;
}
DynamicList *temp = node->next;
node->next = NULL;
freeNode(&temp);
}
void deleteValue(DynamicList **front, int value) {
if((*front) == NULL) {
printf("The list is empty!\n");
return;
}
DynamicList *node = (*front), *help = NULL;
while(node != NULL && node->info != value) {
help = node;
node = node->next;
}
if(node != NULL) {
if(help == NULL) {
(*front) = node->next;
} else {
help->next = node->next;
}
freeNode(&node);
} else {
printf("This value isn't in the list!\n");
}
}
void deleteAll(DynamicList **front) {
while((*front) != NULL) {
DynamicList *node = (*front);
(*front) = (*front)->next;
freeNode(&node);
}
}
void freeNode(DynamicList **node) {
(*node)->info = 0;
(*node)->next = NULL;
free((*node));
}
void printList(DynamicList *front) {
if(front == NULL) {
printf("List is empty!\n");
return;
}
DynamicList *node = front;
while(node != NULL) {
printf("%d ", node->info);
node = node->next;
}
printf("\n");
}