Advertisement
Guest User

lista duplamente encadeada

a guest
Oct 8th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct no
  5. {
  6. int dado;
  7. struct no *ant;
  8. struct no *prox;
  9. } tno;
  10.  
  11. typedef struct lista
  12. {
  13. tno *com;
  14. } tlista;
  15.  
  16. void IniciaLista(tlista *l)
  17. {
  18. l->com = NULL;
  19. }
  20.  
  21. tno* AlocaNo(int dado)
  22. {
  23. tno *p;
  24. p = (tno*)malloc(sizeof(tno));
  25. if (p)
  26. {
  27. p->dado = dado;
  28. p->ant = NULL;
  29. p->prox = NULL;
  30. }
  31. return p;
  32. }
  33.  
  34. void InsereNo(tlista *l, int dado)
  35. {
  36. tno *aux, *p, *guardaAux;
  37. p = AlocaNo(dado);
  38. if(p)
  39. {
  40. aux = l->com;
  41. while((aux)&&(aux->dado < dado)){
  42. if(aux->prox == NULL){
  43. guardaAux = aux;
  44. }
  45. aux = aux->prox;
  46. }
  47. if(aux) {//insere elemento entre dois elementos
  48. p->ant = aux->ant;
  49. p->prox = aux;
  50. aux->ant = p;
  51. aux->ant->prox = p;
  52.  
  53. }else{
  54. if(!l->com) //!aux, insere o primeiro elemento
  55. {
  56. l->com = p;
  57. }else{
  58. p->ant = guardaAux;
  59. guardaAux->prox = p;
  60. }
  61. }
  62.  
  63.  
  64. }
  65. }
  66.  
  67. void ExcluiNo(tlista *l, int dado){
  68. tno *aux, *guardaAux;
  69. aux = l->com;
  70. guardaAux = NULL;
  71. if(!l->com) printf("\nLista Vazia!\n");
  72. else{
  73. while((aux->prox)&&(aux->dado != dado)){
  74. aux = aux->prox;
  75. }
  76. if(aux){
  77. if(!aux->ant)//o primeiro elemento
  78. {
  79. l->com = aux->prox;
  80. l->com->ant = NULL;
  81. free(aux);
  82. }
  83. else //nao eh o primeiro elemento
  84. {
  85. if(!aux->prox){//eh o ultimo
  86. aux->ant->prox = NULL;
  87. free(aux);
  88. }else{//nao eh o ultimo
  89. aux->ant->prox = aux->prox;
  90. aux->prox->ant = aux->ant;
  91. free(aux);
  92. }
  93.  
  94. }
  95. }
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102. void mostraLista(tlista l)
  103. {
  104. tno *aux;
  105. aux = l.com;
  106. if(!aux) printf ("\nLista vazia\n");
  107. else
  108. {
  109. while(aux)
  110. {
  111. printf("\n %d",aux->dado);
  112. aux = aux->prox;
  113. }
  114. }
  115. }
  116.  
  117. int main()
  118. {
  119.  
  120. int i, n, dado, ExDado, status;
  121. tlista l;
  122. tno *p;
  123.  
  124. IniciaLista(&l);
  125.  
  126. printf("Digite a quantidade de elementos da lista:");
  127. scanf("%d", &n);
  128. for(i=0; i<n; i++)
  129. {
  130. printf("Digite o elemento:");
  131. scanf("%d", &dado);
  132. InsereNo(&l, dado);
  133.  
  134. }
  135. printf("Digite o elemento que deseja excluir:");
  136. scanf("%d", &ExDado);
  137. ExcluiNo(&l, ExDado);
  138.  
  139.  
  140. mostraLista(l);
  141.  
  142.  
  143.  
  144.  
  145.  
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement