PopaLepo

Stive

Apr 6th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. STIVA
  2.  
  3. typedef struct node
  4. {
  5.     int key;
  6.     struct node *ant;
  7. } NodeT;
  8.  
  9. 1) Adaugarea unui nod in stiva
  10.  
  11. void adauga(NodeT** vf, int x)
  12. {
  13.     NodeT *p;
  14.     p=(NodeT*)malloc(sizeof(NodeT));
  15.     p->key=x;
  16.  
  17.     if(*vf==NULL)
  18.     {
  19.         p->ant=NULL;
  20.         *vf=p;
  21.     }
  22.     else
  23.     {
  24.         p->ant=*vf;
  25.         *vf=p;
  26.     }
  27. }
  28.  
  29. 2) Stergerea unui nod din stiva
  30.  
  31. void *sterge(NodeT** vf)
  32. {
  33.     if(*vf==NULL)
  34.     {
  35.         printf("The stack is NULL");
  36.     }
  37.     else
  38.     {
  39.         if((*vf)->ant==NULL)
  40.         {
  41.             NodeT *q=*vf;
  42.             *vf=NULL;
  43.             free(*vf);
  44.         }
  45.         else
  46.         {
  47.             NodeT *p,*q;
  48.             q=*vf;
  49.             p=(*vf)->ant;
  50.             (*vf)->ant=NULL;
  51.             *vf=NULL;
  52.             free(*vf);
  53.             *vf=p;
  54.         }
  55.     }
  56. }
  57.  
  58. 3) Afisarea elementelor stivei
  59.  
  60. void afis(NodeT* vf)
  61. {
  62.     while(vf != NULL)
  63.     {
  64.         printf("%d ", vf->key);
  65.         vf=vf->ant;
  66.     }
  67.     printf("\n");
  68. }
Add Comment
Please, Sign In to add comment