This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
178 lines (123 loc) · 2.72 KB
/
lists.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
178
/*
** Fichero: lists.c
** Autores:
** Carlos Manjón García
** Miguel Sánchez González
*/
#include <stdio.h>
#include <stdlib.h>
#include "lists.h"
int createEmpty(List *l)
{
if ((l->raiz = l->ultimo = (Node *)malloc(sizeof(Node))) == NULL)
return -1;
else {
l->raiz->data = NULL;
l->raiz->next = NULL;
return 0;
}
}
int isEmpty(List *l)
{
if (l->raiz == NULL) return -1;
return (l->raiz->next == NULL);
}
int destroy(List *l)
{
if (l->raiz == NULL) return -1;
if (l->raiz->next != NULL) {
removeAll(l);
free(l->raiz);
l->raiz = l->ultimo = NULL;
} else {
free(l->raiz);
l->raiz = l->ultimo = NULL;
}
return 0;
}
idPosition previousPosition(List *l, idPosition p)
{
idPosition anterior;
if ((l->raiz == NULL) || (p == NULL)) return NULL;
if (p == l->raiz)
return l->raiz;
else {
anterior = l->raiz;
while ((anterior->next != NULL) && (anterior->next != p))
anterior = anterior->next;
return anterior;
}
}
idPosition nextPosition(List *l, idPosition p)
{
if ((l->raiz == NULL) || (p == NULL)) return NULL;
if ((l->raiz->next == NULL) || (p == l->ultimo)) return NULL;
return p->next;
}
idPosition firstPosition(List *l)
{
if (l->raiz == NULL) return NULL;
return l->raiz;
}
idPosition lastPosition(List *l)
{
if (l->raiz == NULL) return NULL;
return l->ultimo;
}
idPosition getPosition(List *l, nodeData x)
{
if (l->raiz == NULL) return NULL;
if (l->raiz->next == NULL) return NULL;
idPosition recorre = l->raiz->next;
while ((recorre->data != x) && (recorre->next != NULL))
recorre = recorre->next;
return ((recorre->data == x) ? recorre : NULL);
}
nodeData getData(List *l, idPosition p)
{
if (l->raiz == NULL || (p == NULL)) return NULL;
if ((l->raiz->next == NULL) || (p == l->ultimo)) return NULL;
return p->next->data;
}
int insertAt(List *l, nodeData x, idPosition p)
{
if ((l->raiz == NULL) || (p == NULL)) return -1;
idPosition nueva;
if ((nueva = (Node *)malloc(sizeof(Node))) == NULL)
return -3;
nueva->data = x;
nueva->next = p->next;
p->next = nueva;
if (p == l->ultimo) l->ultimo = nueva;
return 0;
}
int removeAt(List *l, idPosition p)
{
if ((l->raiz == NULL) || (p == NULL)) return -1;
if ((l->raiz->next == NULL) || (p == l->ultimo)) return -2;
idPosition sigCelda;
if (p->next != l->ultimo) {
sigCelda = p->next->next;
free(p->next->data);
free(p->next);
p->next = sigCelda;
} else {
l->ultimo = p;
free(p->next->data);
free(p->next);
p->next = NULL;
}
return 0;
}
int removeAll(List *l)
{
if (l->raiz == NULL) return -1;
idPosition aBorrar;
while(l->raiz->next != NULL) {
aBorrar = l->raiz->next;
l->raiz->next = l->raiz->next->next;
free(aBorrar->data);
free(aBorrar);
}
return 0;
}