forked from 4ndres4ngarita/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple_List.c
More file actions
270 lines (220 loc) · 5.3 KB
/
Simple_List.c
File metadata and controls
270 lines (220 loc) · 5.3 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
261
262
263
264
265
266
267
268
269
270
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int dato;
struct node *sgte;
}*nodo, tipoNodo;
typedef struct{
nodo last;
nodo first;
}*lista, tipoLista;
nodo crearNodo(int);//done
lista crearLista();//done
void insert(int, int, lista);//done
void insertfront(int, lista);//done
void insertlast(int, lista);//done
int delete(int, lista);//done (creo)
int deletefront(lista);//done
int deletelast(lista);//done (creo)
int empty(lista);//done
int size(lista);//done
int first(lista);//done
int last(lista);//done
void imprimir(){
printf("Impreso :D\n");
}
int main()
{
lista datos = crearLista();
int bit;
bit = 1;
while (bit > 0){
scanf("%d", &bit);
if (bit > 0) {
insertlast(bit, datos);
imprimir();
}
}
int pos;
bit=0;
while (bit > 0 || pos > 0){
//scanf("%d", &bit);
scanf("%d", &pos);
if (pos > 0) {
delete(pos, datos);
}
}
int n;n=0;
while(!empty(datos)){
n+=1;
printf(" valor %d: %d \n", n, deletefront(datos));
}
return 0;
}
nodo crearNodo(int valor){
nodo item = (nodo) malloc(sizeof(tipoNodo));
item->dato = valor;
item->sgte = NULL;
return item;
}
lista crearLista(){
lista List = (lista) malloc(sizeof(tipoLista));
List->last = NULL;
List->first = NULL;
return List;
}
void insert(int valor, int posicion, lista List){
int info;
if (!empty(List)){
if(posicion == 1){
insertfront( valor, List);
}else if(posicion == (size(List)+1)){
insertlast( valor, List);
}else if(posicion == 2){
nodo item = List -> first;
nodo nodoAinsertar = crearNodo(valor);
nodoAinsertar -> sgte = item -> sgte;
item -> sgte = nodoAinsertar;
}else if(posicion <= size(List)){
nodo item = List -> first;
short n=1;
while(n != (posicion-1)){
n++;
item = item -> sgte;
}
nodo nodoAinsertar = crearNodo(valor);
nodoAinsertar -> sgte = item -> sgte;
item -> sgte = nodoAinsertar;
}else{
printf("Error :(\n");
}
}else{
printf("Error :(\n");
}
}
void insertfront(int valor, lista List){
if(empty(List)){
nodo elemento = crearNodo(valor);
List -> last = elemento;
List -> first = elemento;
}else{
nodo elemento = crearNodo(valor);
elemento -> sgte = List -> first;
List -> first = elemento;
}
}
void insertlast(int valor, lista List){
if(empty(List)){
nodo elemento = crearNodo(valor);
List -> last = elemento;
List -> first = elemento;
}else{
nodo elemento = crearNodo(valor);
List -> last -> sgte = elemento;
List -> last = elemento;
}
}
int delete(int posicion, lista List){
int info;
if (!empty(List)){
if(posicion == 1){
info = deletefront(List);
}else if(posicion == size(List)){
info = deletelast(List);
}else if(posicion == 2){
nodo item = List -> first;
info = item -> sgte -> dato;//paso 1.seleccciono el dato del nodo a eliminar
nodo nodoAborrar = item -> sgte;
item -> sgte = item -> sgte -> sgte;//paso 2.apunto al que apuntaba el que va a ser eliminado
free(nodoAborrar);//paso 3. elimino el nodo
}else if(posicion <= size(List)){
nodo item = List -> first;
short n=1;
while(n != (posicion-1)){//paso 0.llego una posicion anterior
n++;
item = item -> sgte;
}
info = item -> sgte -> dato;//paso 1.seleccciono el dato del nodo a eliminar
nodo nodoAborrar = item -> sgte;
item -> sgte = item -> sgte -> sgte;//paso 2.apunto al que apuntaba el que va a ser eliminado
free(nodoAborrar);//paso 3. elimino el nodo
}else{
info = -123;
}
}else{
info = -123;
}
return info;
}
int deletefront(lista List){
int info;
if (!empty(List))
{
nodo item = List -> first;
info = item -> dato;
if(List -> first == List -> last){
List -> first = NULL;
List -> last = NULL;
}else{
List -> first = item -> sgte;
}
free(item);
}else{
info = -123;
}
return info;
}
int deletelast(lista List){
int info;
if (!empty(List))
{
nodo item = List -> first;
if(List -> first == List -> last){
info = item -> dato;
List -> first = NULL;
List -> last = NULL;
free(item);
}//else if(List -> first -> sgte == List -> last){
//info = List -> last -> dato;
//List -> last = List -> first;
//free(item -> sgte);
//}
else{
int n=1;
while(item -> sgte != List -> last){
n++;
item = item -> sgte;
}
info = item -> sgte -> dato;
List->last = item;
free(item -> sgte);
}
}else{
info = -123;
}
return info;
}
int empty(lista List){ return (List -> first == NULL && List -> last == NULL);
}
int size(lista List){
int contador = 0;
nodo actual = List->first;
while (actual != NULL){
contador++;
actual = actual->sgte;
}
return contador;
}
int first(lista List){
return List -> first -> dato;
}
int last(lista List){
return List -> last -> dato;
}
*/