Radoan_Ahmed

Untitled

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