Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 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(struct element *p3, int v1);
  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.     viewList(p);
  49.    
  50.     return 0;  
  51. }
  52.  
  53.  
  54. struct element *addToTop(struct element *p0, int v1)
  55. {
  56.     struct element *punt;  
  57.     punt=malloc(sizeof(struct element));
  58.     punt->info=v1;
  59.     punt->next=p0;
  60.     return punt;
  61. }
  62.  
  63.  
  64. void addToBottom(struct element *p1, int v1)
  65. {
  66.     struct element *punta;
  67.    
  68.     punta=malloc(sizeof(struct element));
  69.     punta->info=v1;
  70.     while(p1!=NULL)
  71.     {
  72.         if(p1->next==NULL)
  73.         {
  74.             p1->next=punta;
  75.             punta->next=NULL;
  76.             break;
  77.         }
  78.        
  79.         p1=p1->next;
  80.     }
  81. }
  82.  
  83.  
  84. struct element *deleteOcc(struct element *p3, int v1)
  85. {
  86.     struct element *pi, *pr;
  87.     pr=p3;
  88.    
  89.     if(pr->info==(-1)*v1)
  90.     {
  91.         pi=pr;
  92.         pr=pr->next;
  93.         free(pi);
  94.         return pr;
  95.     }
  96.    
  97.     else
  98.     {
  99.    
  100.     while(pr->next!=NULL)
  101.     {
  102.         if(pr->next->info==(-1)*v1)
  103.         {
  104.             pi=pr->next;
  105.             p3->next=pr->next->next;
  106.             free(pi);
  107.             return p3;
  108.         }  
  109.        
  110.         pr=pr->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