Guest User

Untitled

a guest
Dec 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct nodo
  5. {
  6. float dado;
  7. struct nodo *proximo;
  8. } Nodo;
  9.  
  10.  
  11. int push(Nodo **inicio, float dado) {
  12. Nodo *nodo;
  13. if ((nodo = (Nodo *) malloc(sizeof(Nodo))) == NULL)
  14. return 0;
  15.  
  16. nodo->dado = dado;
  17. nodo->proximo = NULL;
  18.  
  19. if (*inicio != NULL)
  20. nodo->proximo = *inicio;
  21.  
  22. *inicio = nodo;
  23. return 1;
  24. }
  25.  
  26. int pop(Nodo **inicio, float *dado) {
  27. if (*inicio == NULL)
  28. return 0;
  29. else {
  30. Nodo *auxiliar = *inicio;
  31. *dado = (*inicio)->dado;
  32. *inicio = (*inicio)->proximo;
  33. free(auxiliar);
  34. }
  35. return 1;
  36. }
  37.  
  38. void DestroiLista(Nodo **inicio) {
  39. Nodo *ptrAux;
  40. while (*inicio != NULL) {
  41. ptrAux = *inicio;
  42. *inicio = ptrAux->proximo;
  43. printf("Destruindo %.1fn",ptrAux->dado);
  44. free(ptrAux);
  45. }
  46. }
  47.  
  48. int main( ) {
  49. int i;
  50. float dado;
  51. Nodo *pilha = NULL;
  52.  
  53. push(&pilha, 5);
  54. push(&pilha, 2);
  55. push(&pilha, 7);
  56. push(&pilha, 6);
  57. for (i=0;i<6;i++) {
  58. if (pop(&pilha,&dado))
  59. printf("Deu certo remover o valor %f da filan",dado);
  60. else
  61. printf("Nao deu certo remover um elemento da pilhan");
  62. }
  63. DestroiLista(&pilha);
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment