一、介绍
这是使用了带表头结点的单链表 制作的通讯录1.0版本
没有使用文件,所以信息无法持久化当程序结束时,信息会丢失
下次无法访问到这些信息
二、功能
1.增加成员及信息 2.显示通讯录所以成员信息
3.查找成员信息 4.删除成员
5.修改成员信息 6.释放通讯录并退出程序
三、代码
1.主函数:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"addess_list.h"
int main(){
Link head = NULL;
Link new_node = NULL;
int num;
welcome();
create_link(&head);
while(1){
printf("please choice num:\n");
scanf("%d",&num);
switch(num){
case 1:
create_node(&new_node);
printf("please input name: hometel: worktel: address:\n");
scanf("%s %s %s %s",new_node->name,new_node->hometel,new_node->worktel,new_node->address);
insert_node_head(head,new_node);
//printf("please choice num:\n");
break;
case 2:
display(head);
break;
case 3:
find_node(head);
break;
case 4:
delete_node(head);
break;
case 5:
alter_node(head);
break;
case 6:
release(&head);
exit(0);
}
}
}
2.功能函数:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"addess_list.h"
enum rel{MALLOC_OK = 100,MALLOC_NO,CREATE_OK,CREATE_NO,INSERT_NO,INSERT_OK,DELETE_NO,DELETE_OK};
void welcome(){
printf("*************************************************************************\n");
printf("**************** *****************\n");
printf("**************** 编程从通讯录开始 *****************\n");
printf("**************** *****************\n");
printf("*************************************************************************\n");
printf("\n");
printf("**************** what can I do for you ,sir *****************\n");
printf("\n");
printf("****** 1 Add Information! 2 Display List! ******\n");
printf("****** 3 Information Find! 4 Delete Information ******\n");
printf("****** 5 Alter Information! 6 Exit! ******\n");
printf("*************************************************************************\n");
}
int is_malloc(Link new_node){
if(new_node == NULL){
printf("malloc is fail\n");
return MALLOC_NO;
}
else{
return MALLOC_OK;
}
}
int create_node(Link * new_node){
*new_node = (Link)malloc(sizeof(Node));
if(is_malloc(*new_node) == MALLOC_NO){
printf("create number fail\n");
return CREATE_NO;
}
else{
return CREATE_OK;
}
}
void create_link(Link * head){
if(create_node(head) == CREATE_NO){
printf("create address is fail\n");
}
else{
(*head)->next = NULL;
}
}
int insert_node_head(Link head,Link new_node){
if(head == NULL){
printf("not have address list");
return INSERT_NO;
}
else{
new_node->next = head->next;
head->next = new_node;
printf("add information success\n");
return INSERT_OK;
}
}
void display(Link head){
Link p = NULL;
if(head == NULL){
printf("not have this address list\n");
}
else if(head->next == NULL){
printf("this address is empty\n");
}
else{
p = head->next;
while(p != NULL){
printf("name:%s hometel:%s worktel:%s address:%s\n",p->name,p->hometel,p->worktel,p->address);
p = p->next;
}
}
}
void delete_node(Link head){
Link p = NULL;
Link q = NULL;
q = head;
p = head->next;
char name[MAX];
printf("please input you want to delete name:\n");
scanf("%s",name);
while(p != NULL && strcmp(p->name,name) != 0){
q = p;
p = p->next;
}
if(p == NULL){
printf("not find you want to delete name\n");
}
else{
q->next = p->next;
free(p);
printf("delete success\n");
}
}
void find_node(Link head){
char name[MAX];
Link p = NULL;
p = head->next;
printf("please input you want to find member name:\n");
scanf("%s",name);
while(p != NULL && strcmp(name,p->name) != 0){
p = p->next;
}
if(p == NULL){
printf("not find you want to find member");
}
else{
printf("you find member is: %s %s %s %s\n",p->name,p->hometel,p->worktel,p->address);
}
}
void alter_node(Link head){
Link p = NULL;
char name[MAX];
int num;
printf("please input you want to alter member name:\n");
scanf("%s",name);
p = head->next;
while(p != NULL && strcmp(name,p->name) != 0){
p = p->next;
}
if(p == NULL){
printf("not find you want to alter member\n");
}
else{
printf("name:%s hometel:%s worktel:%s address:%s\n",p->name,p->hometel,p->worktel,p->address);
printf("1----name\n");
printf("2----hometel\n");
printf("3----worktel\n");
printf("4----address\n");
printf("input you want to alter num\n");
scanf("%d",&num);
if(num == 1){
scanf("%s",p->name);
printf("alrer success\n");
}
if(num == 2){
scanf("%s",p->hometel);
printf("alter success\n");
}
if(num == 3){
scanf("%s",p->worktel);
printf("alter success\n");
}
if(num == 4){
scanf("%s",p->address);
printf("alter success\n");
}
}
}
void empty_link(Link head){
Link p = NULL;
if(head == NULL){
printf("address list not exist\n");
}
else{
p = head->next;
while(p != NULL){
head->next = p->next;
free(p);
p = head->next;
}
printf("empty address_list success\n");
}
}
void release(Link * head){
empty_link(*head);
free(*head);
*head = NULL;
printf("release address_list success\n");
}
3.函数声明:
#ifndef address_list
#define address_list
#define MAX 20
struct node{
char name[MAX];
char hometel[MAX];
char worktel[MAX];
char address[MAX];
struct node *next;
};
typedef struct node Node;
typedef struct node * Link;
void welcome();
void create_link(Link * head);
int create_node(Link * new_node);
int is_malloc(Link new_node);
int insert_node_head(Link head,Link new_node);
void display(Link head);
void delete_node(Link head);
void find_node(Link head);
void alter_node(Link head);
void empty_link(Link head);
void release(Link * head);
#endif