C-Language linked list write address Book

Source: Internet
Author: User
Tags strcmp

/*    implements an address book that can be used to store 1000 people's information, including: Name, gender, age, phone, Address Provision method: 1. Add contact information 2. Delete the specified contact information 3. Find the specified contact information 4. Modify the specified contact information 5. Display all contact information 6. Clear all contacts   */#include <stdio.h># Include "Phone_book.h" #include <string.h> #include <assert.h>typedef struct phone_book{char  name[10];char sex[5];char addr[20];int year;int phone_num;struct phone_book  *pnext;} Book,*pbook;pbook add_linkman (pbook head) {int i = 0;char name[10];char sex[ 5];char addr[20];int year;long phone_num;pbook pbase = head;assert (head); flag: printf ("How many contacts do you want to add:"); scanf ("%d", &i);if  (i <= 0) {printf ("Wrong number of inputs!") \ n "); goto flag;} if  (head->pnext == null) {while  (i--) {pbook pnew =  (pBook) malloc (sizeof ( book), assert (Pnew);p rintf ("Enter contact information: (Name, gender, address, age, phone number) \ n"), scanf ("%s",  name); scanf ("%s",  sex); scanf ("%s",  addr); strcpy (pnew->name, name); strcpy (Pnew->sex, sex); strcpy (PNEW-&GT;ADDR,&NBSP;ADDR); scanf ("%d",  &pnew->year); scanf ("%d" ,  &pnew->phone_num);p base->pnext = pnew;pnew->pnext = null;pbase =  pnew;}} else{while  (pbase->pnext != null) {pbase = pbase->pnext;} while  (i--) {pbook pnew =  (pbook) malloc (sizeof), assert (Pnew);p rintf (Enter contact information: (Name, Gender, Address, age, phone number) \ n "), scanf ("%s ",  name), scanf ("%s ",  sex), scanf ("%s ",  addr); strcpy (Pnew->name,  name); strcpy (pnew->sex, sex); strcpy (PNEW-&GT;ADDR,&NBSP;ADDR); scanf ("%d", &pnew-> year); scanf ("%d",  &pnew->phone_num);p base->pnext = pnew;pnew->pnext =  NULL;}} Return head;} Pbook remove_linkman (Pbook head) {pbook link_man = head;char name[10];if  (! Head) {printf ("No contacts, can't delete!") \ n "); return null;} printf ("which contact would you like to delete?")    "); scanf ("%s ", name);while  (link_Man->pnext!=null) {if  (strcmp (link_man->pnext->name, name)  == 0) {if  (link_ Man->pnext->pnext == null) {free (link_man->pnext); link_man->pnext = null;printf ("Delete done! \ n ");} Else{pbook ptmp = null;ptmp = link_man->pnext;link_man->pnext = link_ Man->pnext->pnext;free (ptmp);p rintf ("Delete done! \ n ");} Return head;} Else{link_man = link_man->pnext;}} printf ("The contact was not found!") \ n "); return head;} Void traver_linkman (Pbook head) {pbook link_man = head;if  (head->pNext ==  null) {printf ("Phone book is empty! \ n ");} while  (link_man->pnext!=null) {printf ("%s\n",  link_man->pnext->name);p rintf ("%s\n",  Link_man->pnext->sex);p rintf ("%s\n",  link_man->pnext->addr);p rintf ("%d\n", link_man-> Pnext->year);p rintf ("%ld\n",  link_man->pnext->phone_num);p rintf ("******************************* \ n "); Link_man = liNk_man->pnext;}} Void empty_linkman (pbook head) {Pbook ptmp = null;pbook link_man = head ;if  (head->pnext == null) {printf ("Phone book is empty! Don't empty it! (ˇ0ˇ)  \n ");} while  (link_man->pnext != null) {if  (link_man->pnext->pnext == null) { Free (link_man->pnext); link_man->pnext = null;} Else{ptmp = link_man->pnext;link_man->pnext = link_man->pnext->pnext;free (ptmp );}} printf ("Empty finished!" \ n ");} Pbook amend_linkman (Pbook head) { char name[10];char sex[5];char addr[20];p book  link_man = head;if  (!head) {printf ("No contacts, cannot be modified!") \ n "); return null;} printf ("Would you like to change which contact?")    scanf ("%s",  name);while  (link_man->pnext != null) {if  (strcmp (link_ Man->pnext->name, name) { == 0) {printf ("Enter contact information: (Name, gender, address, age, phone number) \ n"), scanf ("%s",  name); scanf ("%s",  sex); scanf ("%s",  addr); Strcpy (Link_man->pnext->name, name); strcpy (link_man->pnext->sex, sex); strcpy (link_man-> PNEXT-&GT;ADDR,&NBSP;ADDR); scanf ("%d",  &link_man->pnext->year); scanf ("%d",  &link_man- >pnext->phone_num); return head;} Elselink_man = link_man->pnext;} Return head;} Void search_linkman (Pbook head) {char name[10];p book link_man = head;if  (! Head) {printf ("No contacts, find failed!") \ n "); return null;} printf ("which contact are you looking for?")    scanf ("%s",  name);while  (link_man->pnext != null) {if  (strcmp (link_ Man->pnext->name, name) { == 0) {printf ("%s\n",  link_man->pnext->name);p rintf ("%s \ n ",  link_man->pnext->sex);p rintf ("%s\n ",  link_man->pnext->addr);p rintf ("%d\n ",  Link_man->pnext->year);p rintf ("%ld\n",  link_man->pnext->phone_num);p rintf ("****************** \ n "); return 0;} Elselink_man = lInk_man->pnext;} printf ("No find! \ n ");} Void start (pbook *phead) {Int i = -1;assert (Phead);while  (1) {printf ("Welcome to the phone book system!") Select your action: \ n ");p rintf (" *********************************************\n "   " * * ". Add contact information       2. Delete the specified contact information   *\n "   ". Find the specified contact information  4. Modify the specified contact information    *\n "   ". Show all contact information  6. Clear all Contacts       *\n "     "*0. End System!   ———— CIA-specific ————     *\n "                 "*********************************************\n"); scanf ("%d", &i); switch   (i) {case 0:printf ("* * * * * Thank you for using the System!*****\n"); exit (0); Case 1:*phead=add_linkman ( phead); break ; Case 2:*phead = remove_linkman (Phead); Break;case 3: search_linkman (phead); break;case  4:*phead = amend_linkman (Phead); Break;case 5:traver_linkman (Phead);Break;case 6:empty_linkman ( phead); break;default:printf ("Wrong choice!") Please re-select \ n "); #include <stdio.h> #include "phone_book.h" Int main () { pbook phead = null;start ( &phead); return 0;}


C-Language linked list write address Book

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.