1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <locale.h>
4
5 struct Node
6 {
7 int num;
8 struct Node *prox;
9 };
10 typedef struct Node node;
11
12 int tam, x;
13
14 void inicia(node *LISTA);
15 int menu(void);
16 void opcao(node *LISTA, int op);
17 node *criaNo();
18 void insereFim(node *LISTA);
19 void insereInicio(node *LISTA);
20 void exibe(node *LISTA);
21 void libera(node *LISTA);
22 void insere (node *LISTA);
23 node *retiraInicio(node *LISTA);
24 node *retiraFim(node *LISTA);
25 node *retira(node *LISTA);
26 node *buscar(node *LISTA);
27 void ordemCrescente(node *LISTA);
28 void atualizar(node *LISTA);
29 void pesquisaMaior(node *LISTA);
30 void pesquisaMenor(node *LISTA);
31 void imprimir(node *LISTA);
32
33 int main(void)
34 {
35 setlocale(LC_ALL, "Portuguese");
36 node *LISTA = (node *) malloc(sizeof(node));
37 if(!LISTA)
38 {
39 printf("nSem memória disponível!n");
40 exit(1);
41 }
42 else
43 {
44 inicia(LISTA);
45 int opt;
46
47 do
48 {
49 opt=menu();
50 opcao(LISTA,opt);
51 }
52 while(opt);
53
54 free(LISTA);
55 return 0;
56 }
57 }
58
59 void inicia(node *LISTA)
60 {
61 LISTA->num = 0;
62 LISTA->prox = NULL;
63 tam=0;
64 }
65
66 int menu(void)
67 {
68 int opt;
69
70 printf("nn============================================================n");
71 printf(" Escolha a opcaon");
72 printf(" 0. Sairn");
73 printf(" 1. Zerar listan");
74 printf(" 2. Imprimir listan");
75 printf(" 3. Inserir no inicion");
76 printf(" 4. Inserir no finaln");
77 printf(" 5. Escolher onde inserirn");
78 printf(" 6. Remover do inicion");
79 printf(" 7. Remover do fimn");
80 printf(" 8. Escolher de onde tirarn");
81 printf(" 9. Buscarn");
82 printf("10. Inserir em Ordem Crescenten");
83 printf("11. Atualizar um elemento da lista.n");
84 printf("12. Encontrar o MAIOR elemento da lista.n");
85 printf("13. Encontrar o MENOR elemento da lista.n");
86 printf("14. Quantidade de NÓS da lista.n");
87 printf("Opcao: ");
88 scanf("%d", &opt);
89
90 return opt;
91 }
92
93 void opcao(node *LISTA, int op)
94 {
95 node *tmp;
96 switch(op)
97 {
98 case 0:
99 libera(LISTA);
100 break;
101
102 case 1:
103 libera(LISTA);
104 inicia(LISTA);
105 break;
106
107 case 2:
108 if(vazia(LISTA))
109 printf("nA lista está vazia! ");
110 else
111 //exibe(LISTA);
112 imprimir(LISTA);
113 break;
114
115 case 3:
116 insereInicio(LISTA);
117 break;
118
119 case 4:
120 insereFim(LISTA);
121 break;
122
123 case 5:
124 insere(LISTA);
125 break;
126
127 case 6:
128 if(vazia(LISTA))
129 printf("nA lista está vazia! ");
130 else
131 {
132 tmp= retiraInicio(LISTA);
133 printf("nRetirado: %3d.n", tmp->num);
134 }
135
136 break;
137
138 case 7:
139 if(vazia(LISTA))
140 printf("nA lista está vazia! ");
141 else
142 {
143 tmp= retiraFim(LISTA);
144 printf("nRetirado: %3d. n", tmp->num);
145 }
146 break;
147
148 case 8:
149 if(vazia(LISTA))
150 printf("nA lista está vazia! ");
151 else
152 {
153 tmp= retira(LISTA);
154 printf("nRetirado: %3d. n", tmp->num);
155 }
156
157 break;
158
159 case 9:
160 if(vazia(LISTA))
161 printf("nA lista está vazia! ");
162 else
163 {
164 tmp = buscar(LISTA);
165 if(tmp == NULL)
166 {
167 printf("nO valor NÃO foi encontrado. ");
168 }
169 else
170 {
171 printf("nO valor foi encontrado: %d", tmp->num);
172 }
173 }
174 break;
175
176 case 10:
177 if(vazia(LISTA))
178 printf("nA lista está vazia! ");
179 else
180 ordemCrescente(LISTA);
181 break;
182
183 case 11:
184 if(vazia(LISTA))
185 printf("nA lista está vazia! ");
186 else
187 atualizar(LISTA);
188 break;
189
190 case 12:
191 if(vazia(LISTA))
192 printf("nA lista está vazia! ");
193 else
194 pesquisaMaior(LISTA);
195 break;
196
197 case 13:
198 if(vazia(LISTA))
199 printf("nA lista está vazia! ");
200 else
201 pesquisaMenor(LISTA);
202 break;
203
204 case 14:
205 if(vazia(LISTA))
206 printf("nA lista está vazia! ");
207 else
208 printf("nA quantidade total de NÓS desta lista é: %d", tam);
209 break;
210
211 default:
212 printf("nComando inválido! n");
213 }
214 }
215
216 int vazia(node *LISTA)
217 {
218 if(LISTA->prox == NULL)
219 return 1;
220 else
221 return 0;
222 }
223
224 node *aloca()
225 {
226 node *novo=(node *) malloc(sizeof(node));
227 if(!novo)
228 {
229 printf("nSem memória disponível! n");
230 exit(1);
231 }
232 else
233 {
234 printf("nNovo elemento: ");
235 scanf("%d", &novo->num);
236 return novo;
237 }
238 }
239
240
241 void insereFim(node *LISTA)
242 {
243 node *novo = aloca();
244 novo->prox = NULL;
245
246 if(vazia(LISTA))
247 LISTA->prox=novo;
248 else
249 {
250 node *tmp = LISTA->prox;
251
252 while(tmp->prox != NULL)
253 tmp = tmp->prox;
254
255 tmp->prox = novo;
256 }
257 tam++;
258 }
259
260 void insereInicio(node *LISTA)
261 {
262 node *novo=aloca();
263 node *aux = LISTA->prox;
264 LISTA->prox = novo;
265 novo->prox = aux;
266 tam++;
267 }
268
269 void imprimir(node *LISTA)
270 {
271 if(vazia(LISTA))
272 {
273 printf("n Lista vazia! n");
274 return ;
275 }
276 node *tmp;
277 tmp = LISTA->prox;
278 printf("nLista:");
279 while( tmp != NULL)
280 {
281 printf("%5d", tmp->num);
282 tmp = tmp->prox;
283 }
284 printf("nn");
285 }
286
287 void exibe(node *LISTA)
288 {
289 system("clear");
290 if(vazia(LISTA))
291 {
292 printf("n Lista vazia! n");
293 return ;
294 }
295 node *tmp;
296 tmp = LISTA->prox;
297 printf("nLista:");
298 while( tmp != NULL)
299 {
300 printf("%5d", tmp->num);
301 tmp = tmp->prox;
302 }
303 printf("n ");
304 int count;
305 for(count=0 ; count < tam ; count++)
306 printf(" ^ ");
307 printf("nOrdem:");
308 for(count=0 ; count < tam ; count++)
309 printf("%5d", count+1);
310 printf("nn");
311 }
312
313 void libera(node *LISTA)
314 {
315 if(!vazia(LISTA))
316 {
317 node *proxNode,
318 *atual;
319
320 atual = LISTA->prox;
321 while(atual != NULL)
322 {
323 proxNode = atual->prox;
324 free(atual);
325 atual = proxNode;
326 }
327 }
328 }
329
330 void insere(node *LISTA)
331 {
332 int pos, count;
333 printf("nEm que posicao, [de 1 ate %d] voce deseja inserir: ", tam);
334 scanf("%d", &pos);
335
336 if(pos>0 && pos <= tam)
337 {
338 if(pos==1)
339 insereInicio(LISTA);
340 else
341 {
342 node *atual = LISTA->prox,
343 *anterior=LISTA;
344 node *novo=aloca();
345
346 for(count=1 ; count < pos ; count++)
347 {
348 anterior=atual;
349 atual=atual->prox;
350 }
351 anterior->prox=novo;
352 novo->prox = atual;
353 tam++;
354 }
355
356 }
357 else
358 printf("nElemento inválido! n");
359 }
360
361 node *retiraInicio(node *LISTA)
362 {
363 if(LISTA->prox == NULL)
364 {
365 printf("nLista vazia! n");
366 return NULL;
367 }
368 else
369 {
370 node *aux = LISTA->prox;
371 LISTA->prox = aux->prox;
372 tam--;
373 return aux;
374 }
375
376 }
377
378 node *retiraFim(node *LISTA)
379 {
380 if(LISTA->prox == NULL)
381 {
382 printf("nLista vazia! n");
383 return NULL;
384 }
385 else
386 {
387 node *ultimo = LISTA->prox, *penultimo = LISTA;
388 while(ultimo->prox != NULL)
389 {
390 penultimo = ultimo;
391 ultimo = ultimo->prox;
392 }
393 penultimo->prox = NULL;
394 tam--;
395 return ultimo;
396 }
397 }
398
399 node *retira(node *LISTA)
400 {
401 int opt,count;
402 printf("nQue posicao, [de 1 ate %d] voce deseja retirar: ", tam);
403 scanf("%d", &opt);
404
405 if(opt>0 && opt <= tam)
406 {
407 if(opt==1)
408 return retiraInicio(LISTA);
409 else
410 {
411 node *atual = LISTA->prox,
412 *anterior = LISTA;
413
414 for(count=1 ; count < opt ; count++)
415 {
416 anterior = atual;
417 atual = atual->prox;
418 }
419
420 anterior->prox = atual->prox;
421 tam--;
422 return atual;
423 }
424
425 }
426 else
427 {
428 printf("n Elemento inválido! n");
429 return NULL;
430 }
431 }
432
433 node *buscar(node *LISTA)
434 {
435 printf("nDigite um valor a ser procurado: ");
436 scanf("%d", &x);
437 node *aux = LISTA;
438 while((aux != NULL) && (aux->num != x))
439 {
440 aux = aux->prox;
441 }
442 return aux;
443 }
444
445 void ordemCrescente(node *LISTA)
446 {
447 node *aux1 = LISTA;
448 node *aux2 = LISTA;
449 node *novo = aloca();
450 novo->prox = NULL;
451 int i=0;
452
453 while((aux2!=NULL) && (aux2->num < novo->num))
454 {
455 printf("n ENTREI NO WHILE %d. n", i++);
456 aux1 = aux2;
457 aux2 = aux2->prox;
458 }
459
460 if(aux1 == aux2)
461 {
462 printf("n ENTREI NO AUX1 = AUX2 n");
463 novo->prox = aux1;
464 LISTA = novo;
465 }
466 else
467 {
468 printf("n ENTREI NO AUX1 != AUX2 n");
469 novo->prox = aux1->prox;
470 aux1->prox = novo;
471 }
472
473 }
474
475 void atualizar(node *LISTA)
476 {
477 node *aux = buscar(LISTA);
478 if(aux == NULL)
479 {
480 printf("n O valor NÃO foi encontrado. n");
481 }
482 else
483 {
484 printf("n O valor foi encontrado: %d n", aux->num);
485 printf("n Digite o novo valor: ");
486 scanf("%d", &aux->num);
487 printf("n O valor foi atualizado com sucesso! n");
488 }
489 }
490
491
492 void pesquisaMaior(node *LISTA)
493 {
494 int maior = LISTA->num;
495 while(LISTA != NULL)
496 {
497 if(LISTA->num > maior)
498 maior = LISTA->num;
499 LISTA = LISTA->prox;
500 }
501 printf("nO maior elemento da lista é: %d. n", maior);
502 }
503
504 void pesquisaMenor(node *LISTA)
505 {
506 LISTA = LISTA->prox;
507 int menor = LISTA->num;
508 while(LISTA != NULL)
509 {
510 if(LISTA->num < menor)
511 menor = LISTA->num;
512 LISTA = LISTA->prox;
513 }
514 printf("nO menor elemento da lista é: %d. n", menor);
515 }
516

Lista simplesmente encadeada

  • 1.
    1 #include <stdio.h> 2#include <stdlib.h> 3 #include <locale.h> 4 5 struct Node 6 { 7 int num; 8 struct Node *prox; 9 }; 10 typedef struct Node node; 11 12 int tam, x; 13 14 void inicia(node *LISTA); 15 int menu(void); 16 void opcao(node *LISTA, int op); 17 node *criaNo(); 18 void insereFim(node *LISTA); 19 void insereInicio(node *LISTA); 20 void exibe(node *LISTA); 21 void libera(node *LISTA); 22 void insere (node *LISTA); 23 node *retiraInicio(node *LISTA); 24 node *retiraFim(node *LISTA); 25 node *retira(node *LISTA); 26 node *buscar(node *LISTA); 27 void ordemCrescente(node *LISTA); 28 void atualizar(node *LISTA); 29 void pesquisaMaior(node *LISTA); 30 void pesquisaMenor(node *LISTA); 31 void imprimir(node *LISTA); 32 33 int main(void) 34 { 35 setlocale(LC_ALL, "Portuguese"); 36 node *LISTA = (node *) malloc(sizeof(node)); 37 if(!LISTA) 38 { 39 printf("nSem memória disponível!n"); 40 exit(1); 41 } 42 else 43 { 44 inicia(LISTA); 45 int opt; 46 47 do 48 { 49 opt=menu(); 50 opcao(LISTA,opt); 51 } 52 while(opt); 53 54 free(LISTA); 55 return 0; 56 } 57 } 58 59 void inicia(node *LISTA) 60 { 61 LISTA->num = 0; 62 LISTA->prox = NULL; 63 tam=0; 64 } 65 66 int menu(void) 67 { 68 int opt; 69 70 printf("nn============================================================n"); 71 printf(" Escolha a opcaon"); 72 printf(" 0. Sairn"); 73 printf(" 1. Zerar listan"); 74 printf(" 2. Imprimir listan"); 75 printf(" 3. Inserir no inicion"); 76 printf(" 4. Inserir no finaln"); 77 printf(" 5. Escolher onde inserirn"); 78 printf(" 6. Remover do inicion"); 79 printf(" 7. Remover do fimn"); 80 printf(" 8. Escolher de onde tirarn"); 81 printf(" 9. Buscarn"); 82 printf("10. Inserir em Ordem Crescenten"); 83 printf("11. Atualizar um elemento da lista.n"); 84 printf("12. Encontrar o MAIOR elemento da lista.n");
  • 2.
    85 printf("13. Encontraro MENOR elemento da lista.n"); 86 printf("14. Quantidade de NÓS da lista.n"); 87 printf("Opcao: "); 88 scanf("%d", &opt); 89 90 return opt; 91 } 92 93 void opcao(node *LISTA, int op) 94 { 95 node *tmp; 96 switch(op) 97 { 98 case 0: 99 libera(LISTA); 100 break; 101 102 case 1: 103 libera(LISTA); 104 inicia(LISTA); 105 break; 106 107 case 2: 108 if(vazia(LISTA)) 109 printf("nA lista está vazia! "); 110 else 111 //exibe(LISTA); 112 imprimir(LISTA); 113 break; 114 115 case 3: 116 insereInicio(LISTA); 117 break; 118 119 case 4: 120 insereFim(LISTA); 121 break; 122 123 case 5: 124 insere(LISTA); 125 break; 126 127 case 6: 128 if(vazia(LISTA)) 129 printf("nA lista está vazia! "); 130 else 131 { 132 tmp= retiraInicio(LISTA); 133 printf("nRetirado: %3d.n", tmp->num); 134 } 135 136 break; 137 138 case 7: 139 if(vazia(LISTA)) 140 printf("nA lista está vazia! "); 141 else 142 { 143 tmp= retiraFim(LISTA); 144 printf("nRetirado: %3d. n", tmp->num); 145 } 146 break; 147 148 case 8: 149 if(vazia(LISTA)) 150 printf("nA lista está vazia! "); 151 else 152 { 153 tmp= retira(LISTA); 154 printf("nRetirado: %3d. n", tmp->num); 155 } 156 157 break; 158 159 case 9: 160 if(vazia(LISTA)) 161 printf("nA lista está vazia! "); 162 else 163 { 164 tmp = buscar(LISTA); 165 if(tmp == NULL) 166 { 167 printf("nO valor NÃO foi encontrado. "); 168 }
  • 3.
    169 else 170 { 171printf("nO valor foi encontrado: %d", tmp->num); 172 } 173 } 174 break; 175 176 case 10: 177 if(vazia(LISTA)) 178 printf("nA lista está vazia! "); 179 else 180 ordemCrescente(LISTA); 181 break; 182 183 case 11: 184 if(vazia(LISTA)) 185 printf("nA lista está vazia! "); 186 else 187 atualizar(LISTA); 188 break; 189 190 case 12: 191 if(vazia(LISTA)) 192 printf("nA lista está vazia! "); 193 else 194 pesquisaMaior(LISTA); 195 break; 196 197 case 13: 198 if(vazia(LISTA)) 199 printf("nA lista está vazia! "); 200 else 201 pesquisaMenor(LISTA); 202 break; 203 204 case 14: 205 if(vazia(LISTA)) 206 printf("nA lista está vazia! "); 207 else 208 printf("nA quantidade total de NÓS desta lista é: %d", tam); 209 break; 210 211 default: 212 printf("nComando inválido! n"); 213 } 214 } 215 216 int vazia(node *LISTA) 217 { 218 if(LISTA->prox == NULL) 219 return 1; 220 else 221 return 0; 222 } 223 224 node *aloca() 225 { 226 node *novo=(node *) malloc(sizeof(node)); 227 if(!novo) 228 { 229 printf("nSem memória disponível! n"); 230 exit(1); 231 } 232 else 233 { 234 printf("nNovo elemento: "); 235 scanf("%d", &novo->num); 236 return novo; 237 } 238 } 239 240 241 void insereFim(node *LISTA) 242 { 243 node *novo = aloca(); 244 novo->prox = NULL; 245 246 if(vazia(LISTA)) 247 LISTA->prox=novo; 248 else 249 { 250 node *tmp = LISTA->prox; 251 252 while(tmp->prox != NULL)
  • 4.
    253 tmp =tmp->prox; 254 255 tmp->prox = novo; 256 } 257 tam++; 258 } 259 260 void insereInicio(node *LISTA) 261 { 262 node *novo=aloca(); 263 node *aux = LISTA->prox; 264 LISTA->prox = novo; 265 novo->prox = aux; 266 tam++; 267 } 268 269 void imprimir(node *LISTA) 270 { 271 if(vazia(LISTA)) 272 { 273 printf("n Lista vazia! n"); 274 return ; 275 } 276 node *tmp; 277 tmp = LISTA->prox; 278 printf("nLista:"); 279 while( tmp != NULL) 280 { 281 printf("%5d", tmp->num); 282 tmp = tmp->prox; 283 } 284 printf("nn"); 285 } 286 287 void exibe(node *LISTA) 288 { 289 system("clear"); 290 if(vazia(LISTA)) 291 { 292 printf("n Lista vazia! n"); 293 return ; 294 } 295 node *tmp; 296 tmp = LISTA->prox; 297 printf("nLista:"); 298 while( tmp != NULL) 299 { 300 printf("%5d", tmp->num); 301 tmp = tmp->prox; 302 } 303 printf("n "); 304 int count; 305 for(count=0 ; count < tam ; count++) 306 printf(" ^ "); 307 printf("nOrdem:"); 308 for(count=0 ; count < tam ; count++) 309 printf("%5d", count+1); 310 printf("nn"); 311 } 312 313 void libera(node *LISTA) 314 { 315 if(!vazia(LISTA)) 316 { 317 node *proxNode, 318 *atual; 319 320 atual = LISTA->prox; 321 while(atual != NULL) 322 { 323 proxNode = atual->prox; 324 free(atual); 325 atual = proxNode; 326 } 327 } 328 } 329 330 void insere(node *LISTA) 331 { 332 int pos, count; 333 printf("nEm que posicao, [de 1 ate %d] voce deseja inserir: ", tam); 334 scanf("%d", &pos); 335 336 if(pos>0 && pos <= tam)
  • 5.
    337 { 338 if(pos==1) 339insereInicio(LISTA); 340 else 341 { 342 node *atual = LISTA->prox, 343 *anterior=LISTA; 344 node *novo=aloca(); 345 346 for(count=1 ; count < pos ; count++) 347 { 348 anterior=atual; 349 atual=atual->prox; 350 } 351 anterior->prox=novo; 352 novo->prox = atual; 353 tam++; 354 } 355 356 } 357 else 358 printf("nElemento inválido! n"); 359 } 360 361 node *retiraInicio(node *LISTA) 362 { 363 if(LISTA->prox == NULL) 364 { 365 printf("nLista vazia! n"); 366 return NULL; 367 } 368 else 369 { 370 node *aux = LISTA->prox; 371 LISTA->prox = aux->prox; 372 tam--; 373 return aux; 374 } 375 376 } 377 378 node *retiraFim(node *LISTA) 379 { 380 if(LISTA->prox == NULL) 381 { 382 printf("nLista vazia! n"); 383 return NULL; 384 } 385 else 386 { 387 node *ultimo = LISTA->prox, *penultimo = LISTA; 388 while(ultimo->prox != NULL) 389 { 390 penultimo = ultimo; 391 ultimo = ultimo->prox; 392 } 393 penultimo->prox = NULL; 394 tam--; 395 return ultimo; 396 } 397 } 398 399 node *retira(node *LISTA) 400 { 401 int opt,count; 402 printf("nQue posicao, [de 1 ate %d] voce deseja retirar: ", tam); 403 scanf("%d", &opt); 404 405 if(opt>0 && opt <= tam) 406 { 407 if(opt==1) 408 return retiraInicio(LISTA); 409 else 410 { 411 node *atual = LISTA->prox, 412 *anterior = LISTA; 413 414 for(count=1 ; count < opt ; count++) 415 { 416 anterior = atual; 417 atual = atual->prox; 418 } 419 420 anterior->prox = atual->prox;
  • 6.
    421 tam--; 422 returnatual; 423 } 424 425 } 426 else 427 { 428 printf("n Elemento inválido! n"); 429 return NULL; 430 } 431 } 432 433 node *buscar(node *LISTA) 434 { 435 printf("nDigite um valor a ser procurado: "); 436 scanf("%d", &x); 437 node *aux = LISTA; 438 while((aux != NULL) && (aux->num != x)) 439 { 440 aux = aux->prox; 441 } 442 return aux; 443 } 444 445 void ordemCrescente(node *LISTA) 446 { 447 node *aux1 = LISTA; 448 node *aux2 = LISTA; 449 node *novo = aloca(); 450 novo->prox = NULL; 451 int i=0; 452 453 while((aux2!=NULL) && (aux2->num < novo->num)) 454 { 455 printf("n ENTREI NO WHILE %d. n", i++); 456 aux1 = aux2; 457 aux2 = aux2->prox; 458 } 459 460 if(aux1 == aux2) 461 { 462 printf("n ENTREI NO AUX1 = AUX2 n"); 463 novo->prox = aux1; 464 LISTA = novo; 465 } 466 else 467 { 468 printf("n ENTREI NO AUX1 != AUX2 n"); 469 novo->prox = aux1->prox; 470 aux1->prox = novo; 471 } 472 473 } 474 475 void atualizar(node *LISTA) 476 { 477 node *aux = buscar(LISTA); 478 if(aux == NULL) 479 { 480 printf("n O valor NÃO foi encontrado. n"); 481 } 482 else 483 { 484 printf("n O valor foi encontrado: %d n", aux->num); 485 printf("n Digite o novo valor: "); 486 scanf("%d", &aux->num); 487 printf("n O valor foi atualizado com sucesso! n"); 488 } 489 } 490 491 492 void pesquisaMaior(node *LISTA) 493 { 494 int maior = LISTA->num; 495 while(LISTA != NULL) 496 { 497 if(LISTA->num > maior) 498 maior = LISTA->num; 499 LISTA = LISTA->prox; 500 } 501 printf("nO maior elemento da lista é: %d. n", maior); 502 } 503 504 void pesquisaMenor(node *LISTA)
  • 7.
    505 { 506 LISTA= LISTA->prox; 507 int menor = LISTA->num; 508 while(LISTA != NULL) 509 { 510 if(LISTA->num < menor) 511 menor = LISTA->num; 512 LISTA = LISTA->prox; 513 } 514 printf("nO menor elemento da lista é: %d. n", menor); 515 } 516