Advertisement
TAHMID37

prac_linklist

Aug 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4.  
  5. struct node
  6. {
  7.    int data;
  8.    int key;
  9.  
  10.    struct node *next;
  11.    struct node *prv;
  12. };
  13. struct node *head=NULL;
  14.  
  15. void insert(int data,int key)
  16. {
  17.  
  18.     struct node *temp=(struct node*)malloc(sizeof(struct node));
  19.     temp->data=data;
  20.     temp->key=key;
  21.  
  22.     if(head==NULL)
  23.     {
  24.         temp->next=NULL;
  25.         head=temp;
  26.         return;
  27.     }
  28.     struct node *temp1=head;
  29.     while(temp1->next!=NULL)
  30.     {
  31.         temp1=temp1->next;
  32.     }
  33.  
  34.     temp1->next=temp;
  35.     temp->next=NULL;
  36.  
  37. }
  38.  
  39. void print( )
  40. {
  41.     struct node *ptr=head;
  42.  
  43.     while(ptr!=NULL)
  44.     {
  45.         printf("DATA:%d\t KEY:%d\n",ptr->data,ptr->key);
  46.         ptr=ptr->next;
  47.     }
  48. }
  49. int length()
  50. {
  51.     int len=0;
  52.     struct node *temp=head;
  53.     while(temp!=NULL)
  54.     {
  55.         len++;
  56.         temp=temp->next;
  57.     }
  58.     return len;
  59. }
  60.  
  61. void in_pos(int data,int key,int n)
  62. {
  63.  
  64.     struct node *temp=(struct node*)malloc(sizeof(struct node));
  65.     temp->data=data;
  66.     temp->key=key;
  67.     if(n==1)
  68.     {
  69.         temp->next=head;
  70.         head=temp;
  71.         return;
  72.     }
  73.  
  74.     int size=length();
  75.  
  76.     if((size+1)<n)
  77.     {
  78.         printf("Can't insert in this position\n");
  79.         return ;
  80.     }
  81.  
  82.     struct node *cur=head;
  83.     int i;
  84.     for(i=0;i<(n-2);i++)
  85.     {
  86.         cur=cur->next;
  87.     }
  88.  
  89.     temp->next=cur->next;
  90.     cur->next=temp;
  91.  
  92. }
  93.  
  94. void delete_first()
  95. {
  96.  
  97.  
  98.     if(head==NULL)
  99.     {
  100.         printf("Can't delete no elements assigned\n");
  101.         return;
  102.     }
  103.     head=head->next;
  104.  
  105. }
  106.  
  107. void last_delete()
  108. {
  109.  
  110.     struct node* cur=head,*prv=NULL;
  111.     while(cur->next!=NULL)
  112.     {
  113.         prv=cur;
  114.         cur=cur->next;
  115.     }
  116.  
  117.     prv->next=NULL;
  118.  
  119.  
  120.  
  121.  
  122. }
  123.  
  124. void delete_nth(int n)
  125. {
  126.  
  127.     int size=length();
  128.     if(size<n)
  129.     {
  130.         printf("No elements in this position\n");
  131.         return;
  132.     }
  133.  
  134.     if(n==1)
  135.     {
  136.         head=head->next;
  137.         return;
  138.     }
  139.     int i;
  140.     struct node *cur=head,*temp;
  141.     for(i=0;i<n-2;i++)
  142.     {
  143.         cur=cur->next;
  144.     }
  145.     temp=cur->next;
  146.     cur->next=temp->next;
  147.     free(temp);
  148.  
  149. }
  150.  
  151. void del_key(int key)
  152. {
  153.     if((head->key)==key)
  154.     {
  155.         head=head->next;
  156.         return;
  157.     }
  158.     struct node *cur=head,*prv=NULL;
  159.     while(cur->key!=key)
  160.     {
  161.         if(cur->next==NULL)
  162.             return NULL;
  163.  
  164.         prv=cur;
  165.         cur=cur->next;
  166.     }
  167.     prv->next=cur->next;
  168.     free(cur);
  169.  
  170. }
  171.  
  172. int main()
  173. {
  174.    insert(10,12);
  175.    insert(13,14);
  176.    insert(16,18);
  177.    insert(20,18);
  178.    int size=length();
  179.    printf("%d\n",size);
  180.    print();
  181.  
  182.    in_pos(11,12,1);
  183.    in_pos(12,100,4);
  184.    in_pos(100,111,10);
  185.    insert(11,111111);
  186.    size=length();
  187.  
  188.    printf("%d\n",size);
  189.  
  190.    print();
  191.    printf("\n");
  192.    delete_first();
  193.    delete_first();
  194.    size=length();
  195.    printf("%d\n",size);
  196.    print();
  197.    last_delete();
  198.    size=length();
  199.    printf("%d\n",size);
  200.    print();
  201.    delete_nth(2);
  202.    printf("\n");
  203.    print();
  204.    del_key(14);
  205.    printf("\n");
  206.    print();
  207.  
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement