数据结构4:基于单链表的通讯录项目

头文件

SList.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"

//typedef int SLDataType;
typedef PersonInfo SLDataType;

//创建链表节点结构
typedef struct SListNode {
	SLDataType a;
	struct SListNode* next;
}SListNode;

//销毁
void SLDestory(SListNode** phead);

//打印
void SLPrint(SListNode* phead);

//尾插
void SLPushBack(SListNode** pphead, SLDataType x);

//头插
void SLPushFront(SListNode** pphead, SLDataType x);

//尾删
void SLPopBack(SListNode** pphead);

//头删
void SLPopFront(SListNode** pphead);

//查找
SListNode* SLFind(SListNode* phead, SLDataType x);

//在指定数据之前插入数据
void SLInsertFront(SListNode** pphead, SLDataType pos, SLDataType x);

//指定数据之后插入
void SLInsertBack(SListNode* phead, SLDataType pos, SLDataType x);

//指定位置之前插入数据
void SLInsertFront_1(SListNode** pphead, SListNode* pos, SLDataType x);

//指定位置之前插入数据
void SLInsertBack_1(SListNode* pos, SLDataType x);

//删除pos结点
void SLErase(SListNode** pphead, SListNode* pos);

//删除pos之后的结点
void SLEraseAfter(SListNode* pos);

Contact.h

#pragma once


#define NAME_MAX 50
#define GENDER_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100

//将单链表重命名为通讯录
typedef struct SListNode Contact;

//定义联系人数据结构
typedef struct PersonInfo {
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}PersonInfo;

//销毁通讯录
void ContactDestory(Contact** con);

//添加通讯录数据
void ContactPushBack(Contact** con);

//展示通讯录
void ContactPrint(Contact* con);

//保存数据
void SaveContact(Contact* con);

//读取文件
void LoadContact(Contact** con);

//删除联系人
void ContactDele(Contact** con);

//查找联系人
void ContactFind(Contact* con);

//修改联系人信息
void ContactModify(Contact** con);

实现文件

SList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"SList.h"

//打印
//void SLPrint(SListNode* phead)
//{
//	SListNode* pcur = phead;
//	while (pcur)
//	{
//		printf("%d->", pcur->a);
//		pcur = pcur->next;
//	}
//	printf("NULL\n");
//}

//创建新节点
SListNode* BuyNode(SLDataType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc fail!\n");
		exit(1);
	}
	newnode->a = x;
	newnode->next = NULL;
	return newnode;
}

//链表的销毁
void SLDestory(SListNode** pphead)
{
	assert(pphead);
	SListNode* pcur = *pphead;
	while (pcur)
	{
		SListNode* tmp = pcur->next;
		free(pcur);
		pcur = tmp;
	}
	*pphead = NULL;
}

//尾插
void SLPushBack(SListNode** pphead, SLDataType x)
{
	assert(pphead);
	SListNode* newnode = BuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else {
		SListNode* ptail = *pphead;
		while (ptail->next != NULL)
		{
			ptail = ptail->next;
		}
		ptail->next = newnode;
	}
}

//头插
void SLPushFront(SListNode** pphead, SLDataType x)
{
	assert(pphead);
	SListNode* newnode = BuyNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
void SLPopBack(SListNode** pphead)
{
	assert(pphead && *pphead);
	if ((*pphead)->next == NULL)
	{
		SLDestory(pphead);
		return;
	}
	SListNode* pprev = *pphead;
	SListNode* ptail = *pphead;
	while (ptail->next != NULL)
	{
		pprev = ptail;
		ptail = ptail->next;
	}
	pprev->next = NULL;
	free(ptail);
	ptail = NULL;
}

//头删
void SLPopFront(SListNode** pphead)
{
	assert(pphead && *pphead);
	SListNode* pcur = *pphead;
	*pphead = pcur->next;
	free(pcur);
	pcur = NULL;
}

//查找
//SListNode* SLFind(SListNode* phead, SLDataType x)
//{
//	assert(phead);
//	SListNode* pcur = phead;
//	while (pcur)
//	{
//		if (pcur->a == x)
//		{
//			return pcur;
//		}
//		pcur = pcur->next;
//	}
//	return NULL;
//}

//指定数据之前插入
//void SLInsertFront(SListNode** pphead, SLDataType pos, SLDataType x)
//{
//	assert(pphead && *pphead);
//	if ((*pphead)->a == pos)
//	{
//		SLPushFront(pphead, x);
//		return;
//	}
//	SListNode* newnode = BuyNode(x);
//	SListNode* pcur = *pphead;
//	while (pcur->next->a != pos)
//	{
//		pcur = pcur->next;
//	}
//	newnode->next = pcur->next;
//	pcur->next = newnode;
//}

//指定数据之后插入
//void SLInsertBack(SListNode* phead, SLDataType pos, SLDataType x)
//{
//	assert(phead);
//	SListNode* newnode = BuyNode(x);
//	SListNode* find = SLFind(phead, pos);
//	newnode->next = find->next;
//	find->next = newnode;
//}

//指定位置之前插入数据
//void SLInsertFront_1(SListNode** pphead, SListNode* pos, SLDataType x)
//{
//	assert(pphead && *pphead);
//	assert(pos);
//	if (*pphead == pos)
//	{
//		SLPushFront(pphead, x);
//		return;
//	}
//	SListNode* newnode = BuyNode(x);
//	SListNode* pcur = *pphead;
//	while (pcur->next != pos)
//	{
//		pcur = pcur->next;
//	}
//	newnode->next = pcur->next;
//	pcur->next = newnode;
//}

//指定位置之前插入数据
//void SLInsertBack_1(SListNode* pos, SLDataType x)
//{
//	assert(pos);
//	SListNode* newnode = BuyNode(x);
//	newnode->next = pos->next;
//	pos->next = newnode;
//}

//删除pos结点
void SLErase(SListNode** pphead, SListNode* pos)
{
	assert(pphead && *pphead);
	assert(pos);
	if (*pphead == pos)
	{
		SLPopFront(pphead);
		return;
	}
	SListNode* prev = *pphead;
	while (prev->next != pos)
	{
		prev = prev->next;
	}
	prev->next = pos->next;
	free(pos);
	pos = NULL;
}

//删除pos之后的结点
void SLEraseAfter(SListNode* pos)
{
	assert(pos && pos->next);
	SListNode* next = pos->next;
	pos->next = next->next;
	free(next);
	next = NULL;
}

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include"Contact.h"
#include"SList.h"

//销毁通讯录
void ContactDestory(Contact** con)
{
	SLDestory(con);
}

//添加通讯录数据
void ContactPushBack(Contact** con)
{
	assert(con);
	Contact* phead = *con;
	PersonInfo tmp;
	printf("请输入联系人姓名:\n");
	scanf("%s", tmp.name);
	printf("请输入联系人性别:\n");
	scanf("%s", tmp.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", tmp.tel);
	printf("请输入联系人地址:\n");
	scanf("%s", tmp.addr);
	SLPushBack(con, tmp);
}

//查找联系人信息
Contact* Findbyname(Contact* con,char name[NAME_MAX])
{
	assert(con);
	Contact* pcur = con;
	while (pcur)
	{
		if (strcmp(pcur->a.name, name) == 0) {
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}

//删除联系人
void ContactDele(Contact** con)
{
	assert(con);
	Contact* phead = *con;
	char name[NAME_MAX];
	printf("请输入要删除的联系人姓名:\n");
	scanf("%s", name);
	Contact* find = Findbyname(*con, name);
	if (find == NULL)
	{
		printf("无此联系人信息!\n");
		return;
	}
	SLErase(con, find);
}

//查找联系人
void ContactFind(Contact* con)
{
	assert(con);
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名:\n");
	scanf("%s", name);
	Contact* find = Findbyname(con, name);
	if (find == NULL)
	{
		printf("无此联系人信息!\n");
		return;
	}
	printf("姓名 性别 电话 地址\n");
	printf("%s %s %s %s\n", find->a.name
		, find->a.gender
		, find->a.tel
		, find->a.addr);
}

//修改联系人信息
void ContactModify(Contact** con)
{
	assert(con);
	char name[NAME_MAX];
	printf("请输入要修改的联系人姓名:\n");
	scanf("%s", name);
	Contact* find = Findbyname(*con, name);
	if (find == NULL)
	{
		printf("无此联系人信息!\n");
		return;
	}
	printf("请输入新的联系人姓名:\n");
	scanf("%s", find->a.name);
	printf("请输入联系人性别:\n");
	scanf("%s", find->a.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", find->a.tel);
	printf("请输入联系人地址:\n");
	scanf("%s", find->a.addr);
	printf("修改成功!\n");
}

//展示通讯录
void ContactPrint(Contact* con)
{
	Contact* pcur = con;
	printf("姓名 性别 电话 地址\n");
	while (pcur) {
		printf("%s %s %s %s\n", pcur->a.name
			, pcur->a.gender
			, pcur->a.tel
			, pcur->a.addr);
		pcur = pcur->next;
	}
}

//保存文件
void SaveContact(Contact* con)
{
	FILE* pf = fopen("Contact.txt", "wb");
	if (pf == NULL) {
		perror("fopen fail!\n");
		return;
	}
	Contact* pcur = con;
	while (pcur) {
		fwrite(&(pcur->a), sizeof(PersonInfo), 1, pf);
		pcur = pcur->next;
	}
	fclose(pf);
	pf = NULL;
	printf("保存成功!\n");
	return;
}

//读取文件
void LoadContact(Contact** con)
{
	FILE* pf = fopen("Contact.txt", "rb");
	if (pf == NULL) {
		perror("fopen fail!\n");
		return;
	}
	PersonInfo a;
	PersonInfo* tmp = &a;
	while (fread(tmp,sizeof(PersonInfo),1,pf) )
	{
		SLPushBack(con, *tmp);
	}
	printf("读取成功!\n");
}

测试代码

#define _CRT_SECURE_NO_WARNINGS 1

#include"SList.h"
#include"Contact.h"

//测试通讯录
void Test2(Contact* con)
{
	//读取数据
	LoadContact(&con);

	//执行操作
	int input = 0;
	do {
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			ContactPushBack(&con);
			break;
		case 2:
			ContactDele(&con);
			break;
		case 3:
			ContactModify(&con);
			break;
		case 4:
			ContactFind(con);
			break;
		case 5:
			ContactPrint(con);
		default:
			break;
		}
	} while (input);
	printf("正在退出...\n");

	//保存数据
	SaveContact(con);
}

int main()
{
	Contact* con = NULL;

	Test2(con);

	//销毁
	ContactDestory(&con);
	return 0;
}

相关推荐

  1. 数据结构4基于通讯录项目

    2024-04-13 12:48:01       38 阅读
  2. 数据结构_基于通讯录

    2024-04-13 12:48:01       46 阅读
  3. 基于实现通讯录项目

    2024-04-13 12:48:01       63 阅读
  4. 数据结构实现通讯录

    2024-04-13 12:48:01       32 阅读
  5. 通讯录基于

    2024-04-13 12:48:01       26 阅读
  6. 数据结构】利用再实现通讯录

    2024-04-13 12:48:01       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 12:48:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 12:48:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 12:48:01       87 阅读
  4. Python语言-面向对象

    2024-04-13 12:48:01       96 阅读

热门阅读

  1. <Halcon> 变换矩阵求解

    2024-04-13 12:48:01       41 阅读
  2. 算法 第37天 贪心6

    2024-04-13 12:48:01       133 阅读
  3. 数据库系统大纲

    2024-04-13 12:48:01       78 阅读
  4. 【P2P】

    【P2P】

    2024-04-13 12:48:01      42 阅读
  5. 算法-日期问题

    2024-04-13 12:48:01       41 阅读
  6. Node.js 开发技巧

    2024-04-13 12:48:01       40 阅读
  7. 蓝桥杯嵌入式之电位器R37和R38存入EEPROM

    2024-04-13 12:48:01       36 阅读
  8. Qt/QML编程之路:图片进度条的实现(50)

    2024-04-13 12:48:01       33 阅读
  9. 程序员如何提高收入

    2024-04-13 12:48:01       38 阅读
  10. 全国大学生电子设计大赛-TI杯2020年赛题分享

    2024-04-13 12:48:01       38 阅读