Advertisement
nuray__alam

all linked list

Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct node                 //STRUCTURE///////////////////////////////////////////////////
  5. {
  6.     char name[30];
  7.     char phone[20];
  8.     struct node *next;
  9. } node;
  10. node *head=NULL;
  11.  
  12. int Add()                          // ADD FUNCTION////////////////////////////////////////////////
  13. {
  14.     int pos,option;
  15.     printf("\t\t\t\t1.Add at First Position\t\t2.Add at Last Position\n");
  16.     printf("\t\t\t\t\t\t  3.Add at n'th Position\n\n");
  17.     scanf("%d",&option);
  18.     if(option==1)
  19.     {
  20.         node *n=(node*)malloc(sizeof(node));  //ADD___AT___FIRST///////////////////////////////////
  21.         n->next=NULL;
  22.         printf("Enter The Name:\n\n");
  23.         scanf(" %[^\n]",n->name);
  24.         printf("Enter The Phone Number:\n\n");
  25.         scanf(" %[^\n]",n->phone);
  26.         n->next=head;
  27.         head=n;
  28.         menu();
  29.  
  30.     }
  31.     else if(option==2)                   //ADD__AT__LAST//////////////////////////////////////////////////
  32.     {
  33.         node *n=(node*)malloc(sizeof(node));
  34.         n->next=NULL;
  35.         printf("\t\t\t\t\t\tEnter The Name:\n\n");
  36.         scanf(" %[^\n]",n->name);
  37.         printf("\t\t\t\t\t\tEnter The Phone Number:\n\n");
  38.         scanf(" %[^\n]",n->phone);
  39.         node *list=head;
  40.         if(list==NULL)
  41.         {
  42.             head=n;
  43.             printf("\t\t\t\t\t\ Successfully Added to the last Position\n\n");
  44.             menu();
  45.  
  46.         }
  47.         else
  48.         {
  49.             while(list->next!=NULL)
  50.             {
  51.                 list=list->next;
  52.             }
  53.             list->next=n;
  54.             menu();
  55.         }
  56.     }
  57.     else if(option==3)       //ADD__AT__nth__POSITION////////////////////////////////////////////////////
  58.     {
  59.         printf("\t\t\t\t\t\tEnter the position:\n\n");
  60.         scanf("%d",&pos);
  61.         node *n=(node*)malloc(sizeof(node));
  62.         n->next=NULL;
  63.         printf("\t\t\t\t\t\tEnter The Name:\n\n");
  64.         scanf(" %[^\n]",n->name);
  65.         printf("\t\t\t\t\t\tEnter The Phone Number:\n\n");
  66.         scanf(" %[^\n]",n->phone);
  67.         node *list=head;
  68.         if(pos==1)
  69.         {
  70.             n->next=head;
  71.             head=list;
  72.             menu();
  73.         }
  74.         else if(pos%1==0)
  75.         {
  76.             pos=pos-2;
  77.             while(pos!=0 && list->next!=NULL)
  78.             {
  79.                 list=list->next;
  80.             }
  81.             n->next=list->next;
  82.             list->next=n;
  83.             menu();
  84.  
  85.         }
  86.  
  87.     }
  88.     else
  89.     {
  90.  
  91.         printf("\t\t\t\t\t\tYou have entered a wrong command, Follow the steps again\n\n");
  92.         Add();
  93.  
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. int Delete()           //DELETE__FUNCTION///////////////////////////////////////////////////////////
  100. {
  101.     int option,pos;
  102.     char test[30];
  103.     node *list,*temp;
  104.     printf("\t\t\t\t\t\t1.Delete by Position\t\t2.Delete by Matching Name\n\n");
  105.     scanf("%d",&option);
  106.     if(option==1)           //DELETE__BY__POSITION/////////////////////////////////////////////////////
  107.     {
  108.         printf("\t\t\t\t\t\tEnter the Position\n\n");
  109.         scanf("%d",&pos);
  110.         if(pos==1)
  111.         {
  112.             temp=head;
  113.             head=head->next;
  114.             free(temp);
  115.             printf("\t\t\t\t\t\tSuccessfully Deleted the Data\a\n");
  116.             menu();
  117.         }
  118.         else
  119.         {
  120.             pos=pos-2;
  121.             list=head;
  122.             while(pos!=0 && list->next!=NULL)
  123.             {
  124.                 list=list->next;
  125.                 pos--;
  126.                 if(list==NULL){
  127.                     printf("There is No %dth Position\n\n",pos);
  128.                     menu();                }
  129.             }
  130.             temp=list->next;
  131.             list->next=temp->next;
  132.             free(temp);
  133.             printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
  134.             menu();
  135.         }
  136.         menu();
  137.  
  138.     }
  139.     else if(option==2)           //DELETE__BY__Name/////////////////////////////////////////////////
  140.     {
  141.         printf("\t\t\t\t\t\tEnter the Name you want to Delete\n\n");
  142.         scanf(" %[^\n]",test);
  143.         list=head;
  144.         if(strcmp(test,list->name)==0)
  145.         {
  146.             head=list->next;
  147.             free(list);
  148.             printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
  149.             menu();
  150.         }
  151.         else
  152.         {
  153.             list=head;
  154.             while(strcmp(list->next->name,test)!=0 && list!=NULL)
  155.             {
  156.                 list=list->next;
  157.             }
  158.             temp=list->next;
  159.             list->next=temp->next;
  160.             free(temp);
  161.             printf("\t\t\t\t\t\tSuccessfully Deleted the Data\n");
  162.             menu();
  163.         }
  164.     }
  165.     else
  166.     {
  167.         printf("\t\t\t\t\t\tYou have entered a wrong command, Follow the steps again\a\n\n");
  168.         Delete();
  169.     }
  170.     }
  171. int display()          //DISPLAY__FUNCTION/////////////////////////////////////////////
  172. {
  173.     int count=0;
  174.     node *list=head;
  175.     while(list!=NULL)
  176.     {
  177.         count++;
  178.         printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
  179.         list=list->next;
  180.     }
  181.     menu();
  182. }
  183.  
  184. int search()           //////Search_FUNCTON////////////////////////////////////
  185. {
  186.     char test[30];
  187.     int option,count=0;
  188.     node *list=head;
  189.     printf("\t\t\t\t1.Search by name\t\t2.Search by Phone number\n");
  190.     scanf("%d",&option);
  191.     if (option==1){             //////SEARCH_BY_NAME////////////////////////////////////////////
  192.         printf("\t\t\t\t\t\tEnter the name\n\n");
  193.         scanf(" %[^\n]",test);
  194.         while(list!=NULL){
  195.             if(strcmp(test,list->name)==0){
  196.                 printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
  197.                 count++;
  198.             }
  199.             list=list->next;
  200.  
  201.         }
  202.         if(count==0){
  203.             printf("\t\t\t\t\t\tNot Found Your Data\a\n\n");
  204.             menu();
  205.         }
  206.         menu();
  207.     }
  208.     if(option==2){   // Search_BY_PHONE_NUMBER////////////////////////////////////////////////////
  209.         printf("Enter The Phone Number:\n\n");
  210.         scanf(" %[^\n]",test);
  211.         while(list!=NULL){
  212.             if(strcmp(test,list->phone)==0){
  213.                 printf("\t\t\t\t\t\t%d NO. BLOCK\n\t\t\t\t\t\tName : %s\n\t\t\t\t\t\tPhone Number : %s\n",count,list->name,list->phone);
  214.                 count++;
  215.             }
  216.             list=list->next;
  217.         }
  218.         if(count==0){
  219.             printf("\t\t\t\t\t\tNot Found Your Data\a\n\n");
  220.             menu();
  221.         }
  222.         menu();
  223.     }
  224.     else{
  225.         printf("\t\t\t\t\t\tYou have Entered wrong command, Automatically terminated to Menu\n\n");
  226.         menu();
  227.     }
  228.     menu();
  229.  
  230. }
  231.  
  232. int menu()
  233. {
  234.     int n;
  235.     printf("\n\n");
  236.     printf("\t\t\t\t1.Add a new Contact\t\t2.Delete an Contact\n");
  237.     printf("\t\t\t\t3.Search an Contact\t\t4.Display all Contact\n");
  238.     printf("\t\t\t\t\t\t  5.End The Program\n\n\n\n");
  239.     scanf("%d",&n);
  240.     if(n==1)
  241.     {
  242.         Add();
  243.     }
  244.     else if(n==2)
  245.     {
  246.         Delete();
  247.     }
  248.     else if(n==3){
  249.         search();
  250.     }
  251.     else if(n==4)
  252.     {
  253.         display();
  254.     }
  255.     else if(n==5)
  256.     {
  257.         exit(0);
  258.     }
  259.     else
  260.     {
  261.         printf("\t\t\t\t\t\tYou have Entered wrong command, Automatically terminated to Menu\n\n");
  262.         menu();
  263.     }
  264.  
  265. }
  266.  
  267. int main()
  268. {
  269.     menu();
  270.     return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement