Advertisement
Radoan_Ahmed

Untitled

Oct 26th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.43 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.         list -> next - > pre = temp -> next;
  87.         free(list);
  88.         display();
  89.         return;
  90.     }
  91. }
  92.  
  93. void delete_by_valu(int n)
  94. {
  95.     list = head;
  96.     while(list -> next != NULL)
  97.     {
  98.         if(head -> pre == NULL && list -> roll == n)
  99.         {
  100.             head = list -> next;
  101.             head -> pre = NULL;
  102.             free(list);
  103.             display();
  104.             return;
  105.         }
  106.         else if(list -> roll == n)
  107.         {
  108.             data *temp = list -> pre;
  109.             data *temp1 = list -> next;
  110.             temp -> next = list -> next;
  111.             temp1 -> pre = temp;
  112.             free(list);
  113.             display();
  114.             return;
  115.         }
  116.         list = list -> next;
  117.     }
  118.     data *temp1 = list -> pre;
  119.     temp1 -> next = NULL;
  120.     free(list);
  121.     display();
  122.  
  123.     return;
  124.  
  125. }
  126.  
  127. void insert_at_nth(int n)
  128. {
  129.     data *p = (data*)malloc(sizeof(data));
  130.     printf("Enter your name: ");
  131.     scanf(" %[^\n]s",p -> name);
  132.     printf("Enter your name: ");
  133.     scanf("%d",&p -> roll);
  134.     p -> pre = NULL;
  135.     p -> next = NULL;
  136.     list = head;
  137.     int pos = n;
  138.     if(n == 1)
  139.     {
  140.         p -> next = list;
  141.         list -> pre = p;
  142.         head = p;
  143.         display();
  144.         return;
  145.     }
  146.     else if(n>1 && n<=pos)
  147.     {
  148.         int i;
  149.         for(i=0;i<n-1;i++)
  150.         {
  151.             list = list -> next;
  152.         }
  153.         data *temp = list -> pre;
  154.         temp -> next = p;
  155.         p -> pre = temp;
  156.         p -> next = list;
  157.         list -> pre = p;
  158.         display();
  159.  
  160.     }
  161.     else
  162.     {
  163.         printf("invalid position\n");
  164.     }
  165.     return;
  166. }
  167.  
  168. void insert_at_last()
  169. {
  170.     data *q = (data*)malloc(sizeof(data));
  171.     printf("Enter your name: ");
  172.     scanf(" %[^\n]s",q -> name);
  173.     printf("Enter your roll: ");
  174.     scanf("%d",&q -> roll);
  175.     q -> pre = NULL;
  176.     q -> next = NULL;
  177.     list = head;
  178.     while(list -> next != NULL)
  179.     {
  180.         list = list -> next;
  181.     }
  182.     list -> next = q;
  183.     q -> pre = list;
  184.     q -> next = NULL;
  185.     display();
  186.     return;
  187. }
  188.  
  189. void insert_at_fast()
  190. {
  191.     data *q = (data*)malloc(sizeof(data));
  192.     printf("Enter your name: ");
  193.     scanf(" %[^\n]s",q -> name);
  194.     printf("Enter your roll: ");
  195.     scanf("%d",&q -> roll);
  196.     q -> pre = NULL;
  197.     q -> next = NULL;
  198.     list = head;
  199.     list -> pre = q;
  200.     q -> next = list;
  201.     head = q;
  202.     display();
  203.     return;
  204. }
  205.  
  206. main()
  207. {
  208.     int n,i,m,x,y,z,k,t;
  209.     printf("Enter your node amount: ");
  210.     scanf("%d",&n);
  211.     for(i=0; i<n; i++)
  212.     {
  213.         data *N = (data*)malloc(sizeof(data));
  214.         printf("Enter name: ");
  215.         scanf(" %[^\n]s",N -> name);
  216.         printf("Enter roll: ");
  217.         scanf("%d",&N -> roll);
  218.         N -> pre = NULL;
  219.         N -> next = NULL;
  220.  
  221.         if(head == NULL)
  222.         {
  223.             head = N;
  224.             list = head;
  225.         }
  226.         else
  227.         {
  228.             list -> next = N;
  229.             N -> pre = list;
  230.             list = N;
  231.         }
  232.     }
  233.     printf("............................\n");
  234.     display();
  235.     printf("............................\n");
  236.     printf("Enter your search position: ");
  237.     scanf("%d",&m);
  238.     if(m>n || m<=0)
  239.     {
  240.         printf("Invalid position\n");
  241.     }
  242.     else
  243.     {
  244.         search_by_position(m);
  245.     }
  246.     printf("...........................\n");
  247.     printf("Enter your search value: ");
  248.     scanf("%d",&x);
  249.     search_by_value(x);
  250.     printf("...........................\n");
  251.     printf("Enter your delete position: ");
  252.     scanf("%d",&y);
  253.     if(y>n || y <= 0)
  254.     {
  255.         printf("Invalid position\n");
  256.     }
  257.     else
  258.     {
  259.         delete_by_pos(y);
  260.     }
  261.     printf("...........................\n");
  262.     printf("Enter your delete value: ");
  263.     scanf("%d",&z);
  264.     delete_by_valu(z);
  265.     printf("...........................\n");
  266.     printf("Enter a position for add node: ");
  267.     scanf("%d",&t);
  268.     insert_at_nth(t);
  269.     insert_at_last();
  270.     insert_at_fast();
  271.     return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement