C语言单链表(纯代码)[已经封装成通讯录]

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//顺序表的初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
	if (ps->arr != NULL)//不是空指针
	{
		free(ps->arr);//释放
		ps->size = ps->capacity = 0;//大小,容量归0
	}
}
//顺序表空间检查
void SLCheckCapacity(SL* ps)
{
	//插入之前看看空间够不够
	if (ps->capacity == ps->size)
		//申请空间
		//用realloc
		//三目表达式
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//判断初始是否为空?若为空则给四个空间,不为空给原来的两倍
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//增加多少个字节的空间
		if (tmp == NULL)//申请失败
		{
			perror("realloc fail");
			exit(1);//直接退出程序,不再执行
		}

		//空间申请成功
		ps->arr = tmp;
		ps->capacity = newCapacity;//为什么要用newCapacity?防止申请失败变成野指针
	}
}
	//尾插
void SLPushBack(SL * ps, SLDataType x)
	{
		assert(ps);//等于assert(ps != NULL)

		//ps->arr[ps->size] = x;
		//++ps->size;
		SLCheckCapacity(ps);
		ps->arr[ps->size++] = x;//size的位置在最后一个位置之后,使用完后++
	}

	//头插
void SLPushFront(SL* ps, SLDataType x)
	{
		assert(ps);
		SLCheckCapacity(ps);
		for (int i = ps->size; i > 0; i--)//把除了第一个的数据往后移动一位
		{
			ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]
		}
		ps->arr[0] = x;
		ps->size++;
	}
//打印
//void SLPrint(SL* s)
//	{
//		for (int i = 0; i < s->size; i++)
//		{
//			printf("%d", s->arr[i]);//SLDataType 为int,这里用%d,但是结构体不能直接打印,所以得注释掉
//		}
//		printf("\n");
//	}

//尾部删除
	void SLPopBack(SL* ps)
	{
		assert(ps);//传过来的指针不为空
		assert(ps->size);//顺序表不为空
		--ps->size;//或者ps->arr[ps->size - 1] = -1;但是直接将size--就相当于删除尾部元素

	}

	//头部删除
void SLPopFront(SL* ps)//头删
	{
		assert(ps);
		assert(ps->size);
		//数据整体往前挪动一位
		for (int i = 0; i < ps->size - 1; i++)
		{
			ps->arr[i] = ps->arr[i + 1];//最后一个是 ps->arr[size - 2] = ps->arr[size - 1]
		}
		ps->size--;//删了一位size要减一
	}

//指定位置掺入数据
	void SLInsert(SL* ps, int pos, SLDataType x)//指定位置插入数据
	{
		assert(ps);
		assert(pos >= 0 && pos <= ps->size);
		//插入数据;判断空间够不够
		SLCheckCapacity(ps);
		//pos以及之后的往后挪动一位
		for (int i = ps->size; i > pos; i++)
		{
			ps->arr[i] = ps->arr[i - 1];
		}
		ps->arr[pos] = x;
		ps->size++;
	}
//指定位置删除数据
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
		ps->size--;
	}
}
//查找
//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)//这里用结构体的话会有报错,先注释掉
//		{
//			return i;
//		}
//	}
//	return -1;
//}

SeqList.h

#pragma once
//此次代码编写为抄写,在于理清思路
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
//typedef int SLDataType;//把int重命名为SLDataType方便更换数据类型而不大规模改动//如果做通讯录的话就得换成自定义类型
typedef peoInfo SLDataType;
typedef struct SeqList //定义动态顺序表的(结构体)
{
	SLDataType * arr;
	int size;//有效数据个数
	int capacity;//空间大小
}SL;//重命名,SLDatatype太长了,把他命名为SL
//或者用typedef struct SeqList SL;

//顺序表的初始化
void SLInit(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);
void SLPrint(SL* ps);

//顺序表的头部插入删除/尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);

void SLPopBack(SL* ps);
void SLPopFront(SL* ps);

//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType);
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps, SLDataType x);

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
#include"SeqList.h"


//通讯录的初始化
void ContactInit(Contact* con)
{
	//实际上就是顺序表的初始化,已经实现好了
	SLInit(con);
}

//通讯录的销毁
void ContactDestroy(Contact *con)
{
	SLDestroy(con);
}

//通讯录添加数据
void ContactAdd(Contact* con)
{
	peoInfo info;
	//获取用户输入的内容 姓名+性别+年龄+电话+地址 
	printf("请输入要添加的联系人姓名:\n");
	scanf("%s", info.name);

	printf("请输入要添加的联系人性别:\n");
	scanf("%s", info.gender);

	printf("请输入要添加的联系人年龄:\n");
	scanf("%d", &info.age);

	printf("请输入要添加的联系人电话:\n");
	scanf("%s", info.tel);

	printf("请输入要添加的联系人地址:\n");
	scanf("%s", info.addr);

	//往通讯录里插入数据
	SLPushBack(con, info);


}
//通讯录的查找
int FindByName(Contact* con, char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].name, name))
		{
			return i;//找到了
		}
	}
	return -1;//没有找到

}



//通讯录的删除(必须找到对应元素才能进行删除操作,所以得有一个查找函数)
void ContactDel(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要删除的联系人姓名:\n");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("要删除的联系人数据不存在!\n");
		return;
	}
	//否则存在
	SLErase(con, find);
	printf("删除成功!\n");
}

//展示通讯录数据
void ContactShow(Contact* con)
{
	printf("%s %s %s %s %s \n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%3s %3s %3d %3s %3s\n",
			con->arr[i].name,
			con->arr[i].gender,
			con->arr[i].age,
			con->arr[i].tel,
			con->arr[i].addr );
	}

}


//通讯录的修改
void ContactModify(Contact* con)
{
	char name[NAME_MAX];
	 //要修改的联系人存在
	printf("请输入要修改的联系人姓名:\n"); //ctrl + 鼠标左键
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("要修改的数据不存在");
		return;
	}
	//直接修改
	printf("请输入要新的的姓名:\n");
	scanf("%s", con->arr[find].name);

	printf("请输入要新的的性别:\n");
	scanf("%s", con->arr[find].gender);

	printf("请输入要新的的年龄:\n");
	scanf("%d", &con->arr[find].age);	

	printf("请输入要新的的电话:\n");
	scanf("%s", con->arr[find].tel);

	printf("请输入要新的的地址:\n");
	scanf("%s", con->arr[find].addr);
}

//通讯录查找
void ContactFind(Contact* con)
{
	//找到并且打印信息
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名:\n");
	scanf("%s", name);
	/*for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].name, name))
		{
			printf("找到了,以下为该联系人数据:\n");
			printf("%3s %3s %3d %3s %3s\n",
				con->arr[i].name,
				con->arr[i].gender,
				con->arr[i].age,
				con->arr[i].tel,
				con->arr[i].addr);
			return 0;
		}
	}		
		{
			printf("没有找到,该数据不存在\n");
		}*/  //---------------自己写的
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("要查找的联系人不存在");
		return;
	}
	printf("%s %s %s %s %s \n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%3s %3s %3d %3s %3s\n",
		con->arr[find].name,
		con->arr[find].gender,
		con->arr[find].age,
		con->arr[find].tel,
		con->arr[find].addr);

}

Contact.h

#pragma once
//通讯录头文件
#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100
#define _CRT_SECURE_NO_WARNINGS 1
//定义联系人 数据 结构
//五个数据:姓名 性别 年龄 电话 地址
typedef struct personInfo //通讯录的原名为personInfo,改名字为peoInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}peoInfo;


//前置声明---为什么呢?在SeqList.h里包含了Contact.h,但是Contact.h里不知道下面的SL是啥,所以要声明一下SeqList是啥。
struct SeqList;
 
//通讯录用到的方法是顺序表的方法,对通讯录的操作就是对顺序表的操作,所以要改名包含
typedef struct SeqList Contact;//如果不声明。不知道SeqList是什么,因为两个.h文件不能相互包含(注意,这里不能用改过的名字SL,只能用原来的SeqList)
//通讯录的相关方法


void ContactInit(Contact* con);//通讯录的初始化


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


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


void ContactDel(Contact* con);//通讯录删除数据


void ContactModify(Contact* con);//通讯录的修改


void ContactFind(Contact* con);//通讯录查找


void ContactShow(Contact* con);//展示通讯录的数据

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"
//void SLTest01()
//{
	/*SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);*/

	/*SLPopBack(&sl);
	SLPrint(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);*/
	//SLPopBack(&sl);
	//SLPrint(&sl);//这里已经没了,看一下能不能报错
	/*SLErase(&sl,3);
	SLPrint(&sl);
	void SLDestroy(SL * ps);
}//
int main()
{
	/*SLTest01();*/
	/*return 0;*/
//void ContactTest01()
//{
//	Contact con;
//	ContactInit(&con);
//	ContactAdd(&con);
//	ContactAdd(&con);
//	ContactShow(&con);
//
//	/*ContactDel(&con);*/
//	ContactModify(&con);
//	ContactFind(&con);
//	ContactShow(&con);
//	ContactDestroy(&con);
//}
//int main()
//{
//	ContactTest01();
//	return 0;
//}
void menu()
{
	printf("***********************通讯录*************************\n");
	printf("*********1.增加联系人数据      2.删除联系人数据 ******\n");
	printf("*********3.查找联系人数据      4.修改联系人数据 ******\n");
	printf("*********5.展示联系人数据      0.退出*****************\n");
	printf("******************************************************\n");

}
int main()
{
	int op = -1;
	Contact con;
	ContactInit(&con);
	
	do {
		menu();
		scanf("%d", &op);
		switch (op)
		{
		case 1 :ContactAdd(&con);
			printf("添加成功!\n");
			break;
		case 2:ContactDel(&con);
			break;
		case 3:ContactFind(&con);
			break;
		case 4:ContactModify(&con);
			break;
		case 5:ContactShow(&con);
			printf("展示成功!\n");
			break;
		case 0:
			printf("退出成功!");
			break;		
		default:
			printf("请输入正确的选项!");
			break;
		}
	} while (op != 0);
	ContactDestroy(&con);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值