Advertisement
Radoan_Ahmed

Untitled

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