Advertisement
Radoan_Ahmed

Untitled

Oct 11th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 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; 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.  
  49. void search_by_value(int x)
  50. {
  51.     list = head;
  52.     while(list != NULL)
  53.     {
  54.         if(list -> roll == x)
  55.         {
  56.             printf("%s\n",list -> name);
  57.             printf("%d\n",list -> roll);
  58.         }
  59.         list = list -> next;
  60.     }
  61.     return;
  62.  
  63. }
  64.  
  65. void delete_by_pos(int n)
  66. {
  67.     int i;
  68.     list = head;
  69.     if(n==1)
  70.     {
  71.         head = list -> next;
  72.         head -> pre = NULL;
  73.         free(list);
  74.         display();
  75.         return;
  76.     }
  77.     else
  78.     {
  79.         for(i=0; i<n-1; i++)
  80.         {
  81.             list = list -> next;
  82.         }
  83.         data *temp = list -> pre;
  84.         temp -> next = list -> next;
  85.         free(list);
  86.         display();
  87.         return;
  88.     }
  89. }
  90.  
  91. void delete_by_valu(int n)
  92. {
  93.     list = head;
  94.     while(list -> next != NULL)
  95.     {
  96.         if(head -> pre == NULL && list -> roll == n)
  97.         {
  98.             head = list -> next;
  99.             head -> pre = NULL;
  100.             free(list);
  101.             display();
  102.             return;
  103.         }
  104.        else if(list -> roll == n)
  105.         {
  106.             data *temp = list -> pre;
  107.             data *temp1 = list -> next;
  108.             temp -> next = list -> next;
  109.             temp1 -> pre = temp;
  110.             free(list);
  111.             display();
  112.             return;
  113.         }
  114.         list = list -> next;
  115.     }
  116.     data *temp1 = list -> pre;
  117.     temp1 -> next = NULL;
  118.     free(list);
  119.     display();
  120.  
  121.     return;
  122.  
  123. }
  124.  
  125. main()
  126. {
  127.     int n,i,m,x,y,z,k;
  128.     printf("Enter your node amount: ");
  129.     scanf("%d",&n);
  130.     for(i=0; i<n; i++)
  131.     {
  132.         data *N = (data*)malloc(sizeof(data));
  133.         scanf(" %[^\n]s",N -> name);
  134.         scanf("%d",&N -> roll);
  135.         N -> pre = NULL;
  136.         N -> next = NULL;
  137.  
  138.         if(head == NULL)
  139.         {
  140.             head = N;
  141.             list = head;
  142.         }
  143.         else
  144.         {
  145.             list -> next = N;
  146.             N -> pre = list;
  147.             list = N;
  148.         }
  149.     }
  150.     printf("............................\n");
  151.     display();
  152.     printf("............................\n");
  153.     printf("Enter your search position: ");
  154.     scanf("%d",&m);
  155.     if(m>n)
  156.     {
  157.         printf("Invalid position\n");
  158.     }
  159.     else
  160.     {
  161.         search_by_position(m);
  162.     }
  163.     printf("...........................\n");
  164.     printf("Enter your search value: ");
  165.     scanf("%d",&x);
  166.     if(x>n)
  167.     {
  168.         printf("Invalid position\n");
  169.     }
  170.     else
  171.     {
  172.         search_by_value(x);
  173.     }
  174.     printf("...........................\n");
  175.     printf("Enter your delete position: ");
  176.     scanf("%d",&y);
  177.     if(y>n)
  178.     {
  179.         printf("Invalid position\n");
  180.     }
  181.     else
  182.     {
  183.         delete_by_pos(y);
  184.     }
  185.     printf("...........................\n");
  186.     printf("Enter your delete value: ");
  187.     scanf("%d",&z);
  188.     if(z>n)
  189.     {
  190.         printf("Invalid value\n");
  191.     }
  192.     else
  193.     {
  194.         delete_by_valu(z);
  195.     }
  196.         return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement