-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_list.c
177 lines (149 loc) · 2.81 KB
/
sequential_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "sequential_list.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <strings.h>
List *create_list() {
List *li;
li = (List *)malloc(sizeof(struct list));
if (li != NULL) {
li->qtd = 0;
}
return li;
}
int free_list(List *li) {
if (li == NULL) {
return -1;
}
free(li);
li = create_list();
return 1;
}
int list_length(List *li) {
if (li == NULL) {
return -1;
} else {
return li->qtd;
}
}
bool is_full(List *li) {
if (li == NULL) {
return -1;
} else {
return (li->qtd == MAX);
}
}
bool is_empty(List *li) {
if (li == NULL) {
return -1;
} else {
return (li->qtd == 0);
}
}
int append_to_end(List *li, struct student st) {
if (li == NULL) {
return -1;
}
if (is_full(li)) {
return 0;
}
li->data[li->qtd] = st;
li->qtd++;
return 1;
}
int append_to_start(List *li, Student st) {
if (li == NULL)
return -1;
if (is_full(li))
return 0;
for (int i = li->qtd - 1; i >= 0; i--) {
li->data[i + 1] = li->data[i];
}
li->data[0] = st;
li->qtd++;
return 1;
}
int append_sorting(List *li, Student st) {
if (li == NULL) {
return -1;
}
if (is_full(li)) {
return -1;
}
int k, i = 0;
// Enquanto i for menor que o tamanho da lista e o numero da matricula da
// da posição i for menor que o aluno passado como parâmetro, incremente o i
while (i < li->qtd && li->data[i].register_number < st.register_number) {
i++;
}
for (k = li->qtd - 1; k >= i; k--) {
li->data[k + 1] = li->data[k];
}
li->data[i] = st;
li->qtd++;
return 1;
}
int remove_from_start(List *li) {
if (li == NULL) {
return -1;
}
if (is_empty(li)) {
return -1;
}
for (int i = 0; i < li->qtd; i++) {
li->data[i] = li->data[i + 1];
}
li->qtd--;
return 1;
}
int remove_from_end(List *li) {
if (li == NULL) {
return 0;
}
if (is_empty(li)) {
return -1;
}
li->qtd--;
return 1;
}
int remove_from_index(List *li, int idx) {
if (li == NULL) {
return -1;
}
if (idx < 0 || idx >= li->qtd) {
// index out of range.
return -1;
}
if (is_empty(li)) {
// cannot remove items from an empty list.
return -1;
}
for (int k = idx; k < li->qtd - 1; k++) {
// first K position is the item to be removed and K+1 is the next item in
// the list.
li->data[k] = li->data[k + 1];
}
li->qtd--;
return 1;
}
int get_by_index(List *li, int idx, struct student *st) {
if (li == NULL || idx >= li->qtd) {
return -1;
}
*st = li->data[idx];
return 1;
}
int get_by_register_number(List *li, int reg_number, struct student *st) {
if (li == NULL) {
return -1;
}
int i = 0;
while (i <= li->qtd - 1) {
if (reg_number == li->data[i].register_number) {
*st = li->data[i];
return 1;
}
i++;
}
return -1;
}