Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct celula {
  5. int valor;
  6. struct celula *prox;
  7. };
  8.  
  9. void InsereNaLista (struct celula * ini) {
  10. // inserir elementos em ordem!
  11. struct celula *anterior = NULL;
  12. struct celula *novo;
  13. novo = malloc (sizeof(struct celula));
  14. printf("Digite o valor: ");
  15. scanf("%d", &novo->valor);
  16.  
  17. // a lista está vazia
  18. if (ini->prox == NULL) {
  19. ini->prox = novo;
  20. novo->prox = NULL;
  21. }
  22.  
  23. // a lista nao esta vazia
  24. else {
  25. while (ini->prox != NULL && ini->valor < novo->valor) {
  26. anterior = ini;
  27. ini = ini->prox;
  28. }
  29. novo->prox = ini;
  30. if (anterior == NULL) ini = novo;
  31. else anterior->prox = novo;
  32. }
  33. }
  34.  
  35. void ImprimeListaCompleta(struct celula *inicio) {
  36. struct celula *p;
  37. printf("Teste lista completa: ");
  38. for (p = inicio->prox; p != NULL; p = p->prox) printf("%d ", p->valor);
  39. printf("\n\n");
  40. }
  41.  
  42. int main() {
  43.  
  44. struct celula *inicio;
  45. inicio = malloc (sizeof(struct celula));
  46. inicio->prox = NULL;
  47. inicio->valor = -666;
  48.  
  49. // quero acrescentar quantos elementos eu desejar a minha lista ligada
  50. // quero inseri-los em ordem!
  51. while (1) {
  52. InsereNaLista(inicio);
  53. ImprimeListaCompleta(inicio);
  54. }
  55.  
  56. system("Pause");
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement