Florii11

l2

Mar 2nd, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int key;
  7.     struct node *next;
  8. } NodeT;
  9.  
  10. NodeT *search(NodeT *head, int givenKey)
  11. {
  12.     //TODO: search the given key and return the first node containing it or NULL
  13.     NodeT *p;
  14.     p=head;
  15.     while(p)
  16.     {
  17.         if(p->key==givenKey)
  18.             return p;
  19.         p=p->next;
  20.     }
  21.     return NULL;
  22. }
  23.  
  24. void print_list(NodeT *head)
  25. {
  26.     //TODO: print list keys
  27.     NodeT *p;
  28.     p=head;
  29.     while(p!=NULL)
  30.     {
  31.         printf("%d ",p->key);
  32.         p=p->next;
  33.     }
  34.     printf("\n");
  35. }
  36.  
  37. void insert_first(NodeT **head, NodeT **tail, int givenKey)
  38. {
  39.     //TODO: insert the given key in the first position of the list given by head and tail;
  40.     NodeT *p=(NodeT*)malloc(sizeof(NodeT));
  41.     p->key=givenKey;
  42.     p->next=NULL;
  43.     if(*head==NULL)
  44.     {
  45.         p->next=NULL;
  46.         *head=p;
  47.         *tail=p;
  48.     }
  49.     else
  50.     {
  51.         p->next=*head;
  52.         *head=p;
  53.     }
  54. }
  55.  
  56. void insert_last(NodeT **head, NodeT **tail, int givenKey)
  57. {
  58.     //TODO: insert the given key in the last position of the list given by head and tail;
  59.     NodeT *p=(NodeT*)malloc(sizeof(NodeT));
  60.     p->key=givenKey;
  61.     p->next=NULL;
  62.     if(*head==NULL)
  63.     {
  64.         *head=p;
  65.         *tail=p;
  66.     }
  67.     else
  68.     {
  69.         (*tail)->next=p;
  70.         *tail=p;
  71.     }
  72. }
  73.  
  74. void insert_after_key(NodeT** head, NodeT** tail, int afterKey, int givenKey)
  75. {
  76.     //TODO: insert the given key after afterKey, in list given by head and tail;
  77.     NodeT*q;
  78.     q=*head;
  79.     NodeT *p=(NodeT*)malloc(sizeof(NodeT));
  80.     p->key=givenKey;
  81.     p->next=NULL;
  82.     if(q==NULL)
  83.     {
  84.         insert_first(head,tail,givenKey);
  85.         return;
  86.     }
  87.     while(q!=NULL)
  88.     {
  89.         if(q->key==afterKey)
  90.         {
  91.             p->next=q->next;
  92.             q->next=p;
  93.             if(q==*tail)
  94.                 *tail=p;
  95.             break;
  96.         }
  97.         q=q->next;
  98.     }
  99.  
  100. }
  101.  
  102. void insert_before_key(NodeT** head, NodeT** tail, int beforeKey, int givenKey)
  103. {
  104.     //TODO: insert the given key after afterKey, in list given by head and tail;
  105.     NodeT *p=(NodeT*)malloc(sizeof(NodeT));
  106.     p->key=givenKey;
  107.     p->next=NULL;
  108.  
  109.     NodeT*crt,*before;
  110.     crt=*head;
  111.     before=NULL;
  112.  
  113.     while(crt!=NULL)
  114.     {
  115.         if(crt->key==beforeKey)
  116.         {
  117.             before->next=p;
  118.             p->next=crt;
  119.             break;
  120.         }
  121.         else
  122.         {
  123.             before=crt;
  124.             crt=crt->next;
  125.         }
  126.     }
  127. }
  128.  
  129. void delete_first(NodeT** head, NodeT** tail)
  130. {
  131.     //TODO: delete first list element
  132.     if(*head==NULL)
  133.         return;
  134.     else{
  135.         NodeT *p=*head;
  136.         *head=(*head)->next;
  137.         //free(p);
  138.     }
  139. }
  140.  
  141. void delete_last(NodeT** head, NodeT** tail)
  142. {
  143.     //TODO: delete last list element
  144.     NodeT*p=*head;
  145.     NodeT*prev=NULL;
  146.  
  147.     if(*head==NULL)
  148.         return;
  149.  
  150.     while(p!=*tail)
  151.     {
  152.         prev=p;
  153.         p=p->next;
  154.     }
  155.     if(p==*head)
  156.     {
  157.         *head=NULL;
  158.         *tail=NULL;
  159.     }
  160.     prev->next=NULL;
  161.     *tail=prev;
  162. }
  163.  
  164. void delete_key(NodeT** first, NodeT** last, int givenKey)
  165. {
  166.     //TODO: delete element having given key
  167.     NodeT *crt, *before;
  168.     crt=*first;
  169.     before=NULL;
  170.     if(first==NULL)
  171.         return;
  172.     while(crt!=NULL)
  173.     {
  174.         if(crt->key==givenKey)
  175.         {
  176.             if(crt==*first)
  177.                 delete_first(first, last);
  178.             else
  179.             {
  180.                 if(crt==*last)
  181.                     delete_last(first,last);
  182.                 else
  183.                 {
  184.                     before->next=crt->next;
  185.                 }
  186.             }
  187.         }
  188.         before=crt;
  189.         crt=crt->next;
  190.     }
  191. }
  192.  
  193. int main()
  194. {
  195.     NodeT *first = NULL;
  196.     NodeT *last  = NULL;
  197.  
  198.     //perform several insertions
  199.     insert_first(&first, &last, 4);
  200.     insert_first(&first, &last, 1);
  201.     insert_last(&first, &last, 3);
  202.  
  203.     insert_before_key(&first, &last, 4, 5);
  204.  
  205.     //search for two distinct keys...
  206.     int toSearch = 2;
  207.     NodeT *foundNode = search(first, toSearch);
  208.     if (foundNode == NULL)
  209.     {
  210.         printf("Node %d not found!\n", toSearch);
  211.     }
  212.     else
  213.     {
  214.         printf("%d found!\n", foundNode->key);
  215.     }
  216.  
  217.     toSearch = 3;
  218.     foundNode = search(first, toSearch);
  219.     if (foundNode == NULL)
  220.     {
  221.         printf("Node %d not found!\n", toSearch);
  222.     }
  223.     else
  224.     {
  225.         printf("%d found!\n", foundNode->key);
  226.     }
  227.  
  228.     //perform some insertions
  229.     insert_after_key(&first, &last, 4, 22);
  230.     insert_after_key(&first, &last, 3, 25);
  231.  
  232.     //print list contents
  233.     print_list(first);
  234.  
  235.     //perform some deletions
  236.     delete_first(&first,&last);
  237.     delete_last(&first, &last);
  238.     delete_key(&first, &last, 3);
  239.     //delete_key(&first, &last, 8);
  240.  
  241.     //print list contents
  242.     print_list(first);
  243.  
  244.  
  245.     //TODO: place here code to delete all list
  246.     /*....*/
  247.  
  248.     //print list contents
  249.     print_list(first);
  250.  
  251.     return 0;
  252. }
  253.  
Add Comment
Please, Sign In to add comment