PopaLepo

Liste dublu inlantuite

Apr 7th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.63 KB | None | 0 0
  1. LISTE DUBLU INLANTUITE
  2.  
  3. typedef struct node
  4. {
  5.     int key;
  6.     struct node *next;
  7.     struct node *prev;
  8. } NodeDL;
  9.  
  10. typedef struct
  11. {
  12.     NodeDL *first;
  13.     NodeDL *last;
  14. }list_header;
  15.  
  16. 1) Afisare de la inceput
  17.  
  18. void print_forward(list_header L)
  19. {
  20.     NodeDL *p;
  21.     p=L.first;
  22.     while(p)
  23.     {
  24.         printf("%d ",p->key);
  25.         p=p->next;
  26.     }
  27.     printf("\n");
  28. }
  29.  
  30. 2) Afisare de la sfarsit
  31.  
  32. void print_backward(list_header L)
  33. {
  34.     NodeDL *p;
  35.     p=L.last;
  36.     while(p)
  37.     {
  38.         printf("%d ",p->key);
  39.         p=p->prev;
  40.     }
  41.     printf("\n");
  42. }
  43.  
  44. 3) Cautarea unui nod
  45.  
  46. NodeDL *search(list_header L, int givenKey)
  47. {
  48.     NodeDL *p;
  49.     p=L.first;
  50.     while(p)
  51.     {
  52.         if(p->key==givenKey)
  53.             return p;
  54.         p=p->next;
  55.     }
  56.     return NULL;
  57. }
  58.  
  59. 4) Inserarea in fata primului nod
  60.  
  61. void insert_first(list_header *L, int givenKey)
  62. {
  63.     NodeDL *p;
  64.     p=(NodeDL*)malloc(sizeof(NodeDL));
  65.     p->key=givenKey;
  66.  
  67.     if(L->first==NULL)
  68.     {
  69.         p->next=NULL;
  70.         p->prev=NULL;
  71.         L->first=p;
  72.         L->last=p;
  73.     }
  74.     else
  75.     {
  76.         p->next=L->first;
  77.         p->prev=NULL;
  78.         L->first->prev=p;
  79.         L->first=p;
  80.     }
  81. }
  82.  
  83. 5) Inserarea dupa ultimul nod
  84.  
  85. void insert_last(list_header *L, int givenKey)
  86. {
  87.     NodeDL *p;
  88.     p=(NodeDL*)malloc(sizeof(NodeDL));
  89.     p->key=givenKey;
  90.  
  91.     if(L->first==NULL)
  92.     {
  93.         p->next=NULL;
  94.         p->prev=NULL;
  95.         L->first=p;
  96.         L->last=p;
  97.     }
  98.     else
  99.     {
  100.         L->last->next=p;
  101.         p->next=NULL;
  102.         p->prev=L->last;
  103.         L->last=p;
  104.     }
  105. }
  106.  
  107. 6) Inserarea dupa un key
  108.  
  109. void insert_after_key(list_header *L, int afterKey, int givenKey)
  110. {
  111.     if(L->first==NULL)
  112.     {
  113.         printf("The list is NULL");
  114.     }
  115.     else
  116.     {
  117.         int gasit=0;
  118.         NodeDL *p,*q;
  119.         p=L->first;
  120.         while(p)
  121.         {
  122.             if(p->key==afterKey && gasit==0)
  123.             {
  124.                 gasit=1;
  125.                 q=p;
  126.  
  127.             }
  128.             p=p->next;
  129.  
  130.         }
  131.         if(gasit==0)
  132.         {
  133.             printf("The list don't have this key");
  134.         }
  135.         else
  136.         {
  137.             NodeDL *f;
  138.             f=(NodeDL *)malloc(sizeof(NodeDL));
  139.             f->key=givenKey;
  140.  
  141.             if(q->next==NULL)
  142.             {
  143.                 q->next=f;
  144.                 f->prev=q;
  145.                 f->next=NULL;
  146.                 L->last=f;
  147.  
  148.             }
  149.             else
  150.             {
  151.                 f->next=q->next;
  152.                 q->next->prev=f;
  153.                 q->next=f;
  154.                 f->prev=q;
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. 7) Stergerea primului nod
  161.  
  162. void delete_first(list_header *L)
  163. {
  164.     if(L->first==NULL)
  165.     {
  166.         printf("The list is NULL");
  167.     }
  168.     else
  169.     {
  170.         if(L->first->next==NULL)
  171.         {
  172.             L->first=NULL;
  173.             L->last=NULL;
  174.             free(L->first);
  175.         }
  176.         else
  177.         {
  178.             NodeDL *p;
  179.             p=L->first->next;
  180.             L->first->next=NULL;
  181.             L->first=NULL;
  182.             free(L->first);
  183.             L->first=p;
  184.             L->first->prev=NULL;
  185.         }
  186.     }
  187. }
  188.  
  189. 8) Stergerea ultimului nod
  190.  
  191. void delete_last(list_header *L)
  192. {
  193.     if(L->first==NULL)
  194.     {
  195.         printf("The list is NULL");
  196.     }
  197.     else
  198.     {
  199.         if(L->first->next==NULL)
  200.         {
  201.             L->first=NULL;
  202.             L->last=NULL;
  203.             free(L->first);
  204.         }
  205.         else
  206.         {
  207.             NodeDL *q=L->last;
  208.             NodeDL *p=q->prev;
  209.             q=NULL;
  210.             free(q);
  211.             p->next=NULL;
  212.             L->last=p;
  213.         }
  214.     }
  215. }
  216.  
  217. 10) Stergerea unei key date
  218.  
  219. void delete_key(list_header *L, int givenKey)
  220. {
  221.     if(L->first->key==givenKey)
  222.     {
  223.         delete_first(L);
  224.     }
  225.     else
  226.     {
  227.         int gasit=0;
  228.         NodeDL *p,*q;
  229.         p=L->first;
  230.         while(p->next)
  231.         {
  232.             if(p->next->key==givenKey && gasit==0)
  233.             {
  234.                 q=p;
  235.                 gasit=1;
  236.             }
  237.             p=p->next;
  238.         }
  239.         if(gasit==0)
  240.         {
  241.             printf("The list don't have this key");
  242.         }
  243.         else
  244.         {
  245.             if(q->next==L->last)
  246.             {
  247.                 delete_last(L);
  248.             }
  249.             else
  250.             {
  251.                 NodeDL *f;
  252.                 f=q->next;
  253.                 q->next->next->prev=q;
  254.                 q->next=q->next->next;
  255.                 f->next=NULL;
  256.                 f->prev=NULL;
  257.                 f=NULL;
  258.                 free(f);
  259.             }
  260.         }
  261.     }
  262. }
Add Comment
Please, Sign In to add comment