Patey

Untitled

Mar 17th, 2021
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4. typedef struct lista{
  5.     int nr;
  6.     struct lista *urm;
  7. }nod;
  8.  
  9. nod *adaugare(nod *prim, int n)
  10. {
  11.     nod *p,*q;
  12.     p = (nod*)malloc(sizeof(nod));
  13.     p->nr = n;
  14.     p->urm = NULL;
  15.     if (prim == NULL)
  16.         return p;
  17.     else
  18.     {
  19.         q = prim;
  20.         while (q->urm != NULL)
  21.             q = q->urm;
  22.         q->urm = p;
  23.         return prim;
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     nod *prim,*p;
  30.     int n, *po, i = 0, j=0;
  31.     prim = NULL;
  32.  
  33.     p = prim;
  34.     scanf("%d", &n);
  35.     while (n > 0)
  36.     {
  37.         prim = adaugare(prim, n % 2);
  38.         n = n / 2;
  39.     }
  40.  
  41.     p = prim;
  42.     while (p != NULL)
  43.     {
  44.         printf("%d ", p->nr);
  45.         p = p->urm;
  46.         i++;
  47.     }
  48.     printf("\n");
  49.  
  50.     po = (int*)malloc(i * sizeof(int));
  51.     p = prim;
  52.     while (p != NULL)
  53.     {
  54.         po[j] = p->nr;
  55.         p = p->urm;
  56.         j++;
  57.     }
  58.     for (j = 0; j < i; j++)
  59.         printf("%d ", *(po+j));
  60. }
Advertisement
Add Comment
Please, Sign In to add comment