Advertisement
razvanth21

Untitled

Jan 3rd, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. void sterge(int k)
  2. {
  3.     NOD *p = cauta(k);
  4.  
  5.     if (p == NULL)
  6.     {
  7.         printf("%s Nodul cu cheia (%d) nu a fost gasit in lista de nivel 1!\n", ERR_MSG, k);
  8.         return;
  9.     }
  10.  
  11.     if (p->hd != NULL)
  12.     {
  13.         if (p->hd->nxt == NULL) // nu mai exista nod in lista de nivel 2 dupa
  14.             sterge2(k, p->hd->key);
  15.  
  16.         else
  17.         {
  18.             NOD2 *q;
  19.  
  20.             for (q = p->hd; q != NULL; q = q->nxt)
  21.                 sterge2(k, q->key);
  22.         }
  23.     }
  24.  
  25.     if (p == h) // daca nodul care trebuie sters este de fapt capul listei de nivel 1
  26.     {
  27.         if (p->nxt == NULL)
  28.         {
  29.             h = NULL;
  30.             free(p);
  31.  
  32.             return;
  33.         }
  34.  
  35.         h->key = h->nxt->key;
  36.         h->val = h->nxt->val;
  37.  
  38.         NOD *tmp = h->nxt;
  39.  
  40.         free(h);
  41.         h = tmp;
  42.  
  43.         return;
  44.     }
  45.  
  46.     NOD *a = h; // nodul anterior celui pe care dorim sa-l stergem
  47.  
  48.     while (a->nxt != NULL && a->nxt != p)
  49.         a = a->nxt;
  50.  
  51.     a->nxt = p->nxt;
  52.     free(p);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement