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
<Pre name = "code" class = "cpp"> # 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
<Pre name = "code" class = "cpp"> # 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
<Pre name = "code" class = "cpp"> # include <stdio. h>
# Include "record. h"
Void add_person (ADDR * node)
{
ADDR * new_p;
ASK (new_p );
New_p-> next = NULL;
Printf ("enter your name :");
Scanf ("% s", new_p-> data. name );
Printf ("Enter the phone number :");
Scanf ("% s", new_p-> data. tel );
While (node-> next)
Nodenode = 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;
Nodenode = node-> next;
}
Printf ("this name is not found! \ N ");
}
Void dis_person (ADDR * node)
{
If (! Node)
Return;
Nodenode = node-> next;
Dis_person (node );
If (node)
Printf ("name: % s Code: % s \ n", node-> data. name, node-> data. tel );
}
From K-Style technology house