C语言通讯录系统——C语言单向链表实现

Posted dew0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言通讯录系统——C语言单向链表实现相关的知识,希望对你有一定的参考价值。

实现的通讯录功能有:查看通讯录、添加联系人、删除联系人、查询联系人、保存并退出。

通过txt文件保存和读取通讯录数据。

 

#include <stdio.h>
#include <string.h>
#include <Windows.h>

void showMenu();
void initL();
void addPerson();
void displayPersons();
void deletePerson();
void saveList();
void searchPerson();

struct person_node {
	char name[10];
	char tel_num[20];
	char company_or_school[20];
	char sex[5];
	person_node* next;
};

person_node* L = (person_node *)malloc(sizeof(person_node));

int main()
{
	initL();

	int k;
	while (1)
	{
		system("cls");
		showMenu();
		scanf("%d", &k);
		if (k == 1)
		{
			system("cls");
			displayPersons();
		}
		else if (k == 2)
		{
			system("cls");
			addPerson();
		}
		else if (k == 3)
		{
			system("cls");
			deletePerson();
		}
		else if (k == 4)
		{
			system("cls");
			searchPerson();
		}
		else if (k == 0)
		{
			saveList();
			exit(0);
		}
	}

	system("pause");
	return 0;
}

void showMenu()
{
	printf("------------------------------------------------------\\n");
	printf("\\t\\t\\t通讯录系统\\n\\n");
	printf("\\t\\t  1.查看通讯录\\n");
	printf("\\t\\t  2.添加联系人\\n");
	printf("\\t\\t  3.删除联系人\\n");
	printf("\\t\\t  4.查询联系人\\n");
	printf("\\t\\t  0.保存并退出\\n");
	printf("------------------------------------------------------\\n");
}

void addPerson()
{
	int k = 1;

	while (k == 1)
	{
		printf("-------------------新建联系人--------------------------------\\n");
		person_node* new_person = (person_node *)malloc(sizeof(person_node));
		printf("请输入联系人姓名:");
		scanf("%s", new_person->name);
		printf("请输入联系人性别:");
		scanf("%s", new_person->sex);
		printf("请输入联系人电话:");
		scanf("%s", new_person->tel_num);
		printf("请输入联系人单位:");
		scanf("%s", new_person->company_or_school);
		printf("------------------------------------------------------\\n");

		person_node* ptr = L;
		while (ptr->next != NULL) //ptr指向链表L末尾结点
			ptr = ptr->next;

		ptr->next = new_person;
		new_person->next = NULL;

		printf("...输入‘0‘返回菜单...输入‘1‘继续添加...\\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

void displayPersons()
{
	person_node* ptr = L->next;
	if (ptr == NULL)
		printf("...暂未添加任何联系人...\\n");
	else
	{
		while (ptr != NULL)
		{
			printf("->姓名:%s", ptr->name);
			printf("\\t性别:%s", ptr->sex);
			printf("\\t电话:%s", ptr->tel_num);
			printf("\\t学校或公司:%s<-", ptr->company_or_school);
			printf("\\n");
			ptr = ptr->next;
		}
	}
	printf("...输入‘0‘返回菜单...");
	int k = 10;
	while (k != 0)
	{
		scanf("%d", &k);
	}
	return;
}

void initL()
{
	L->next = NULL;

	FILE *fp;
	if ((fp = fopen("list_file.txt", "r")) == NULL)
	{
		printf("...打开联系人文件时出错...\\n");
		return;
	}

	person_node* tail_ptr = L;
	char name[10], tel_num[20], company_or_school[20], sex[5];
	while (!feof(fp))
	{
		fscanf(fp, "%s%s%s%s", name, sex, tel_num, company_or_school);
		person_node* new_node = (person_node *)malloc(sizeof(person_node));
		strcpy(new_node->name, name);
		strcpy(new_node->sex, sex);
		strcpy(new_node->tel_num, tel_num);
		strcpy(new_node->company_or_school, company_or_school);
		tail_ptr->next = new_node;
		new_node->next = NULL;
		tail_ptr = new_node;
		getc(fp);
	}
	fclose(fp);
}

void deletePerson()
{
	int k = 1;
	while (k == 1)
	{
		printf("-------------------删除联系人--------------------------------\\n");
		person_node* ptr = L->next;
		person_node* pre = L;
		if (ptr == NULL)
			printf("...通讯录中暂无联系人...\\n");
		else
		{
			printf("请输入删除联系人的姓名:");
			char del_name[10];
			scanf("%s", del_name);
			while (ptr != NULL)
			{
				if (strcmp(ptr->name, del_name) == 0)
				{
					printf("...删除联系人条目...\\n");
					printf("->姓名:%s", ptr->name);
					printf("\\t性别:%s", ptr->sex);
					printf("\\t电话:%s", ptr->tel_num);
					printf("\\t学校或公司:%s<-", ptr->company_or_school);

					printf("\\n确认删除吗?y/n\\n");
					char input = ‘0‘;
					while (input != ‘y‘ && input != ‘n‘)
					{
						scanf("%c", &input);
					}
					if (input == ‘n‘)
					{
						ptr = ptr->next;
						pre = pre->next;
						continue;
					}
					else if (input == ‘y‘)
					{
						pre->next = ptr->next;
						free(ptr);
						ptr = pre->next;
						continue;
					}
				}
				ptr = ptr->next;
				pre = pre->next;
			}
		}
		printf("------------------------------------------------------\\n");
		printf("...输入‘0‘返回菜单...输入‘1‘继续删除...\\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

void saveList()
{
	if (L->next == NULL)
		return;

	FILE *fp;
	if ((fp = fopen("list_file.txt", "w")) == NULL)
		printf("...打开联系人文件时出错...\\n");

	person_node* ptr = L->next;
	while (ptr != NULL)
	{
		fprintf(fp, "\\n%s %s %s %s", ptr->name, ptr->sex, ptr->tel_num, ptr->company_or_school);
		ptr = ptr->next;
	}
	fclose(fp);
}

void searchPerson()
{
	int k = 1;
	while (k == 1)
	{
		printf("-------------------查询联系人--------------------------------\\n");
		char search_name[10];
		printf("请输入查询姓名:");
		scanf("%s", search_name);
		person_node* ptr = L->next;
		if (ptr == NULL)
			printf("...通讯录中暂无联系人...\\n");
		else
		{
			int count = 0;
			while (ptr != NULL)
			{
				if (strcmp(ptr->name, search_name) == 0)
				{
					count++;
					printf("...查询到联系人条目%d...\\n", count);
					printf("->姓名:%s", ptr->name);
					printf("\\t性别:%s", ptr->sex);
					printf("\\t电话:%s", ptr->tel_num);
					printf("\\t学校或公司:%s<-\\n", ptr->company_or_school);
				}
				ptr = ptr->next;
			}
		}
		printf("------------------------------------------------------\\n");
		printf("...输入‘0‘返回菜单...输入‘1‘继续查询...\\n");
		scanf("%d", &k);
		if (k == 0)
			return;
	}
}

 截图:

技术图片

技术图片

 

以上是关于C语言通讯录系统——C语言单向链表实现的主要内容,如果未能解决你的问题,请参考以下文章

C/C++项目开发:通讯录管理系统源码,C语言链表实现

[C语言]C语言实现栈(基于单向链表)

[C语言]C语言实现栈(基于单向链表)

C语言教程双向链表学习总结和C语言代码实现!值得学习~

数据结构之单向链表(C语言实现)

数据结构之单向链表(C语言实现)