Cainiao cultivation C language design-Address Book (1)

Source: Internet
Author: User

This design uses the one-way linked list implementation in C language.

Including C language key knowledge

1. Use of typedef

2. Use of custom macros

3. Implementation and operation of one-way linked list

1.1 Design Questions

This course is designed to learn how to create a linked list, store structure information using a linked list, add linked list nodes, and delete linked list nodes. In actual design, data information and retrieval functions can be added.

1.2 design requirements

1.2.1 functional design requirements

This design requires the following functions:

(1) This design focuses on the overall design. Only the Member Code and phone number are selected for the information.

(2) If a record already exists, it can only be appended to it.

(3) display the content of the entire record (including new records appended ).

(4) the code can be composed of 6 Characters and digits, such as A201 and 34011d.

(5) make the phone number consist of 18 characters and numbers, such as (86)-551-34443535, 1396786678.

(6) You can delete all records and add new records at any time.

(7) You can use menus to add, delete, and display functions.

(8) dynamically apply for a bucket using a macro definition.

1.2.2 Overall Design

The design requirements for the module design are as follows:

(1) The linked list design should be implemented using multiple files.

(2) divide them into three modules. One module is responsible for input; one module is responsible for displaying recorded content; one module contains the main program, and the main program is responsible for menu selection and command processing.

2. Design Code

2.1main.c

# Include <stdio. h> # include "record. H "int menu_select (void); void hand_menu (INT cmd, ADDR * list_head); int main (INT argc, char * argv []) {int cmd = 0; ADDR * list_head; ask (list_head); list_head-> next = NULL; while (1) {cmd = menu_select (); If (cmd = '0') return 0; hand_menu (CMD, list_head) ;}} int menu_select (void) {int select; printf ("<------ communication thin --------> \ n"); printf ("1: add contact 2: Delete contact \ n "); printf (" 3: show all contacts 0: Exit \ n "); printf (" Enter: \ n "); select = getch (); While (select <'0' | SELECT> '3') {printf ("input error, please input again: \ n "); select = getch ();} return select;} void hand_menu (INT cmd, ADDR * list_head) {Switch (CMD) {Case '1': add_person (list_head); break; case '2': list_head = del_person (list_head); break; Case '3': dis_person (list_head); break; default: break ;}}

2.2 record. h

#ifndef _RECORD_H_#define _RECORD_H_typedef struct{char name[8];char tel[20];}DATA;typedef struct node{DATA data;struct node *next;}ADDR;#define ASK(p) do{\p = (ADDR *)malloc(sizeof(ADDR));\if(p==NULL){printf("malloc memory failed!");exit(-1);}\}while(0)#endif

2.3 opre. c

# Include <stdio. h> # include "record. h "void add_person (ADDR * node) {ADDR * new_p; ASK (new_p); new_p-> next = NULL; printf (" Enter name :"); scanf ("% s", new_p-> data. name); printf ("Enter the phone number:"); scanf ("% s", new_p-> data. tel); while (node-> next) node = node-> next; node-> next = new_p;} void del_person (ADDR * node) {char name [8]; ADDR * pre = NULL; printf ("Enter the name to delete:"); scanf ("% s", name); while (node) {if (! Strcmp (node-> data. name, name) {pre-> next = node-> next; free (node); printf ("deleted successfully! \ N "); return;} pre = node; node = node-> next;} printf (" this name is not found! \ N ");} void dis_person (ADDR * node) {if (! Node) return; node = node-> next; dis_person (node); if (node) printf ("Name: % s Code: % s \ n", node-> data. name, node-> data. tel );}

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.