通讯录

1.用链表的形式模仿手机的通讯录,实现实现通讯录的一些基本功能, 比如添加、删除、查找、排序、修改等操作。

#define  _CRT_SECURE_NO_WARNINGS  1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

//***************联系人信息**********
struct student 
{
	char *name;
	int age;
	char sex[4];
	char phone[12];
	char *address;
	struct student *next;
};
//************选择界面**********
void Menu()
{
	printf("*********************************************\n");
	printf("****1. 添加指定联系人   2. 删除指定联系人****\n");
	printf("****3. 修改指定联系人   4. 显示所有联系人****\n");
	printf("****5. 查找指定联系人   6. 清空所有联系人****\n");
	printf("****7. 按名字排序       0. 退出          ****\n");
	printf("*********************************************\n");
}
//*********添加联系人**********
void Insert(struct student **p)//添加联系人
{
	int i = 0;
	int n = 0;
	struct student *q = (*p);
	printf("请输入要添加联系人的个数:");
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		char *t = NULL;
		char *s = NULL;
		char a[1024] = "0";
		q = (struct student *)malloc(sizeof(struct student));
		printf("\n请输入联系人的名字:");
		scanf("%s", a);
		t = (char*)malloc(strlen(a)+1);
		strcpy(t, a);
		q->name = t;
		printf("\n请输入联系人的年龄:");
		scanf("%d", &q->age);
		printf("\n请输入联系人的性别:");
		scanf("%s", q->sex);
		printf("\n请输入联系人的电话:");
		scanf("%s", q->phone);
		printf("\n请输入联系人的地址:");
		scanf("%s", a);
		s = (char*)malloc(strlen(a)+1);
		strcpy(s, a);
		q->address = s;
		q->next = *p;
		*p = q;
	}
}



//*************显示所有联系人***********
void show(struct student *p)//显示所有联系人
{
	do
	{
		if(p == NULL)
		{
			printf("您的通信录为空\n");
			break;
		}
		printf("姓名为%s  年龄为%d  性别为%s  电话为%s  住址为%s\n", p->name,p->age,p->sex,p->phone,p->address);
		p = p->next;
	}while(p != NULL);
}

//***********删除指定联系人****************
void Drop(struct student **p)//删除指定联系人
{

	char a[20] = "0";
	struct student *q = *p;
	struct student *head = *p;
	assert(*p);
	printf("请输入你要删除的姓名:");
	scanf("%s", a);
	while(1)
	{
		if(!strcmp(q->name,a))
		{
			if(head == q)//删除第一个联系人
			{
				*p = (*p)->next;
			}
			else
			{
				head->next = q->next;
			}
			break;
		}
		head = q;
		q = q->next; 
	}
	free(q);
}

//**************修改指定联系人的信息********
void Amend(struct student **p)//修改指定联系人的信息
{

	char a[20] = "0";
	struct student *q = (*p) ;
	assert(*p);
	printf("请输入要修改的联系人的姓名:");
	scanf("%s", a);
	while(1)
	{
		if((!strcmp(q->name,a))&& q != NULL)
		{
			char *t = NULL;
			char *s = NULL;
			char b[1024] = "0";
			free(q->name);//释放掉原来的联系人的姓名
			printf("\n请输入新联系人的名字:");
			scanf("%s", b);
			t = (char*)malloc(strlen(b)+1);
			strcpy(t, b);
			q->name = t;
			printf("\n请输入新联系人的年龄:");
			scanf("%d", &q->age);
			printf("\n请输入新联系人的性别:");
			scanf("%s", q->sex);
			printf("\n请输入新联系人的电话:");
			scanf("%s", q->phone);
			printf("\n请输入新联系人的地址:");
			scanf("%s", b);
			s = (char*)malloc(strlen(b)+1);
			strcpy(s, b);
			q->address = s;
			break;
		}
		q = q->next; 
	}
	if(q == NULL)
	{
		printf("通信录中没有这个人\n");
	}
}



//***********查找指定联系人**********
void Find(struct student *p)//查找指定联系人
{

	char a[20] = "0";
	struct student *q = p;
	assert(p);
	printf("请输入你要查找的姓名:");
	scanf("%s", a);
	while(1)
	{
		if(!strcmp(q->name,a)&& q != NULL)
		{
			printf("姓名为%s  年龄为%d  性别为%s  电话号为%s  住址为%s\n", q->name,q->age,q->sex,q->phone,q->address);
			break;
		}
		q = q->next; 
	}
	if(q == NULL)
	{
		printf("查无此人\n");
	}
}

//**************交换两个结构体里的内容***********
void Exchange(struct student **p,struct student **head)//交换两个结构体里的内容
{
	struct student *a = (struct student *)malloc(sizeof(struct student));
	a->name = (char*)malloc(100);
	a->address = (char*)malloc(100);
	strcpy(a->name , (*p)->name);
	strcpy((*p)->name , (*head)->name);
	strcpy((*head)->name , a->name);

	a->age = (*p)->age;
	(*p)->age = (*head)->age;
	(*head)->age = a->age;

    strcpy(a->sex , (*p)->sex);
	strcpy((*p)->sex , (*head)->sex);
	strcpy((*head)->sex , a->sex);

	strcpy(a->phone , (*p)->phone);
	strcpy((*p)->phone , (*head)->phone);
	strcpy((*head)->phone , a->phone);

	strcpy(a->address , (*p)->address);
	strcpy((*p)->address , (*head)->address);
	strcpy((*head)->address , a->address);
	free(a->name);
	free(a->address);
	free(a);
}

void Sort(struct student **p)//按名字对所有联系人排序
{

	struct student *q = (*p);
	struct student *head = q;
	assert(*p);
	while(q!=NULL)
	{
		head = q;
		while(head != NULL)
		{
			if(strcmp(q->name ,head->name)>0)
			{
				Exchange(&q,&head);
			}
			head = head->next;
		}
		q = q->next;
	}
}

void Allfree(struct student **p)//释放
{
	struct student *q = (*p);
	while(q != NULL)
	{
		q = (*p)->next;
		free(*p);
		*p = NULL;
		q = *p;
	}
}

int main()
{
	int i = 0;
	struct student *p = NULL;//首结点
	do
	{
		Menu();
		printf("请选择:");
		scanf("%d", &i);
		switch(i)
		{
			case 1:
				Insert(&p);
				break;
			case 2:
				Drop(&p);
				break;
			case 3:
				Amend(&p);
				break;
			case 4:
				show(p);
				break;
			case 5:
				Find(p);
				break;
			case 6:
				Allfree(&p);
				break;
			case 7:
				Sort(&p);
				break;
			case 0:
				Allfree(&p);
				printf("退出通信录\n");
				break;
			default:
				printf("请重新输入\n");
				break;
		}
	}while(i);
	free(p);
	p = NULL;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值