-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista.c
72 lines (58 loc) · 1.39 KB
/
lista.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
#include "lista.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool createNode(Pos *p){
*p = malloc(sizeof(struct Node));
return (*p != NULL);
}
/* Crear la lista */
void createlist (List *head){
Pos p;
if(createNode(&p)){
*head = p;
p->next = NULL;
}
}
/* Primero de la lista */
Pos first(List head){
return head->next;
}
/* Ultimo de la lista */
Pos last(List head){
Pos aux;
for (aux = head; aux -> next != NULL; aux = aux -> next);
return aux;
}
/* Insertar en la lista */
bool insertItem (char *com, List *head){
Pos q, aux;
int i = 1;
if(*head == NULL) // No se puede insertar
return false;
if(!createNode(&q))
return false;
else {
strcpy(q->data.comando, com);
q->next = NULL;
for (aux = *head; aux -> next != NULL; aux = aux -> next, i++); // Insertar al final de la lista
aux -> next = q;
q -> data.n = i;
}
return true;
}
/* Buscar el elemento numero n */
char *find_n(int n, List *head){
Pos p;
for(p = *head; p->data.n != n; p = p -> next);
return (p -> data.comando);
}
/* Borrar la lista */
void deleteList(List *head){
Pos p;
while(*head != NULL){ // Vamos borrando cada elemento uno a uno
p = *head;
*head = (*head)->next; // Vamos desplazando p
free(p); // Liberamos memoria de p
}
}