-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.h
More file actions
260 lines (225 loc) · 5.39 KB
/
linkedList.h
File metadata and controls
260 lines (225 loc) · 5.39 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* AUTOR: Luis Eduardo Galindo Amaya FECHA: 26-02-2023
*
* DESCRIPCIÓN:
* En este archivo se almacenan todos los métodos para controlar una lista
* enlazada.
*/
#ifndef linkedList
#define linkedList
#include "linkedListNode.h"
#include <stdlib.h>
typedef struct LinkedList
{
struct LinkedListNode *nodes;
int size;
} LinkedList;
/**
* Longitud de la lista enlazada
* @param lista direccion a la lista
* @return int
*/
int linkedListLenght(LinkedList *list)
{
return list->size;
}
/**
* Devuelve verdadero si la lista esta vacía
* @param list direccion a una lista
* @return boolean
*/
short linkedListEmpty(LinkedList *list)
{
return list->nodes == NULL;
}
/**
* Devuelve verdadero si la posición NO es negativa y
* existe dentro el arreglo
* @param
* @return
*/
short linkedListInvalidPosition(LinkedList *list, int position)
{
/* tamaño es inferior a la posicion (hay menos elementos que la
posicion solicitada), la posicion es un valor negativo */
return position > linkedListLenght(list) || position < 0;
}
/**
* Inicializar el arreglo
* @param list dirección a una lista
*/
void linkedListInit(LinkedList *list)
{
list->nodes = NULL;
list->size = 0;
}
/**
* Liberar un nodo de la lista que ya no se va a usar
* @param node direccion del nodo
*/
void freeLinkedListNode(LinkedListNode *node)
{
if (node == NULL)
{
printf("freeLinkedListNode: El nodo esta vacio\n");
exit(EXIT_FAILURE);
}
free(node);
}
/**
* Obtener un nodo de la lista
* @param position
* @return LinkedListNode
*/
LinkedListNode *linkedListGet(LinkedList *list, int position)
{
LinkedListNode *current = list->nodes; /* primer elemento */
if (linkedListInvalidPosition(list, position))
{
/* Si la posición solicitada es mas grande que la cantidad
de nodo disponibles entonces termina */
printf("linkedListGet: Posición fuera de rango!!\n");
exit(EXIT_FAILURE);
}
if (linkedListEmpty(list))
{
/* No se puede extraer valores de una lista vacía */
printf("linkedListGet: La lista esta vacia!!\n");
exit(EXIT_FAILURE);
}
if (position == 0)
{
/* Primer elemento de la lista */
return list->nodes;
}
for (int i = 0; i < position; i++)
{
/* caso generico */
current = current->next;
}
return current;
}
/**
* Insertar elementos en la lista
* @param list dirección a una lista
* @param node dirección a un nodo
* @param position posición donde insertar el elemento
*/
void linkedListInsert(LinkedList *list, LinkedListNode *node, int position)
{
LinkedListNode *new = node;
LinkedListNode *current = list->nodes; /* primer elemento */
LinkedListNode *previous = NULL;
if (linkedListEmpty(list))
{
/* si no hay elementos en la lista inserta en la primera posición */
list->nodes = node;
list->size++;
return;
}
if (linkedListInvalidPosition(list, position))
{
/* Si la posición solicitada es mas grande que la cantidad
de nodo disponibles o la posicion es negativa entonces termina */
printf("linkedListInsert: Posición %d fuera de rango!!\n", position);
exit(EXIT_FAILURE);
}
if (position == 0)
{
/* insertar al inicio de la lista */
new->next = current;
list->nodes = new;
list->size++;
return;
}
for (int i = 0; i < position; i++)
{
/* caso generico */
previous = current;
current = current->next;
}
previous->next = new;
new->next = current;
list->size++;
}
/**
* Eliminar elemento en una posicion
* @param list direccion a la lista
* @param position posición del elemeto
*/
void linkedListRemove(LinkedList *list, int position)
{
LinkedListNode *current = list->nodes; /* primer elemento */
LinkedListNode *previous = NULL;
if (linkedListEmpty(list))
{
/* No se puede extraer valores de una lista vacía */
printf("linkedListRemove: La lista esta vacia!!\n");
exit(EXIT_FAILURE);
}
if (linkedListInvalidPosition(list, position))
{
/* no valores fuera de rango o negativos */
printf("linkedListRemove: Posición fuera de rango!!\n");
exit(EXIT_FAILURE);
}
if (position == linkedListLenght(list))
{
printf("linkedListRemove: Posición fuera de rango!!\n");
exit(EXIT_FAILURE);
}
if (linkedListLenght(list) == 1)
{
/* si solo hay un elemento en lista */
free(list->nodes);
list->nodes = NULL;
list->size = 0;
return;
}
if (position == 0)
{
/* si se quiere eliminar el primer elemento */
list->nodes = current->next;
free(current);
list->size--;
return;
}
for (int i = 0; i < position; i++)
{
/* caso generico */
previous = current;
current = current->next;
}
previous->next = current->next;
free(current);
list->size--;
}
/**
* eliminar el ultimo elemento de la lista
* @param list direccion a la lista
*/
void linkedListRemoveLast(LinkedList *list)
{
linkedListRemove(list, linkedListLenght(list) - 1);
}
/**
* inserta un elemento al final de la lista
* @param list direccio de la lista
* @param node direccion del nuevo nodo
*/
void linkedListInsertLast(LinkedList *list, LinkedListNode *node)
{
linkedListInsert(list, node, linkedListLenght(list));
}
/**
* Liberar memoria del la lista enlazada
* @param list direccion de la lista
*/
void linkedListFree(LinkedList *list)
{
for (int i = linkedListLenght(list); i > 0; i--)
{
linkedListRemove(list, 0);
}
}
#endif