Radoan_Ahmed

Untitled

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