Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. struct lista
  6. {
  7.     int nr;
  8.     struct lista *urmator;
  9. };
  10.  
  11. typedef struct lista LI;
  12.  
  13. void afisare2(LI *cap)
  14. {
  15.     LI *aux;
  16.     for(aux = cap;aux != NULL;aux = aux->urmator)
  17.     {
  18.         printf("Val(2) = %d\n",aux->nr);
  19.     }
  20.     printf("-----------------------\n");
  21. }
  22.  
  23. LI *elimina(LI *cap)
  24. {
  25.     LI *aux,*p;
  26.     while(cap != NULL && cap->nr%2 == 0 && cap->urmator != NULL)
  27.     {
  28.         aux = cap;
  29.         cap = cap->urmator;
  30.         free(aux);
  31.     }
  32.     if(cap->urmator == NULL)
  33.     {
  34.         return NULL;
  35.     }
  36.     p = cap;
  37.     while(p->urmator != NULL)
  38.     {
  39.         if(p->urmator->nr%2 == 0)
  40.         {
  41.             aux = p->urmator;
  42.             p->urmator = p->urmator->urmator;
  43.             free(aux);
  44.         }
  45.         else p = p->urmator;
  46.     }
  47.     return cap;
  48. }
  49.  
  50. int main(void)
  51. {
  52.     LI *CapDeLista,*p,*q;
  53.     int i,n;
  54.     printf("Introdceti numarul de elemente din lista:\n");
  55.     scanf("%d",&n);
  56.     p = (LI*)malloc(sizeof(LI));
  57.     printf("Introduceti valoarea: \n");
  58.     scanf("%d",&p->nr);
  59.     p->urmator = NULL;
  60.     CapDeLista = p;
  61.     for(i=1;i<n;i++)
  62.     {
  63.         q = (LI*)malloc(sizeof(LI));
  64.         printf("Introduceti valoarea: \n");
  65.         scanf("%d",&q->nr);
  66.         q->urmator = NULL;
  67.         p->urmator = q;
  68.         p = q;
  69.     }
  70.     afisare2(CapDeLista);
  71.     CapDeLista = elimina(CapDeLista);
  72.     if(CapDeLista == NULL)
  73.         printf("Lista goala!\n");
  74.     else afisare2(CapDeLista);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement