Advertisement
nuray__alam

Doubly Linked List

Oct 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.21 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct node
  6. {
  7.     char name[30],phone[20];
  8.     struct node *next,*prev;
  9. }node;
  10. node *head=NULL,*tail=NULL;
  11.  
  12. void add_at_first()   //ADD_AT_FIRST_FUNCTION///////////////////////////////////////////////////////
  13. {
  14.     node *n=(node*)malloc(sizeof(node));
  15.     n->next=NULL; n->prev=NULL;
  16.     node *list=head;
  17.     printf("Enter the Name: ");
  18.     scanf(" %[^\n]",n->name);
  19.     printf("Enter the Phone Number: ");
  20.     scanf(" %[^\n]",n->phone);
  21.     printf("\n");
  22.     if(head==NULL){
  23.         head=n;
  24.         tail=n;
  25.     }
  26.     else {
  27.     n->next=head;
  28.     head->prev=n;
  29.     head=n;
  30.     while(list->next!=NULL){
  31.         list=list->next;
  32.     }
  33.     tail=list;
  34.     }
  35.     menu();
  36. }
  37.  
  38. void add_at_last()      //ADD_AT_LAST_FUNCTION///////////////////////////////////////////////////////
  39. {
  40.     node *n=(node*)malloc(sizeof(node));
  41.     n->next=NULL; n->prev=NULL;
  42.     node *list=head;
  43.     printf("Enter the Name: ");
  44.     scanf(" %[^\n]",n->name);
  45.     printf("Enter the Phone Number: ");
  46.     scanf(" %[^\n]",n->phone);
  47.     printf("\n");
  48.     if(head==NULL){
  49.         head=n;
  50.         tail=n;
  51.     }
  52.     else{
  53.         while(list->next!=NULL){
  54.             list=list->next;
  55.         }
  56.         list->next=n;
  57.         n->prev=list;
  58.         tail=n;
  59.     }
  60.     menu();
  61. }
  62.  
  63. void add_at_nth_pos()       //ADD_AT_nTH_POSITION_FUNCTION///////////////////////////////////////////
  64. {
  65.     int pos;
  66.     node *n=(node*)malloc(sizeof(node));
  67.     n->prev=NULL; n->next=NULL;
  68.     node *list=head;
  69.     printf("Enter the Position: ");
  70.     scanf("%d",&pos);
  71.     printf("\n");
  72.     printf("Enter the Name: ");
  73.     scanf(" %[^\n]",n->name);
  74.     printf("Enter the Phone Number: ");
  75.     scanf(" %[^\n]",n->phone);
  76.     printf("\n");
  77.  
  78.     if(pos==1){
  79.         if(head==NULL){
  80.             head=n;
  81.             tail=n;
  82.         }
  83.         else{
  84.             n->next=head;
  85.             head->prev=n;
  86.             head=n;
  87.         }
  88.     }
  89.     else{
  90.          if(head==NULL){
  91.             head=n;
  92.             tail=n;
  93.         }
  94.         else{
  95.             pos=pos-2;
  96.             while(pos!=0){
  97.                     if(list==NULL){
  98.                         printf("There is no such Position that you entered.\n\n");
  99.                         menu();
  100.                         break;
  101.                     }
  102.                     else{
  103.                 list=list->next;
  104.                 pos--;}
  105.             }
  106.             if(list->next==NULL){
  107.                 list->next=n;
  108.                 n->prev=list;
  109.                 tail=n;
  110.             }
  111.             else{
  112.                 n->next=list->next; n->prev=list; n->next->prev=n;
  113.             }
  114.  
  115.         }
  116.     }
  117.     printf("Your new data is added successfully\n\n");
  118.     menu();
  119.  
  120. }
  121. void add()      //ADD_Function///////////////////////////////////////////////////////////////////////
  122. {
  123.     int option;
  124.     printf("\t\t\t\t\t1.Insert at First\t\t2.Insert at Last\n\n");
  125.     printf("\t\t\t\t\t\t   3.Insert at n'th Position\n\n");
  126.     printf("Choose an option: ");
  127.     scanf("%d",&option);
  128.     printf("\n\n");
  129.     if(option==1){
  130.         add_at_first();
  131.     }
  132.     else if(option==2){
  133.         add_at_last();
  134.     }
  135.     else if(option==3){
  136.         add_at_nth_pos();
  137.     }
  138.     else{
  139.         printf("You entered a wrong option!! Automatically terminating to menu.\n\n");
  140.         menu();
  141.     }
  142.  
  143. }
  144.  
  145.  
  146. void display()      //ALL_DATA_DISPLAY_FUNCTION//////////////////////////////////////////////////////////////
  147. {
  148.     node *list=head;
  149.     while(list!=NULL){
  150.         printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
  151.         list=list->next;
  152.     }
  153.  
  154.     printf("Reverse Printing:\n\n");
  155.     list=tail;
  156.     while(list!=NULL){
  157.         printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
  158.         list=list->prev;
  159.     }
  160.     menu();
  161. }
  162.  
  163. delete_by_name()      //DELETE_BY_NAME/////////////////////////////////////////////////////
  164. {
  165.     node *list=head; node *temp;
  166.     char testname[30];
  167.     int match=0;
  168.     printf("Enter the name you want to Delete: ");
  169.     scanf(" %[^\n]",testname);
  170.     printf("\n\n");
  171.     printf("The value of difference: %d\n\n",strcmp(list->name,testname));
  172.     if(strcmp(list->name,testname)==0){// if match a head
  173.         if(head->next==NULL){
  174.             free(head);
  175.             head=NULL; tail=NULL;
  176.             printf("The data is deleted successfully.\n\n");
  177.             menu();
  178.         }
  179.         else{
  180.             head=list->next;
  181.             head->prev=NULL;
  182.             free(list);
  183.             list=head;
  184.             while(list->next!=NULL){
  185.                 list=list->next;
  186.             }
  187.             tail=list;
  188.             printf("The data is deleted successfully.\n\n");
  189.             menu();
  190.         }
  191.     }
  192.     else{
  193.         while(list->next!=NULL){
  194.             if(strcmp(list->next->name,testname)==0){
  195.                 match++;
  196.                 break;
  197.             }
  198.             list=list->next;
  199.         }
  200.  
  201.         if(match==0){
  202.             printf("There is no matching data.\n\n");
  203.             menu();
  204.         }
  205.         else{
  206.             temp=list->next;
  207.             if(temp->next==NULL){
  208.                 tail=list;
  209.                 free(temp);
  210.                 printf("The data is deleted successfully.\n\n");
  211.                 menu();
  212.             }
  213.             else{
  214.                 temp=list->next;
  215.                 list->next=temp->next;
  216.                 temp->next->prev=list;
  217.                 free(temp);
  218.                 while(list->next!=NULL){
  219.                     list=list->next;
  220.                 }
  221.                 tail=list;
  222.                 printf("The data is deleted successfully.\n\n");
  223.             }
  224.         }
  225.     }
  226.  
  227.  
  228. }
  229.  
  230. delete_by_phone_number()      //DELETE_BY_PHONE_NUMBER/////////////////////////////////////////////////////
  231. {
  232.     node *list=head; node *temp;
  233.     char testphone[30];
  234.     int match=0;
  235.     printf("Enter the Phone Number you want to Delete: ");
  236.     scanf(" %[^\n]",testphone);
  237.     printf("\n\n");
  238.     printf("The value of difference: %d\n\n",strcmp(list->phone,testphone));
  239.     if(strcmp(list->phone,testphone)==0){// if match a head
  240.         if(head->next==NULL){
  241.             free(head);
  242.             head=NULL; tail=NULL;
  243.             printf("The data is deleted successfully.\n\n");
  244.             menu();
  245.         }
  246.         else{
  247.             head=list->next;
  248.             head->prev=NULL;
  249.             free(list);
  250.             list=head;
  251.             while(list->next!=NULL){
  252.                 list=list->next;
  253.             }
  254.             tail=list;
  255.             printf("The data is deleted successfully.\n\n");
  256.             menu();
  257.         }
  258.     }
  259.     else{
  260.         while(list->next!=NULL){
  261.             if(strcmp(list->next->phone,testphone)==0){
  262.                 match++;
  263.                 break;
  264.             }
  265.             list=list->next;
  266.         }
  267.  
  268.         if(match==0){
  269.             printf("There is no matching data.\n\n");
  270.             menu();
  271.         }
  272.         else{
  273.             temp=list->next;
  274.             if(temp->next==NULL){
  275.                 tail=list;
  276.                 free(temp);
  277.                 printf("The data is deleted successfully.\n\n");
  278.                 menu();
  279.             }
  280.             else{
  281.                 temp=list->next;
  282.                 list->next=temp->next;
  283.                 temp->next->prev=list;
  284.                 free(temp);
  285.                 while(list->next!=NULL){
  286.                     list=list->next;
  287.                 }
  288.                 tail=list;
  289.                 printf("The data is deleted successfully.\n\n");
  290.             }
  291.         }
  292.     }
  293.  
  294.  
  295. }
  296.  
  297. void delete_by_nth_position()        //DELETE_BY_nth_POSTION//////////////////////////////////////////////
  298. {
  299.     int position,match=0;
  300.     node *list=head; node *temp;
  301.     printf("Enter the Position to delete the data: ");
  302.     scanf("%d",&position);
  303.     printf("\n\n");
  304.     if(position==1){
  305.         if(head->next==NULL){
  306.             free(head);
  307.             head=NULL; tail=NULL;
  308.             menu();
  309.         }
  310.         else{
  311.             head=head->next;
  312.             head->prev=NULL;
  313.             free(list);
  314.             printf("The data is deleted successfully.\n\n");
  315.             menu();
  316.             }
  317.     }
  318.     else{
  319.         position=position-2;
  320.         while(position!=0){
  321.             if(list==NULL){
  322.                 printf("There is no such Position that you entered.\n\n");
  323.                 menu();
  324.                 break;
  325.             }
  326.             else{
  327.                 position--;
  328.             }
  329.         }
  330.         temp=list->next;
  331.         if(temp->next==NULL){
  332.             list->next=NULL;
  333.             tail=list;
  334.             free(temp);
  335.             printf("The data is deleted successfully.\n\n");
  336.             menu();
  337.         }
  338.         else{
  339.             temp=list->next; temp->next->prev=list;
  340.             free(temp);
  341.             printf("The data is deleted successfully.\n\n");
  342.             menu();
  343.         }
  344.     }
  345. }
  346.  
  347. void Delete()    //DELETE_FUNCTION///////////////////////////////////////////////////////////////////
  348.  
  349. {
  350.     int option;
  351.     char name[40];
  352.     printf("\t\t\t\t\t1.Delete by Name\t\t2.Delete by Phone Number\n\n\t\t\t\t\t\t   3.Delete by nth Position.\n\n");
  353.     printf("Choose an option: ");
  354.     scanf("%d",&option);
  355.     printf("\n");
  356.  
  357.     if(option==1){
  358.         delete_by_name();
  359.     }
  360.     else if(option==2){
  361.         delete_by_phone_number();
  362.     }
  363.     else if(option==3){
  364.         delete_by_nth_position();
  365.     }
  366.     else{
  367.          printf("You entered an wrong option. Automatically terminating to menu\n\n");
  368.         menu();
  369.     }
  370.  
  371. }
  372.  
  373. void search_by_name()       //SEARCH_BY_NAME_FUNCTION////////////////////////////////////////////////
  374. {
  375.     int match=0;
  376.     node *list=head;
  377.     char testname[30];
  378.     printf("Enter the name you want to search: ");
  379.     scanf(" %[^\n]",testname);
  380.     printf("\n");
  381.     while(list!=NULL){
  382.             if(strcmp(list->name,testname)==0){
  383.                 printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
  384.                 match++;
  385.             }
  386.         list=list->next;
  387.     }
  388.     if(match==0){
  389.         printf("No match found with %s\n",testname);
  390.     }
  391.     menu();
  392. }
  393.  
  394. void search_by_phone_number()       //SEARCH_BY_PHONE_NUMBER_FUNCTION
  395. {
  396.      int match=0;
  397.     node *list=head;
  398.     char testnumber[30];
  399.     printf("Enter the Phone Number you want to search: ");
  400.     scanf(" %[^\n]",testnumber);
  401.     printf("\n");
  402.     while(list!=NULL){
  403.             if(strcmp(list->phone,testnumber)==0){
  404.                 printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
  405.                 match++;
  406.             }
  407.         list=list->next;
  408.     }
  409.     if(match==0){
  410.         printf("No match found with %s\n",testnumber);
  411.     }
  412.     menu();
  413. }
  414.  
  415.  
  416.  
  417. void search_by_nth_position()       //SEARCH_BY_nTH_POSITION/////////////////////////////////////////
  418. {
  419.     int match=0,position;
  420.     node *list=head;
  421.     printf("Enter the position you want to search: ");
  422.     scanf("%d",&position);
  423.     printf("\n");
  424.  
  425.     if(position==1){
  426.         printf("Name: %s\nPhone Number: %s\n\n",head->name,head->phone);
  427.         menu();
  428.     }
  429.     else{
  430.         position=position-2;
  431.         while(position!=0){
  432.             if(list==NULL){
  433.                 printf("There is no such position that you entered.\n\n");
  434.                 menu();
  435.                 break;
  436.             }
  437.             list=list->next;
  438.         }
  439.         printf("Name: %s\nPhone Number: %s\n\n",list->name,list->phone);
  440.         menu();
  441.     }
  442.  
  443. }
  444.  
  445.  
  446. void search()       //SEARCH_FUNCTION/////////////////////////////////////////////////////////////
  447. {
  448.     int option;
  449.     printf("\t\t\t\t\t1.Search by name\t\t2.Search by phone number\n\n\t\t\t\t\t\t   3.Search Position.\n\n");
  450.      printf("Choose an option: ");
  451.     scanf("%d",&option);
  452.     printf("\n\n");
  453.     if(option==1){
  454.         search_by_name();
  455.     }
  456.     else if(option==2){
  457.         search_by_phone_number();
  458.     }
  459.     else if(option==3){
  460.         search_by_nth_position();
  461.     }
  462.  
  463. }
  464.  
  465.  
  466.  
  467. void menu()     //Menu_FUNCTION//////////////////////////////////////////////////////////////////////
  468. {
  469.     int option;
  470.     printf("\t\t\t\t\t1.Add New Data\t\t2.Delete A Data\n\n");
  471.     printf("\t\t\t\t\t\t   3.End The Program\n\n");
  472.     printf("\t\t\t\t\t4.Search A Data\t\t5.Display all data\n\n");
  473.     printf("Choose an option: ");
  474.     scanf("%d",&option);
  475.     printf("\n\n");
  476.     if(option==1){
  477.         add();
  478.     }
  479.     else if(option==5){
  480.             if(head==NULL){
  481.                 printf("There is no data to display.\n\n");
  482.                 menu();
  483.             }else{
  484.         display();}
  485.     }
  486.     else if(option==2){
  487.         if(head==NULL)
  488.         {
  489.             printf("There is no data to delete.\n\n");
  490.             menu();
  491.                 }
  492.        else{ Delete();}
  493.     }
  494.    else if(option==3){
  495.         printf("Thank you for using the program!!\n");
  496.     }
  497.     else if(option==4){
  498.         if(head==NULL){
  499.             printf("There is no data to search.\n\n");
  500.             menu();
  501.         }
  502.         else{
  503.             search();
  504.         }
  505.     }
  506.     else{
  507.         printf("You entered an wrong option. Automatically terminating to menu\n\n");
  508.         menu();
  509.     }
  510.  
  511.     return;
  512. }
  513.  
  514.  
  515. int main()
  516. {
  517.     menu();
  518.     return 0;
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement