Sreekar_0125

Updated Phonebook Code

Sep 6th, 2022
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. struct node
  5. {
  6.     char number[100];
  7.     char gmail[100];
  8.     char name[100];
  9.     struct node *prev, *next;
  10. } *head = NULL;
  11.  
  12. void insert()
  13. {
  14.     char number[100];
  15.     char gmail[100];
  16.     char name[100];
  17.     char ans;
  18.     do
  19.     {
  20.         struct node *p;
  21.         p = (struct node *)malloc(sizeof(struct node));
  22.         printf("Enter The Name of The Contact: ");
  23.         scanf("%[^\n]", name);
  24.         fflush(stdin);
  25.         strcpy(p->name, name);
  26.         printf("Enter The Phone Number    :\t");
  27.         scanf("%[^\n]", number);
  28.         fflush(stdin);
  29.  
  30.         while (strlen(number) != 10)
  31.         {
  32.             printf("ENTER A VALID 10 DIGIT NUMBER  :\t");
  33.             scanf("%[^\n]", number);
  34.             fflush(stdin);
  35.         }
  36.         strcpy(p->number, number);
  37.         printf("Enter The G-Mail ID  :\t");
  38.         scanf("%[^\n]", gmail);
  39.         strcpy(p->gmail, gmail);
  40.         fflush(stdin);
  41.  
  42.         if (head == NULL)
  43.         {
  44.             p->next = NULL;
  45.             p->prev = NULL;
  46.             head = p;
  47.         }
  48.         else
  49.         {
  50.             struct node *tmp = head;
  51.             while (tmp->next != NULL)
  52.             {
  53.                 tmp = tmp->next;
  54.             }
  55.             p->next = NULL;
  56.             p->prev = tmp;
  57.             tmp->next = p;
  58.         }
  59.         printf("Do You Want To Continue?????????\n");
  60.         scanf("%c", &ans);
  61.         fflush(stdin);
  62.     } while (ans == 'y');
  63. }
  64. void display()
  65. {
  66.     struct node *tmp = head;
  67.     while (tmp != NULL)
  68.     {
  69.         printf("\nNAME  ::  %s", tmp->name);
  70.         printf("\nNUMBER::  +91-%s", tmp->number);
  71.         printf("\nG-MAIL::  %s\n", tmp->gmail);
  72.         tmp = tmp->next;
  73.     }
  74. }
  75. void sort()
  76. {
  77.     struct node *i, *j;
  78.     int temp;
  79.     char a[100], b[100], c[100];
  80.     for (i = head; i->next != NULL; i = i->next)
  81.     {
  82.         for (j = i->next; j != NULL; j = j->next)
  83.         {
  84.             temp = strcmp(i->name, j->name);
  85.             if (temp > 0)
  86.             {
  87.                 strcpy(a, i->name);
  88.                 strcpy(i->name, j->name);
  89.                 strcpy(j->name, a);
  90.                 strcpy(b, i->number);
  91.                 strcpy(i->number, j->number);
  92.                 strcpy(j->number, b);
  93.                 strcpy(c, i->gmail);
  94.                 strcpy(i->gmail, j->gmail);
  95.                 strcpy(j->gmail, c);
  96.             }
  97.         }
  98.     }
  99. }
  100. void deletecontact()
  101. {
  102.     char s[100];
  103.     printf("Enter the Name of the Contact You Wish to Delete\n");
  104.     scanf("%[^\n]", s);
  105.     fflush(stdin);
  106.     int c = 0;
  107.     struct node *tmp = head;
  108.     while (tmp != NULL)
  109.     {
  110.         if (strcmp(s, tmp->name) == 0)
  111.         {
  112.             c = 1;
  113.             break;
  114.         }
  115.         else
  116.         {
  117.             c = 2;
  118.         }
  119.         tmp = tmp->next;
  120.     }
  121.     if (!tmp)
  122.     {
  123.         printf("CONTACT NOT FOUND\n");
  124.         return;
  125.     }
  126.     if (c == 1 && tmp != head && tmp->next != NULL)
  127.     {
  128.         tmp->prev->next = tmp->next;
  129.         tmp->next->prev = tmp->prev;
  130.         free(tmp);
  131.         printf("YOUR CONTACT IS SUCCESSFULLY DELETED\n\n");
  132.     }
  133.     if (tmp == head)
  134.     {
  135.         head = head->next;
  136.         head->prev = NULL;
  137.         free(tmp);
  138.         printf("YOUR CONTACT IS SUCCESSFULLY DELETED\n\n");
  139.     }
  140.     if (tmp->next == NULL)
  141.     {
  142.         tmp->prev->next = NULL;
  143.         tmp->prev = NULL;
  144.         free(tmp);
  145.         printf("YOUR CONTACT IS SUCCESSFULLY DELETED\n\n");
  146.     }
  147. }
  148. void deletesamename()
  149. {
  150.     struct node *tmp1 = head;
  151.     int n = 0;
  152.     while (tmp1 != NULL && tmp1->next != NULL)
  153.     {
  154.         struct node *tmp2 = tmp1;
  155.         while (tmp2->next != NULL)
  156.         {
  157.             if (strcmp(tmp1->name, tmp2->next->name) == 0)
  158.             {
  159.                 struct node *dup = tmp2->next;
  160.                 tmp2->next = tmp2->next->next;
  161.                 free(dup);
  162.                 n++;
  163.             }
  164.             else
  165.             {
  166.                 tmp2 = tmp2->next;
  167.             }
  168.         }
  169.         tmp1 = tmp1->next;
  170.     }
  171.     if (!n)
  172.         printf("Same Names Not Found\n");
  173.     else
  174.         display();
  175. }
  176. void deletesamegmail()
  177. {
  178.     struct node *tmp1 = head;
  179.     int n = 0;
  180.     while (tmp1 != NULL && tmp1->next != NULL)
  181.     {
  182.         struct node *tmp2 = tmp1;
  183.         while (tmp2->next != NULL)
  184.         {
  185.             if (strcmp(tmp1->gmail, tmp2->next->gmail) == 0)
  186.             {
  187.                 struct node *dup = tmp2->next;
  188.                 tmp2->next = tmp2->next->next;
  189.                 free(dup);
  190.                 n++;
  191.             }
  192.             else
  193.             {
  194.                 tmp2 = tmp2->next;
  195.             }
  196.         }
  197.         tmp1 = tmp1->next;
  198.     }
  199.     if (!n)
  200.         printf("Same G-Mail IDs Not Found\n");
  201.     else
  202.         display();
  203. }
  204. void deletesamenumber()
  205. {
  206.     struct node *tmp1 = head;
  207.     int n = 0;
  208.     while (tmp1 != NULL && tmp1->next != NULL)
  209.     {
  210.         struct node *tmp2 = tmp1;
  211.         while (tmp2->next != NULL)
  212.         {
  213.             if (strcmp(tmp1->number, tmp2->number) == 0)
  214.             {
  215.                 struct node *dup = tmp2->next;
  216.                 tmp2->next = tmp2->next->next;
  217.                 free(dup);
  218.                 n++;
  219.             }
  220.             else
  221.             {
  222.                 tmp2 = tmp2->next;
  223.             }
  224.         }
  225.         tmp1 = tmp1->next;
  226.     }
  227.     if (!n)
  228.         printf("Same Numbers Not Found\n");
  229.     else
  230.         display();
  231. }
  232. void searchbyname(char *na)
  233. {
  234.     struct node *tmp = head;
  235.     while (tmp != NULL)
  236.     {
  237.         if (strcmp(na, tmp->name) == 0)
  238.         {
  239.             printf("NAME FOUND\n");
  240.             printf("CONTACT DETAILS ARE BELOW:\n");
  241.             printf("\n\nNAME  ::\t%s", tmp->name);
  242.             printf("\nNUMBER::\t+91-%s", tmp->number);
  243.             printf("\nG-MAIL::\t%s", tmp->gmail);
  244.         }
  245.         tmp = tmp->next;
  246.     }
  247.     if (!tmp)
  248.         printf("CONTACT NOT FOUND\n");
  249. }
  250. void searchbynumber(char *num)
  251. {
  252.     struct node *tmp = head;
  253.     while (tmp != NULL)
  254.     {
  255.         if (strcmp(num, tmp->number) == 0)
  256.         {
  257.             printf("NUMBER FOUND\n");
  258.             printf("CONTACT DETAILS ARE BELOW:\n");
  259.             printf("\n\nNAME  ::\t%s", tmp->name);
  260.             printf("\nNUMBER::\t+91-%s", tmp->number);
  261.             printf("\nG-MAIL::\t%s", tmp->gmail);
  262.         }
  263.         tmp = tmp->next;
  264.     }
  265.     if (!tmp)
  266.         printf("CONTACT NOT FOUND\n");
  267. }
  268. void searchbygmail(char *gm)
  269. {
  270.     struct node *tmp = head;
  271.     while (tmp != NULL)
  272.     {
  273.         if (strcmp(gm, tmp->gmail) == 0)
  274.         {
  275.             printf("G-MAIL FOUND\n");
  276.             printf("CONTACT DETAILS ARE BELOW:\n");
  277.             printf("\n\nNAME  ::\t%s", tmp->name);
  278.             printf("\nNUMBER::\t+91-%s", tmp->number);
  279.             printf("\nG-MAIL::\t%s", tmp->gmail);
  280.         }
  281.         tmp = tmp->next;
  282.     }
  283.     if (!tmp)
  284.         printf("CONTACT NOT FOUND\n");
  285. }
  286. void update()
  287. {
  288.     char n[100];
  289.     char ans;
  290.     int c;
  291.     struct node *tmp = head;
  292.     printf("\nEnter the Name of the Contact that You Wish to Update\n");
  293.     scanf("%[^\n]", n);
  294.     fflush(stdin);
  295.     while (tmp != NULL)
  296.     {
  297.         if (strcmp(n, tmp->name) == 0)
  298.         {
  299.  
  300.             do
  301.             {
  302.                 printf("\nWHAT DO YOU WANT TO UPDATE?\n1.NAME\n2.PHONE NUMBER\n3.G-MAIL\n");
  303.                 scanf("%d", &c);
  304.                 fflush(stdin);
  305.                 switch (c)
  306.                 {
  307.                 case 1:
  308.                     printf("Enter The New Name:\n");
  309.                     scanf("%[^\n]", tmp->name);
  310.                     fflush(stdin);
  311.                     break;
  312.                 case 2:
  313.                     printf("Enter the New Phone Number:\n");
  314.                     scanf("%[^\n]", tmp->number);
  315.                     fflush(stdin);
  316.                     while (strlen(tmp->number) != 10)
  317.                     {
  318.                         printf("ENTER A VALID NUMBER!  :\n");
  319.                         scanf("%[^\n]", tmp->number);
  320.                         fflush(stdin);
  321.                     }
  322.                     break;
  323.                 case 3:
  324.                     printf("Enter The New G-Mail ID:\n");
  325.                     scanf("%[^\n]", tmp->gmail);
  326.                     fflush(stdin);
  327.                     break;
  328.                 }
  329.                 printf("Do You Wish To Continue Updating?\n");
  330.                 scanf("%c", &ans);
  331.                 fflush(stdin);
  332.             } while (ans == 'y');
  333.         }
  334.         tmp = tmp->next;
  335.     }
  336. }
  337. int main()
  338. {
  339.     char n[100];
  340.     char nam[100];
  341.     char name[100];
  342.     char number[100];
  343.     char gmail[100];
  344.     char ans;
  345.     int ch, a;
  346.     printf("**************                                PHONE BOOK                          ********************\n");
  347.     printf("\n\nWHAT IS YOUR NAME?\n");
  348.     scanf("%[^\n]", name);
  349.     fflush(stdin);
  350.     printf("\n\n!!!!!!!!!!!!!!!!!!!!!!!   Welcome %s  !!!!!!!!!!!!!!!!!!!!!!! \n", name);
  351.     printf("\n\n\nLET'S CREATE OUR PHONEBOOK \n\n");
  352.     insert();
  353.     sort();
  354.     fflush(stdin);
  355.     while (1)
  356.     {
  357.         printf("\n\n\n\n1) DISPLAY YOUR PHONE BOOK\n2) INSERT NEW CONTACT\n3) UPDATE DETAILS ON EXISTING CONTACT\n4) DELETE CONTACT\n5) DELETE SAME NAME IN PHONEBOOK\n6) DELETE SAME NUMBERS IN PHONEBOOK\n7) DELETE SAME GMAIL-ID IN PHONEBOOK\n8) SEARCH\n");
  358.         scanf("%d", &ch);
  359.         fflush(stdin);
  360.         switch (ch)
  361.         {
  362.         case 1:
  363.             display();
  364.             break;
  365.  
  366.         case 2:
  367.             insert();
  368.             sort();
  369.             break;
  370.         case 3:
  371.             update();
  372.             sort();
  373.             break;
  374.         case 4:
  375.             deletecontact();
  376.             break;
  377.  
  378.         case 5:
  379.             deletesamename();
  380.             break;
  381.  
  382.         case 6:
  383.             deletesamenumber();
  384.             break;
  385.         case 8:
  386.             do
  387.             {
  388.                 printf("1.SEARCH BY NAME\n2.SEARCH BY NUMBER\n3.SEARCH BY GMAIL\n");
  389.                 scanf("%d", &a);
  390.                 fflush(stdin);
  391.                 switch (a)
  392.                 {
  393.                 case 1:
  394.                     printf("\nENTER THE NAME TO BE SEARCHED\n");
  395.                     scanf("%[^\n]", name);
  396.                     fflush(stdin);
  397.                     searchbyname(name);
  398.                     break;
  399.                 case 2:
  400.                     printf("ENTER THE NUMBER TO BE SEARCHED\n");
  401.                     scanf("%[^\n]", number);
  402.                     fflush(stdin);
  403.                     searchbynumber(number);
  404.                     break;
  405.                 case 3:
  406.                     printf("ENTER THE GMAIL ID TO BE SEARCHED\n");
  407.                     scanf("%[^\n]", gmail);
  408.                     fflush(stdin);
  409.                     searchbygmail(gmail);
  410.                     break;
  411.                 default:
  412.                     printf("\nNO PROPER INPUT GIVEN.....\n");
  413.                 }
  414.                 printf("\nDO YOU WANT TO CONTINUE SEARCHING?????????\n");
  415.                 scanf("%c", &ans);
  416.                 fflush(stdin);
  417.             } while (ans == 'y');
  418.  
  419.             break;
  420.         case 7:
  421.             deletesamegmail();
  422.             break;
  423.         default:
  424.             printf("\nENTER A VALID INPUT\n");
  425.             break;
  426.         }
  427.  
  428.         printf("\n\nDO YOU WANT TO CONTINUE OPERATIONS?????????\n");
  429.         scanf("%c", &ans);
  430.         if (ans != 'y')
  431.             break;
  432.         fflush(stdin);
  433.     }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment