Advertisement
Guest User

lol

a guest
Nov 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node {
  4. int info;
  5. struct node* next;
  6. } ElementoDiLista;
  7.  
  8. void addlast (ElementoDiLista** l, int n) {
  9.  
  10. ElementoDiLista* new = malloc (sizeof (ElementoDiLista));
  11. new -> info = n;
  12. new -> next = NULL;
  13.  
  14. if (*l == NULL)
  15. *l = new;
  16. else
  17. {
  18. ElementoDiLista* corr = *l;
  19. while (corr != NULL){
  20. corr = corr -> next;
  21. }
  22. corr -> next = new;
  23. }
  24. }
  25. void add (ElementoDiLista** l, int n) {
  26.  
  27. ElementoDiLista* new = malloc (sizeof (ElementoDiLista));
  28. new -> info = n;
  29. new -> next = *l;
  30. *l = new;
  31. }
  32. void canc (ElementoDiLista** l, int n) {
  33.  
  34. ElementoDiLista* prec = NULL;
  35. ElementoDiLista* corr = *l; //corr = *l;
  36. int trovato = 0;
  37. while (corr -> next != NULL && !trovato){
  38. if (corr -> info == n) trovato =1;
  39. else {
  40. prec = corr;
  41. corr = corr-> next;
  42. }
  43. }
  44. if (trovato)
  45.  
  46. if (prec == NULL){
  47. *l = (*l)-> next;
  48. free (corr);
  49. }
  50. else {
  51. prec -> next = corr -> next;
  52. free (corr);
  53. }
  54.  
  55. }
  56. void lettura (ElementoDiLista* l){
  57.  
  58. ElementoDiLista* corr = l;
  59. while (corr != NULL){
  60. printf ("%d\n", corr -> info);
  61. corr = corr -> next;
  62. }
  63. }
  64. int main () {
  65.  
  66. ElementoDiLista* list = NULL;
  67. int n;
  68.  
  69. while (n != 0){
  70. scanf ("%d", &n);
  71. }
  72.  
  73. if (n < 0)
  74. canc (&list,n);
  75. else if (n > 0 && n %2==0)
  76. add (&list, n);
  77. else if (n > 0 && n%2==1)
  78. addlast (&list, n);
  79. else if (n == 0)
  80. lettura (list);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement