Address book written in C Language

Source: Internet
Author: User

The unbounded Address Book can store the contact name and number. Of course, it is not saved to a file, because it is just a Data Structure trainer. Some operations of the linked list are used to perform fuzzy search by keywords (names and numbers are acceptable) and display by the number of searches, the search string matching algorithm uses the KMP algorithm. It is purely a cool-killing tool. during initialization, recursion is also used, with more than three hundred lines of code.

/** @ Lkxiaolou Address Book **/# include <stdio. h> # include <string. h> # include <malloc. h>/** define node **/typedef struct P {char * Name; // name char * number; // number int times; // number of searches struct p * Next;} person, * lperson; // create Address Book pointer lperson booklist = NULL; // create a fuzzy search linked list pointer lperson bookselected = NULL; /*** function declaration */lperson addperson (lperson book, char * Name, char * number, int times); // insert node void view (lperson L ); // traverse the entire address book lperson select (lperson book, char * Key ); // Search for the qualified node int surtold (char * s, char * B); // The SIMPLE algorithm lperson change (lperson CED) for keyword matching ); // change the Chain List sequence int Init () after searching; // initialize int index_kmp (char * s, char * P, int POs, int next []); // keyword-matched KMP algorithm void get_next (char * P, int next []); // The next function of the KMP algorithm int surtnew (char * s, char * B ); // keyword-matched KMP algorithm void deleteall (); // clear the address book/** add record **/lperson addperson (lperson book, char * Name, char * number, int times) {// construct the inserted node lperson s; S = (lperson) malloc (S Izeof (person); s-> name = (char *) malloc (sizeof (name); s-> Number = (char *) malloc (sizeof (number )); s-> times = (INT) malloc (sizeof (INT); s-> times = times; strcpy (S-> name, name); strcpy (S-> number, number); s-> next = NULL; If (Book = NULL) {book = s; s-> next = NULL; return book;} else {lperson P = book; while (p-> next! = NULL) {P = p-> next;} p-> next = s; return book;}/** clear address book **/void deleteall () {free (booklist); booklist = NULL;}/** show all records **/void view (lperson L) {lperson P = L; printf ("Name \ t number \ t search times \ n"); While (P! = NULL) {printf ("% s \ t % d \ n", p-> name, p-> Number, p-> times ); P = p-> next; }}/ ** fuzzy search **/lperson select (lperson book, char * Key) {lperson P = book; while (P! = NULL) {If (surtnew (p-> name, key) | surtnew (p-> Number, key) {P-> times ++; bookselected = addperson (bookselected, p-> name, p-> Number, p-> times); booklist = change (P);} p = p-> next ;} return bookselected;}/** next function of the KMP algorithm **/void get_next (char * P, int next []) {int I, j, slen; slen = strlen (p); I = 0; next [0] =-1; j =-1; while (I <slen) {If (j =-1 | P [I] = P [J]) {++ I; ++ J; next [I] = J ;} else J = next [J] ;}}/** KMP algorithm for searching strings **/INT index_kmp (char * s, Char * P, int POs, int next []) {int I, j, slen, Plen; I = pos-1; j =-1; slen = strlen (s ); plen = strlen (p); While (I <slen & J <Plen) {If (j =-1 | s [I] = P [J]) {++ I; ++ J;} else J = next [J];} If (j> = Plen) return 1; else return 0 ;} /** simple string match **/INT surtold (char * s, char * B) {int I, j, lens, lenb, flag, itemp; lens = strlen (s); lenb = strlen (B); If (lens <lenb) return 0; else {for (I = 0; I <= Lens-lenb; I ++) {flag = 0; For (j = 0; j <lenb; j ++) {If (j = 0) Itemp = I; If (s [itemp] = B [J]) {flag ++; itemp ++;} else break; If (flag = lenb) return 1 ;}} return 0 ;}/ ** KMP algorithm String Match **/INT surtnew (char * s, char * B) {int next [1000]; get_next (B, next); Return index_kmp (S, B, 0, next );} /** re-arrange the linked list by the number of searches. The input parameter CED is the node to be changed. **/lperson change (lperson CED) {lperson head = Booklist; lperson newlist = NULL; // If (CED-> times> = head-> times) {newlist = addperson (newlist, CED-> name, CED-> Number, CED-> Times); While (Head! = NULL) {If (Head! = CED) newlist = addperson (newlist, head-> name, head-> Number, head-> times); Head = head-> next ;}} // The Times of CED is not the maximum else {While (Head-> times> CED-> times) {If (Head! = CED) newlist = addperson (newlist, head-> name, head-> Number, head-> times); Head = head-> next;} newlist = addperson (newlist, CED-> name, CED-> Number, CED-> times); While (Head! = NULL) {If (Head! = CED) newlist = addperson (newlist, head-> name, head-> Number, head-> times); Head = head-> next ;}} return newlist ;} // initialization interface int Init () {int F; char newname [80]; char newnum [80]; char key [80]; printf ("------------- Address Book --------------- \ n"); printf ("select \ n"); printf ("1. insert a record \ n "); printf (" 2. view All records \ n "); printf (" 3. find \ n "); printf (" 4. clear Address Book \ n "); printf (" 5. exit \ n "); scanf (" % d ", & F); Switch (f) {Case 1: printf (" enter the name and phone number (separated by spaces) \ n "); scanf (" % s % S ", newname, newnum); booklist = addperson (Booklist, newname, newnum, 0); getchar (); Init (); break; Case 2: View (booklist ); getchar (); Init (); break; Case 3: printf ("Enter the keyword \ n"); scanf ("% s", key); bookselected = select (Booklist, key); If (bookselected = NULL) {printf ("corresponding record not found! \ N ") ;}else {view (bookselected);} Free (bookselected); bookselected = NULL; getchar (); Init (); break; Case 4: deleteall (); getchar (); Init (); break; Case 5: break; default: printf ("enter \ n correctly"); getchar (); Init (); break ;} return 1;} // entry main function int main () {// test data booklist = addperson (Booklist, "LK", "18567106987", 0 ); booklist = addperson (Booklist, "YZ", "17659990000", 0); booklist = addperson (Booklist, "DK", "18967106894", 0); booklist = addperson (Booklist, "XX", "19879999922", 0); Init (); return 1 ;}

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.