Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6. int info;
  7. struct node *next;
  8. }lista;
  9.  
  10. lista *p, *q, *prim = NULL;
  11.  
  12. void add(int i)
  13. {
  14. p = (lista*)malloc(sizeof(lista));
  15.  
  16. p->info = i;
  17. p->next = NULL;
  18.  
  19. if(prim == NULL)
  20. prim = p;
  21. else
  22. q->next = p;
  23. q = p;
  24. }
  25.  
  26. void show()
  27. {
  28. for(p = prim; p != NULL; p = p->next)
  29. printf("%d\n", p->info);
  30. }
  31.  
  32. void remover(int i)
  33. {
  34. for (p = prim; p != NULL; p = p->next)
  35. {
  36. if (p->info == i)
  37. {
  38. if(p == prim)
  39. prim = prim->next;
  40. else
  41. q->next = p->next;
  42. free(p);
  43. break;
  44. }
  45. q = p;
  46. }
  47. }
  48.  
  49. int main(void)
  50. {
  51. int i, num;
  52.  
  53. for(i = 1; i <= 3; i++)
  54. add(i);
  55. show();
  56.  
  57. printf ("Informa o valor a ser apagado (1,2,3): ");
  58. scanf("%d",&num);
  59. remover(num);
  60.  
  61. show();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement