Advertisement
Radoan_Ahmed

Untitled

Oct 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 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 age;
  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 -> age);
  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 -> age);
  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 -> age);
  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 -> age == x)
  56.         {
  57.             printf("%s\n",list -> name);
  58.             printf("%d\n",list -> age);
  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 -> age == n)
  98.         {
  99.             head = list -> next;
  100.             head -> pre = NULL;
  101.             free(list);
  102.             display();
  103.             return;
  104.         }
  105.         else if(list -> age == 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.     scanf(" %[^\n]s",p -> name);
  130.     scanf("%d",&p -> age);
  131.     p -> pre = NULL;
  132.     p -> next = NULL;
  133.     list = head;
  134.     int pos = n;
  135.     if(n == 1)
  136.     {
  137.         p -> next = list;
  138.         list -> pre = p;
  139.         head = p;
  140.         display();
  141.         return;
  142.     }
  143.     else if(n>1 && n<=pos)
  144.     {
  145.         int i;
  146.         for(i=0;i<n-1;i++)
  147.         {
  148.             list = list -> next;
  149.         }
  150.         data *temp = list -> pre;
  151.         temp -> next = p;
  152.         p -> pre = temp;
  153.         p -> next = list;
  154.         list -> pre = p;
  155.         display();
  156.  
  157.     }
  158.     else
  159.     {
  160.         printf("invalid position\n");
  161.     }
  162.     return;
  163. }
  164.  
  165. void insert_at_last()
  166. {
  167.     data *q = (data*)malloc(sizeof(data));
  168.     scanf(" %[^\n]s",q -> name);
  169.     scanf("%d",&q -> age);
  170.     q -> pre = NULL;
  171.     q -> next = NULL;
  172.     list = head;
  173.     while(list -> next != NULL)
  174.     {
  175.         list = list -> next;
  176.     }
  177.     list -> next = q;
  178.     q -> pre = list;
  179.     q -> next = NULL;
  180.     display();
  181.     return;
  182. }
  183.  
  184. void insert_at_fast()
  185. {
  186.     data *q = (data*)malloc(sizeof(data));
  187.     scanf(" %[^\n]s",q -> name);
  188.     scanf("%d",&q -> age);
  189.     q -> pre = NULL;
  190.     q -> next = NULL;
  191.     list = head;
  192.     list -> pre = q;
  193.     q -> next = list;
  194.     head = q;
  195.     display();
  196.     return;
  197. }
  198.  
  199. main()
  200. {
  201.     int n,i,m,x,y,z,k,t;
  202.     scanf("%d",&n);
  203.     for(i=0; i<n; i++)
  204.     {
  205.         data *N = (data*)malloc(sizeof(data));
  206.         scanf(" %[^\n]s",N -> name);
  207.         scanf("%d",&N -> age);
  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.     display();
  224.     printf("Enter your search position: ");
  225.     scanf("%d",&m);
  226.     if(m>n || m<=0)
  227.     {
  228.         printf("Invalid position\n");
  229.     }
  230.     else
  231.     {
  232.         search_by_position(m);
  233.     }
  234.     printf("Enter your search value: ");
  235.     scanf("%d",&x);
  236.     search_by_value(x);
  237.     printf("Enter your delete position: ");
  238.     scanf("%d",&y);
  239.     if(y>n || y <= 0)
  240.     {
  241.         printf("Invalid position\n");
  242.     }
  243.     else
  244.     {
  245.         delete_by_pos(y);
  246.     }
  247.     printf("Enter your delete value: ");
  248.     scanf("%d",&z);
  249.     delete_by_valu(z);
  250.     printf("Enter a position for add node: ");
  251.     scanf("%d",&t);
  252.     insert_at_nth(t);
  253.     insert_at_last();
  254.     insert_at_fast();
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement