Write Address Book in C linked list

Source: Internet
Author: User

Write Address Book in C linked list

/* Implement an address book. The address book can be used to store information of 1000 people. Each person's information includes: name, gender, age, phone number, and address. add Contact Information 2. delete the specified contact information. 3. search for the specified contact information. 4. modify the specified contact information. show all contacts 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 (" input quantity error! \ N "); goto flag;} if (head-> pNext = NULL) {while (I --) {pBook pNew = (pBook) malloc (sizeof (Book )); assert (pNew); printf ("Enter the 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-> addr, addr); scanf ("% d", & pNew-> year); scanf ("% d", & pNew-> phone_num ); pBase-> pNext = pNew; pNew-> pNext = NULL; pBase = pNew;} Else {while (pBase-> pNext! = NULL) {pBase = pBase-> pNext;} while (I --) {pBook pNew = (pBook) malloc (sizeof (Book); assert (pNew ); printf ("Enter the 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-> addr, addr); scanf ("% d", & pNew-> year); scanf ("% d", & pNew-> phone_num ); pBase-> pNext = pNew; pNew-> pNext = NULL ;}} return head;} pBook re Move_linkman (pBook head) {pBook link_man = head; char name [10]; if (! Head) {printf ("no contact, cannot be deleted! \ N "); return NULL;} printf (" which contact do you want to delete? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {if (link_man-> pNext = NULL) {free (link_man-> pNext); link_man-> pNext = NULL; printf ("deletion completed! \ N ") ;}else {pBook ptmp = NULL; ptmp = link_man-> pNext; link_man-> pNext = link_man-> pNext; free (ptmp ); printf ("deleted! \ N ") ;}return head ;}else {link_man = link_man-> pNext ;}} printf (" this contact is not found! \ N "); return head;} void traver_linkman (pBook head) {pBook link_man = head; if (head-> pNext = NULL) {printf (" the phone book is empty! \ N ");} while (link_man-> pNext! = NULL) {printf ("% s \ n", link_man-> pNext-> name); printf ("% s \ n", link_man-> pNext-> sex ); printf ("% s \ n", link_man-> pNext-> addr); printf ("% d \ n", link_man-> pNext-> year ); printf ("% ld \ n", link_man-> pNext-> phone_num ); printf ("********************************** \ 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 Empty! No need to clear it! (Defaults 0 rows) \ n ");} while (link_man-> pNext! = NULL) {if (link_man-> pNext = NULL) {free (link_man-> pNext); link_man-> pNext = NULL ;} else {ptmp = link_man-> pNext; link_man-> pNext = link_man-> pNext; free (ptmp) ;}} printf ("cleared! \ N ");} pBook amend_linkman (pBook head) {char name [10]; char sex [5]; char addr [20]; pBook link_man = head; if (! Head) {printf ("no contact, cannot be modified! \ N "); return NULL;} printf (" which contact Do You Want To modify? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {printf ("Enter the 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-> addr, addr); scanf ("% d ", & link_man-> pNext-> year); scanf ("% d", & link_man-> pNext-> phone_num); return head;} else link_man = link_man-> pNext ;} return h Ead;} void search_linkman (pBook head) {char name [10]; pBook link_man = head; if (! Head) {printf ("no contact, search failed! \ N "); return NULL;} printf (" which contact are you looking? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {printf ("% s \ n ", link_man-> pNext-> name); printf ("% s \ n", link_man-> pNext-> sex); printf ("% s \ n ", link_man-> pNext-> addr); printf ("% d \ n", link_man-> pNext-> year); printf ("% ld \ n ", link_man-> pNext-> phone_num ); printf ("********************************** \ n "); return 0;} else link_man = link_man-> pNext;} printf ("not found! \ N ") ;}void start (pBook * phead) {int I =-1; assert (phead); while (1) {printf (" welcome to the phone book system! Select your operation: \ n "); printf ("************************************* * ******* \ n "" * 1. add Contact Information 2. delete the specified contact information * \ n "" * 3. search for the specified contact information. 4. modify the specified contact information * \ n "" * 5. show all contacts 6. clear all contacts * \ n "" * 0. end the system! ---- ---- * \ N ""******************************* * ************* \ n "); scanf ("% d", & I); switch (I) {case 0: printf ("***** Thank you for using this 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 ("Incorrect choice! Please reselect \ n "); break ;}}# include <stdio. h> # include "phone_book.h" int main () {pBook phead = NULL; start (& phead); return 0 ;}

 

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.