Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct element {
  6.     int info;
  7.     struct element *next;
  8. };
  9.  
  10. struct element *addToTop(struct element *p0, int v1);
  11. void addToBottom(struct element *p1, int v1);
  12. struct element* deleteOcc();
  13. void viewList(struct element *p2);
  14.  
  15.  
  16. int main()
  17. {
  18.     int v=1;
  19.  
  20.     struct element *p;
  21.  
  22.     p=malloc(sizeof(struct element));
  23.     p->next=NULL;
  24.  
  25.     bool once = true;
  26.  
  27.     while(v!=0)
  28.     {
  29.         scanf("%d", &v);
  30.  
  31.         if(once == true && v>0)
  32.         {
  33.             p->info = v;
  34.             once = false;
  35.             continue;
  36.         }
  37.  
  38.         if(v>0 && v%2==0)
  39.             p=addToTop(p,v);
  40.  
  41.         else if(v>0 && v%2!=0)
  42.             addToBottom(p,v);
  43.  
  44.         else if(v<0)
  45.            p=deleteOcc(p,v);
  46.  
  47.     }
  48.  
  49.     viewList(p);
  50.  
  51.     return 0;
  52. }
  53.  
  54.  
  55. struct element *addToTop(struct element *p0, int v1)
  56. {
  57.     struct element *punt;
  58.     punt=malloc(sizeof(struct element));
  59.     punt->info=v1;
  60.     punt->next=p0;
  61.     return punt;
  62. }
  63.  
  64.  
  65. void addToBottom(struct element *p1, int v1)
  66. {
  67.     struct element *punta;
  68.  
  69.     punta=malloc(sizeof(struct element));
  70.     punta->info=v1;
  71.     while(p1!=NULL)
  72.     {
  73.         if(p1->next==NULL)
  74.         {
  75.             p1->next=punta;
  76.             punta->next=NULL;
  77.             break;
  78.         }
  79.         p1=p1->next;
  80.     }
  81. }
  82.  
  83.  
  84. struct element* deleteOcc(struct element *p3, int v1)
  85. {
  86.     struct element *pi;
  87.     //viewList(p3);
  88.     printf("valore p3 iniziale:%d\n",p3->info);
  89.     if(p3->info==(-1)*v1)
  90.     {
  91.         pi = p3;
  92.         printf("valore p3: %d\n",p3->info);
  93.         p3=p3->next;
  94.         printf("valore p3 after: %d\n",p3->info);
  95.         free(pi);
  96.         return p3;
  97.  
  98.     } else {
  99.  
  100.     while(p3->next!=NULL)
  101.     {
  102.         if(p3->next->info==v1 || p3->next->info==(-1)*v1)
  103.         {
  104.             pi=p3->next;
  105.             p3->next=p3->next->next;
  106.             free(pi);
  107.             return p3;
  108.         }
  109.  
  110.         p3=p3->next;
  111.     }
  112.     return p3;
  113. }
  114. }
  115.  
  116. void viewList(struct element *p2)
  117. {
  118.     while(p2!=NULL)
  119.     {
  120.         printf("%d\n", p2->info);
  121.         p2=p2->next;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement