#include
#include
#include
#define MALLOC_OK 1
#define MALLOC_NO 0
#define CREATE_OK 1
#define CREATE_NO 0
struct node
{
int num;
char name[20];
char sex[5];
char phone[20];
struct node *next;
};
typedef struct node Node;
typedef Node * Hlink;
int is_malloc_ok(Hlink new_node)
{
if(NULL == new_node)
{
printf("malloc error!\n");
return MALLOC_NO;
}
return MALLOC_OK;
}
int create_node(Hlink *new_node) //通讯录节点建立
{
*new_node = (Hlink)malloc(sizeof(Node));
if(MALLOC_OK == is_malloc_ok(*new_node))
{
return CREATE_OK;
}
return CREATE_NO;
}
void create_link(Hlink *head) //通讯录链表建立
{
create_node(head);
(*head) -> next = NULL;
}
void display_link(Hlink head) //输出全部通讯录
{
Hlink p = NULL;
p = head -> next;
if(head -> next == NULL)
{
printf("通讯录为空!\n");
}
while(p != NULL)
{
printf("编号:%d\n姓名:%s\n性别:%s\n电话:%s\n",
p -> num,p -> name,p -> sex,p -> phone);
p = p->next;
}
}
void insert_node_tail(Hlink head,Hlink new_node)
{
Hlink p = NULL;
p = head;
while(p -> next != NULL)
{
p = p -> next;
}
p -> next = new_node;
new_node -> next = NULL;
}
void insert_node_location(Hlink head,Hlink new_node,int loc) //选择位置插入
{
int count = 1;
Hlink p = NULL;
Hlink q = NULL;
q = head;
p = head -> next;
while( p != NULL && count != loc) //没找到这个数就放在末尾,找到了就放在这个数的前面
{
q = p;
p = p->next;
count++;
}
q->next = new_node;
new_node->next = p;
}
void clear_link(Hlink head) //通讯录全部删除
{
Hlink p = NULL;
p = head -> next;
if(p == NULL)
{
printf("通讯录是空的!\n");
return;
}
else
{
while(p != NULL)
{
head -> next = p -> next;
free(p);
p = head -> next;
}
printf("通讯录已经全部删除!\n");
}
}
int delete_node(Hlink head,char ch[]) //删除联系人
{
Hlink p = NULL,q = NULL;
q = head;
p = head -> next;
while(p != NULL && strcmp(p -> name,ch) != 0) //p是要删除的节点
{
q = p;
p = p -> next;
}
if(p == NULL)
{
printf("no find!\n");
}
else
{
printf("编号:%d\n姓名:%s\n性别:%s\n电话:%s\n",
p -> num,p -> name,p -> sex,p -> phone);
q -> next = p -> next;
free(p);
printf("联系人已删除!\n");
}
}
int seek_link(Hlink head,char ch[]) //查找联系人
{
Hlink p = NULL;
p = head -> next;
while(p != NULL && strcmp(p -> name,ch) != 0)
{
p = p -> next;
}
if(p == NULL)
{
printf("没有这个联系人!\n");
return 0;
}
else
{
printf("编号:%d\n姓名:%s\n性别:%s\n电话:%s\n",
p -> num,p -> name,p -> sex,p -> phone);
return 1;
}
}
int length_link(Hlink head) //输出联系人总数
{
int len = 0;
Hlink p = NULL;
p = head -> next;
while(p != NULL)
{
len++;
p = p -> next;
}
return len;
}
void menu()
{
printf("*********************************\n");
printf("**********通讯录管理界面*********\n");
printf("=================================\n");
printf("**********1.创建通讯录***********\n");
printf("**********2.插入联系人***********\n");
printf("**********3.查询联系人***********\n");
printf("**********4.删除联系人***********\n");
printf("**********5.显示所有联系人*******\n");
printf("**********6.联系人数量***********\n");
printf("**********7.删除所有联系人*******\n");
printf("**********0.退出管理界面*********\n");
printf("=================================\n");
printf("************选择0-7**************\n");
}
int main()
{
Hlink head = NULL;
Hlink new_node = NULL;
Hlink p = NULL;
int i,m;
int loc;
int choose;
char ch[20];
int num;
char name[20];
char sex[10];
char phone[20];
create_link(&head);
menu();
while(1)
{
printf("请输入你的选项:");
scanf("%d",&choose);
switch(choose)
{
case 1:
{
printf("输入通讯录人数:");
scanf("%d",&m);
for(i = 0;i < m;i++)
{
create_node(&new_node);
insert_node_tail(head,new_node);
p = new_node;
printf("输入第%d位编号:",i + 1);
scanf("%d",&num);
p -> num = num;
getchar();
printf("输入姓名:");
gets(name);
strcpy(p->name,name);
printf("输入性别:");
gets(sex);
strcpy(p->sex,sex);
printf("输入电话:");
// scanf("%s",p -> phone);
gets(phone);
strcpy(p->phone,phone);
printf("\n");
}
break;
}
case 2:
{
create_node(&new_node);
p = new_node;
printf("输入要插入的位置:");
scanf("%d",&loc);
printf("输入插入人员的编号:");
scanf("%d",&p -> num);
printf("输入姓名:");
scanf("%s",p -> name);
printf("输入性别:");
scanf("%s",p -> sex);
printf("输入电话:");
scanf("%s",p -> phone);
insert_node_location(head,new_node,loc);
printf("\n");
break;
}
case 3:
{
printf("输入查询的姓名:");
scanf("%s",ch);
printf("查询到的信息为:\n");
seek_link(head,ch);
printf("\n");
break;
}
case 4:
{
printf("输入删除联系人姓名:");
scanf("%s",ch);
printf("删除的信息为:\n");
delete_node(head,ch);
printf("\n");
break;
}
case 5:
{
printf("显示所有联系人:\n");
display_link(head);
printf("\n");
break;
}
case 6:
{
printf("联系人的总数是:");
int len=length_link(head);
printf("%d",len);
printf("\n");
break;
}
case 7:
{
clear_link(head);
printf("\n");
break;
}
case 0:
{
printf("系统已退出!\n");
exit(0);
break;
}
default:
{
printf("输入有误,请重新输入!\n");
break;
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)