Advertisement
mdnurnobihosen

Link list fully cover example (shanto)

Jun 29th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.63 KB | None | 0 0
  1. ///Link list total example
  2. #include<stdio.h>
  3. #include<stdlib.h> //link list library header
  4. #include<string.h> //String library header
  5.  
  6. ///Structure create
  7. typedef struct Node
  8. {
  9.  
  10.     char name[15];
  11.     char phn[12];
  12.     char email[20];
  13.     struct Node *next;
  14. } node;
  15.  
  16. ///Global Variable
  17. int count=0;
  18. node *head = NULL;
  19. node *temp = NULL;
  20.  
  21. /// Insert data at begin of list...
  22. void insertAtbigen()
  23. {
  24.     printf("Enter number of total list :");
  25.     int i,n;
  26.     scanf("%d",&n);
  27.     for(i=0; i<n; i++)
  28.     {
  29.         printf("\nEnter Node information: #%d\n",i+1);
  30.         node *newnode;
  31.         newnode=(node*)malloc(sizeof(node));
  32.  
  33.         printf("Enter Name:");scanf("%s", &newnode-> name);
  34.         printf("Enter Phone Number:");scanf("%s", &newnode-> phn);
  35.         printf("Enter Email Address:");scanf("%s", &newnode-> email);
  36.         count++;
  37.  
  38.         newnode->next=NULL;
  39.         if(head==NULL)
  40.         {
  41.             head=newnode;
  42.         }
  43.         else
  44.         {
  45.             newnode->next=head;
  46.             head=newnode;
  47.         }
  48.  
  49.     }
  50. }
  51.  
  52. /// insert at end of list....
  53. void insertAtend()
  54. {
  55.     printf("Enter number of total list:");
  56.     int i,n;
  57.     scanf("%d",&n);
  58.     for(i=0; i<n; i++)
  59.     {
  60.         printf("\nEnter Node information: #%d\n",i+1);
  61.         node *newnode;
  62.         newnode=(node*)malloc(sizeof(node));
  63.  
  64.         printf("Enter Name:");scanf("%s", &newnode-> name);
  65.         printf("Enter Phone Number:");scanf("%s", &newnode-> phn);
  66.         printf("Enter Email Address:");scanf("%s", &newnode-> email);
  67.         count++;
  68.  
  69.         newnode->next=NULL;
  70.         if(head==NULL)
  71.         {
  72.             head=newnode;
  73.         }
  74.         else
  75.         {
  76.             temp=head;
  77.             while(temp->next!=NULL)
  78.             {
  79.                 temp=temp->next;
  80.             }
  81.                temp->next=newnode;
  82.         }
  83.     }
  84. }
  85.  
  86. ///print all data ....
  87. void printList()
  88. {
  89.     temp = head;
  90.     printf("\n---------Show Node list Information:---------\n");
  91.     int c=1;
  92.     while(temp != NULL)
  93.     {
  94.         printf("\nDisplay Node information: #%d\n",c++);
  95.         printf("Name: %s\n",temp-> name);
  96.         printf("Phone: %s\n",temp-> phn);
  97.         printf("Email: %s\n",temp-> email);
  98.  
  99.         temp= temp-> next;
  100.     }
  101.  
  102.  
  103. }
  104.  
  105. ///searching area......
  106.  
  107. void searchNode()
  108. {
  109.     printf("\n---------Search part:---------\n");
  110.     temp=head;
  111.     printf("Searching by any value for press-1\n");
  112.     printf("Searching by position for press-2\n");
  113.  
  114.     ///Searching by any value....
  115.     int s;
  116.     scanf("%d",&s);
  117.     if(s==1)
  118.     {
  119.     char src[20];
  120.     printf("Enter search value name or email:");
  121.     scanf("%s",src);
  122.     while(temp!=NULL && ((strcmp(temp->name,src)!=0) && (strcmp(temp->email,src)!=0)))
  123.         {
  124.             temp=temp-> next;
  125.         }
  126.  
  127.     if(temp==NULL)
  128.         {
  129.             printf("Not Found\n");
  130.         }
  131.  
  132.     else {  printf("\nSearch Friend information:\n");
  133.             printf("Name: %s\n",temp-> name);
  134.             printf("Phone: %s\n",temp-> phn);
  135.             printf("Email: %s\n",temp-> email);
  136.  
  137.          }
  138.     }
  139.     ///Searching by position.......
  140.     else if(s==2)
  141.     {
  142.         int position,po=1;
  143.         printf("Enter Position:");
  144.         scanf("%d",&position);
  145.     while(temp!=NULL && position>count && position!=po)
  146.         {
  147.             temp=temp-> next;
  148.             po++;
  149.         }
  150.  
  151.     if(temp==NULL || position>count)
  152.         {
  153.             printf("Not Founded entered your position\n");
  154.         }
  155.  
  156.     else {  printf("\nFounded your Position #%d no information:\n",position);
  157.             printf("Name: %s\n",temp-> name);
  158.             printf("Phone: %s\n",temp-> phn);
  159.             printf("Email: %s\n",temp-> email);
  160.  
  161.          }
  162.     }
  163. }
  164.  
  165. ///Delete or Update Area...
  166. void deleteeNode()
  167. {
  168.     printf("\n---------Delete or Update part:---------\n");
  169.     node *pre=NULL;
  170.      temp=head;
  171.     printf("Searching by any value for press-1\n");
  172.     printf("Searching by position for press-2\n");
  173.  
  174.     ///Searching & deleted by any value....
  175.     int s;
  176.     scanf("%d",&s);
  177.     if(s==1)
  178.     {
  179.     char src[20];
  180.     printf("Enter search value name or email:");
  181.     scanf("%s",src);
  182.     while(temp!=NULL && ((strcmp(temp->name,src)!=0) && (strcmp(temp->email,src)!=0)&&(strcmp(temp->phn,src)!=0)))
  183.         {
  184.             pre=temp;
  185.             temp=temp-> next;
  186.         }
  187.  
  188.     if(temp==NULL)
  189.         {
  190.             printf("Not Found\n");
  191.         }
  192.  
  193.     else {  printf("\nSearch Friend information:\n");
  194.             printf("Name: %s\n",temp-> name);
  195.             printf("Phone: %s\n",temp-> phn);
  196.             printf("Email: %s\n",temp-> email);
  197.  
  198.             printf("Enter 1 for delete or Enter 2 for Edit..\n");
  199.           int d;
  200.           scanf("%d",&d);
  201.           if(2==d)
  202.           {
  203.            printf("Enter update Name:");scanf("%s", &temp-> name);
  204.            printf("Enter update Phone Number:");scanf("%s", &temp-> phn);
  205.            printf("Enter update Email Address:");scanf("%s", &temp-> email);
  206.            printf("Update Successfully\n\n");
  207.           }
  208.           else if(d==1)
  209.           {
  210.             if(temp==head)
  211.             {
  212.               head=temp->next;
  213.               printf("\nInformation of %s has deleted successfully \n",temp->name);
  214.               free(temp);
  215.               --count;
  216.             }
  217.             pre->next=temp->next;
  218.             printf("\nInformation of %s has deleted successfully \n",temp->name);
  219.             free(temp);
  220.             --count;
  221.           }
  222.  
  223.          }
  224.     }
  225.     ///Searching & deleted by position....
  226.     else if(s==2)
  227.     {
  228.         int position,po=1;
  229.         printf("Enter Position:");
  230.         scanf("%d",&position);
  231.     while(temp!=NULL && position>count && position==po)
  232.         {
  233.             pre=temp;
  234.             temp=temp-> next;
  235.             po++;
  236.         }
  237.  
  238.     if(temp==NULL || position>count)
  239.         {
  240.             printf("Not Founded entered your position\n");
  241.         }
  242.  
  243.     else {  printf("\nFounded your Position #%d no information:\n",position);
  244.             printf("Name: %s\n",temp-> name);
  245.             printf("Phone: %s\n",temp-> phn);
  246.             printf("Email: %s\n",temp-> email);
  247.  
  248.           printf("Enter 1 for delete or Enter 2 for Edit..\n");
  249.           int d;
  250.           scanf("%d",&d);
  251.           if(2==d)
  252.           {
  253.            printf("Enter update Name:");scanf("%s", &temp-> name);
  254.            printf("Enter update Phone Number:");scanf("%s", &temp-> phn);
  255.            printf("Enter update Email Address:");scanf("%s", &temp-> email);
  256.            printf("Update Successfully\n\n");
  257.           }
  258.           else if(d==1)
  259.           {
  260.             if(temp==head)
  261.             {
  262.               head=temp->next;
  263.               printf("\nInformation of %s has deleted successfully \n",temp->name);
  264.               free(temp);
  265.               --count;
  266.             }
  267.             pre->next=temp->next;
  268.             printf("\nInformation of %s has deleted successfully \n",temp->name);
  269.             free(temp);
  270.             --count;
  271.           }
  272.  
  273.          }
  274. }
  275. }
  276. ///Main Body Section......
  277. int main()
  278. {
  279.     for(;;)
  280.     {
  281.     int k;
  282.     printf("\n1.Create list At Begin\n");
  283.     printf("2.Create list At End\n");
  284.     printf("3.Searching\n");
  285.     printf("4.delete or Update\n");
  286.     printf("5.Display list\n");
  287.     printf("6.Count list\n");
  288.     printf("7.Exit\n");
  289.  
  290.     scanf("%d",&k);
  291.          if(k==1){insertAtbigen();}
  292.     else if(k==2){insertAtend();}
  293.     else if(k==3){searchNode();}
  294.     else if(k==4){deleteeNode();}
  295.     else if(k==5){printList();}
  296.     else if(k==6){printf("Toatl List of Node ----- %d\n",count);}
  297.     else { break;}
  298.     }
  299.     return 0;
  300.  
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement