-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencial-linear-list.c
112 lines (83 loc) · 2.06 KB
/
sequencial-linear-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
#define MAX_LENGTH 50
#include <stdio.h>
#include <stdlib.h>
typedef int KEY_TYPE;
typedef struct {
KEY_TYPE key;
} Registration;
typedef struct {
Registration array[MAX_LENGTH];
int length;
} List;
List* initialize(List* list) {
list->length = 0;
return list;
}
int totalElementsOf(List* list) {
return list->length;
}
void printElementsOf(List* list) {
if(list->length > 0) {
for (int i = 0; i < list->length; i++)
{
printf("Element: %i \n", list->array[i].key);
}
} else {
printf("List is empty, try to add some elements to it.");
}
}
int findElementIndexOf(List* list, KEY_TYPE key) {
if(list->length > 0) {
for (int i = 0; i < list->length; i++)
{
if(list->array[i].key == key) {
return i;
}
}
}
return -1;
}
void addElementTo(List* list, Registration element) {
list->array[totalElementsOf(list)] = element;
list->length += 1;
}
bool removeElementOf(List* list, KEY_TYPE key) {
int index = findElementIndexOf(list, key);
if(index == -1) {
return false;
}
if(index == list->length - 1) {
list->length -= 1;
return true;
}
if(index <= list->length - 1) {
for (int i = index; i < list->length; i++) {
list->array[i] = list->array[i + 1];
}
list->length -= 1;
return true;
}
return false;
}
restartList(List* list) {
list->length = 0;
list->array = [];
}
int main()
{
List* list = initialize((List*) malloc(sizeof(List)));
for (int i = 0; i < 20; i++)
{
Registration registration;
registration.key = 4 * i;
addElementTo(list, registration);
}
printf("LENGTH: %i \n", totalElementsOf(list));
// printElementsOf(list);
KEY_TYPE key = list->array[findElementIndexOf(list, 68)].key;
printf("Key: %i \n", key);
removeElementOf(list, key);
printElementsOf(list);
printf("%i", totalElementsOf(list));
return 0;
}