Advertisement
Zahidx

Doubly Linked List

Oct 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.41 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct std_info
  4. {
  5.     char name[50];
  6.     int roll;
  7.     struct std_info *pre;
  8.     struct std_info *next;
  9. } data;
  10.  
  11. data *head = NULL;
  12. data *list = NULL;
  13.  
  14. void display()
  15. {
  16.     list = head;
  17.     while(list != NULL)
  18.     {
  19.         printf("%s\n",list -> name);
  20.         printf("%d\n",list -> roll);
  21.         list = list -> next;
  22.     }
  23. }
  24.  
  25. void search_by_position(int m)
  26. {
  27.     int i;
  28.     list = head;
  29.     if(m==0)
  30.     {
  31.         printf("%s\n",list -> name);
  32.         printf("%d\n",list -> roll);
  33.         return;
  34.     }
  35.  
  36.     else
  37.     {
  38.         for(i=0; i<m-1; i++)
  39.         {
  40.             list = list -> next;
  41.         }
  42.         printf("%s\n",list -> name);
  43.         printf("%d\n",list -> roll);
  44.         return;
  45.     }
  46. }
  47.  
  48. void search_by_value(int x)
  49. {
  50.     list = head;
  51.     while(list != NULL)
  52.     {
  53.         if(list -> roll == x)
  54.         {
  55.             printf("%s\n",list -> name);
  56.             printf("%d\n",list -> roll);
  57.         }
  58.         list = list -> next;
  59.     }
  60.     return;
  61. }
  62.  
  63. void delete_by_pos(int n)
  64. {
  65.     int i;
  66.     list = head;
  67.     if(n==1)
  68.     {
  69.         head = list -> next;
  70.         head -> pre = NULL;
  71.         free(list);
  72.         display();
  73.         return;
  74.     }
  75.     else
  76.     {
  77.         for(i=0; i<n-1; i++)
  78.         {
  79.             list = list -> next;
  80.         }
  81.         data *temp = list -> pre;
  82.         temp -> next = list -> next;
  83.         free(list);
  84.         display();
  85.         return;
  86.     }
  87. }
  88.  
  89. void delete_by_valu(int n)
  90. {
  91.     list = head;
  92.     while(list -> next != NULL)
  93.     {
  94.         if(head -> pre == NULL && list -> roll == n)
  95.         {
  96.             head = list -> next;
  97.             head -> pre = NULL;
  98.             free(list);
  99.             display();
  100.             return;
  101.         }
  102.         else if(list -> roll == n)
  103.         {
  104.             data *temp = list -> pre;
  105.             data *temp1 = list -> next;
  106.             temp -> next = list -> next;
  107.             temp1 -> pre = temp;
  108.             free(list);
  109.             display();
  110.             return;
  111.         }
  112.         list = list -> next;
  113.     }
  114.     data *temp1 = list -> pre;
  115.     temp1 -> next = NULL;
  116.     free(list);
  117.     display();
  118.  
  119.     return;
  120. }
  121.  
  122. void insert_at_nth(int n)
  123. {
  124.     data *p = (data*)malloc(sizeof(data));
  125.     printf("Enter your name: ");
  126.     scanf(" %[^\n]s",p -> name);
  127.     printf("Enter your name: ");
  128.     scanf("%d",&p -> roll);
  129.     p -> pre = NULL;
  130.     p -> next = NULL;
  131.     list = head;
  132.     if(n == 1)
  133.     {
  134.         p -> next = list;
  135.         list -> pre = p;
  136.         head = p;
  137.         display();
  138.         return;
  139.     }
  140.     else
  141.     {
  142.         int i;
  143.         for(i=0; i<n-1; i++)
  144.         {
  145.             list = list -> next;
  146.         }
  147.         data *temp = list -> pre;
  148.         temp -> next = p;
  149.         p -> pre = temp;
  150.         p -> next = list;
  151.         list -> pre = p;
  152.         display();
  153.  
  154.     }
  155.     return;
  156. }
  157.  
  158. void insert_at_last()
  159. {
  160.     data *q = (data*)malloc(sizeof(data));
  161.     printf("Enter your name: ");
  162.     scanf(" %[^\n]s",q -> name);
  163.     printf("Enter your roll: ");
  164.     scanf("%d",&q -> roll);
  165.     q -> pre = NULL;
  166.     q -> next = NULL;
  167.     list = head;
  168.     while(list -> next != NULL)
  169.     {
  170.         list = list -> next;
  171.     }
  172.     list -> next = q;
  173.     q -> pre = list;
  174.     q -> next = NULL;
  175.     display();
  176.     return;
  177. }
  178.  
  179. void insert_at_fast()
  180. {
  181.     data *q = (data*)malloc(sizeof(data));
  182.     printf("Enter your name: ");
  183.     scanf(" %[^\n]s",q -> name);
  184.     printf("Enter your roll: ");
  185.     scanf("%d",&q -> roll);
  186.     q -> pre = NULL;
  187.     q -> next = NULL;
  188.     list = head;
  189.     list -> pre = q;
  190.     q -> next = list;
  191.     head = q;
  192.     display();
  193.     return;
  194. }
  195.  
  196. main()
  197. {
  198.     int n,i,m,x,y,z,k,t;
  199.     printf("Enter your node amount: ");
  200.     scanf("%d",&n);
  201.     for(i=0; i<n; i++)
  202.     {
  203.         data *N = (data*)malloc(sizeof(data));
  204.         printf("Enter name: ");
  205.         scanf(" %[^\n]s",N -> name);
  206.         printf("Enter roll: ");
  207.         scanf("%d",&N -> roll);
  208.         N -> pre = NULL;
  209.         N -> next = NULL;
  210.  
  211.         if(head == NULL)
  212.         {
  213.             head = N;
  214.             list = head;
  215.         }
  216.         else
  217.         {
  218.             list -> next = N;
  219.             N -> pre = list;
  220.             list = N;
  221.         }
  222.     }
  223.     printf("............................\n");
  224.     display();
  225.     printf("............................\n");
  226.     printf("Enter your search position: ");
  227.     scanf("%d",&m);
  228.     if(m>n)
  229.     {
  230.         printf("Invalid position\n");
  231.     }
  232.     else
  233.     {
  234.         search_by_position(m);
  235.     }
  236.     printf("...........................\n");
  237.     printf("Enter your search value: ");
  238.     scanf("%d",&x);
  239.     if(x>n)
  240.     {
  241.         printf("Invalid position\n");
  242.     }
  243.     else
  244.     {
  245.         search_by_value(x);
  246.     }
  247.     printf("...........................\n");
  248.     printf("Enter your delete position: ");
  249.     scanf("%d",&y);
  250.     if(y>n)
  251.     {
  252.         printf("Invalid position\n");
  253.     }
  254.     else
  255.     {
  256.         delete_by_pos(y);
  257.     }
  258.     printf("...........................\n");
  259.     printf("Enter your delete value: ");
  260.     scanf("%d",&z);
  261.     if(z>n)
  262.     {
  263.         printf("Invalid value\n");
  264.     }
  265.     else
  266.     {
  267.         delete_by_valu(z);
  268.     }
  269.     printf("...........................\n");
  270.     printf("Enter your value for add node: ");
  271.     scanf("%d",&t);
  272.     insert_at_nth(t);
  273.     insert_at_last();
  274.     insert_at_fast();
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement