C语言:手撕顺序表及基于顺序表写一个通讯录

目录

一.顺序表

1.顺序表的概念及结构

1.1线性表

2.顺序表分类

 2.1顺序表和数组的区别:

2.2顺序表分类

3.动态顺序表的实现

3.1顺序表初始化

3.2顺序表尾插

3.3顺序表销毁

3.4顺序表头插

3.5顺序表尾删

3.6顺序表头删

3.6顺序表数据查找

3.7在指定位置之前插入数据

3.8删除指定位置之前的数据

3.9完整的源码

二.通讯录的实现

Contact.h:

Contact.c:

Sequencelist.h:

Sequencelist.c:

test.c:

运行界面:


一.顺序表

1.顺序表的概念及结构

1.1线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中⼴泛使 ⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串... 线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。 案例:蔬菜分为绿叶类、⽠类、菌菇类。线性表指的是具有部分相同特性的⼀类数据结构的集合 如何理解逻辑结构和物理结构?

2.顺序表分类

 2.1顺序表和数组的区别:

顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接口。

2.2顺序表分类

静态顺序表

静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费。

动态顺序表

按需申请

3.动态顺序表的实现

首先我们创建三个文件:

头文件SequenceList.h用来定义一个顺序表结构体、声明顺序表的接口,源文件SequenceList.c负责实现顺序表的接口,test.c文件用于测试

由于数据类型给的是不确定的,所以我们typedef一下,方便后期修改

typedef int  SLDataType;
typedef struct SequenceList
{
	SLDataType* _arr;
	int _size;
	int _capacity;
}SL;

结构体名字我们也typedef一下,叫SL,这样之后能够动态申请空间的结构体的我们就定义好了

3.1顺序表初始化

头文件声明一下:

void SLInit(SL*sl);

sequence.c实现:

#include"SequenceList.h"
void SLInit(SL* sl)
{
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}

那是否成功初始化呢?我们在主函数调用一下SLInit接口调试的看一下是否初始化:

#include"SequenceList.h"
void SLtest01()
{
	SL sl;
	SLInit(&sl);


}
int main()
{
	SLtest01();
	return 0;
}

按下F11

通过监视窗口我们看到已成功初始化

3.2顺序表尾插

头文件声明一下:

void SLPushBack(SL*sl,SLDataType x);

sequence.c实现:

void CheckSpace(SL* sl)
{
	if (sl->_capacity == sl->_size)
	{
		int newcapacity = sl->_capacity == 0 ? 4 : 2 * sl->_capacity;
		SLDataType* tmp = (SLDataType*)realloc(sl->_arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		sl->_arr=tmp;
		sl->_capacity = newcapacity;
	}

}
void SLPushBack(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	sl->_arr[sl->_size++] = x;
}

因为我们是动态顺序表,每次在插入数据前应该先判断空间够不够,后面也要不断的检查空间够不够所以我们这里把检查空间容量单独封装成一个函数方便后续使用

那么为了方便我们看到数据插入的情况我们再封装一个打印函数,头文件先声明一下:

void SLPrint(SL sl);

sequence.c实现:

因为不需要修改内容,所以我们传值即可

void SLPrint(SL sl)
{
	for (int i = 0; i < sl._size; i++)
	{
		printf("%d ", sl._arr[i]);
	}
	printf("\n");
}

那么test.c文件我们写几个用例看看尾插的情况:

void SLtest02()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(sl);
}
int main()
{
	SLtest02();
	return 0;
}

CTRL+F5运行一下:

尾插实现成功!

3.3顺序表销毁

销毁和初始化类似,多一个释放空间,因为我们是动态开辟空间,销毁时应该将空间还给操作系统

头文件声明一下:

void SLDestroy(SL*sl);

sequence.c实现:

void SLDestroy(SL* sl)
{
	free(sl->_arr);
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}

我们在刚刚尾插之后,进行销毁顺序表看看调试窗口的情况:

按下F10:

销毁成功!

3.4顺序表头插

头文件声明:

void SLPopFront(SL*sl);

sequence.c实现:

void SLPushFront(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	for(int i=sl->_size;i>0;i--)
	{ 
		sl->_arr[i]=sl->_arr[i - 1];
	}
	sl->_arr[0] = x;
	sl->_size++;
}

在头插前,先判断空间够不够,够的情况下,我们先进行数据挪动,让i=sl->_size,刚开始把倒数第二个数据挪动到sl->_size位置处,不断减减i,最后将下标为0的位置即头部插入数据,并将_size++;

3.5顺序表尾删

头文件声明:

void SLPopBack(SL* sl);

sequence.c实现:

void SLPopBack(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	--sl->_size;
}

删除数据时,应该先断言,我们的顺序表必须有数据才能进行删除

这里不需要赋值什么的,直接--sl->_size即可,相当于是控制了访问权限。

3.6顺序表头删

头文件声明:

void SLPopFront(SL*sl);

sequence.c实现:

void SLPopFront(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	for (int i = 0;i<sl->_size-1; i++)
	{
		sl->_arr[i] = sl->_arr[i + 1];
	}
	sl->_size--;
}

头删的话直接挪动数据,直到最后一个数据给了倒数第二个数据时停止,即size-2=size-1停止

3.6顺序表数据查找

头文件声明:

int SLFind(SL* ps, SLDataType x);

sequence.c实现:

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;
}

传一个数据x过来,如果顺序表有这个数据,那么将它的下标返回,如果循环结束还是没有找到,那么就返回-1。

3.7在指定位置之前插入数据

头文件声明:

void SLInsert(SL * ps, int pos, SLDataType x);

sequence.c实现:

void SLInsert(SL* sl, int pos, SLDataType x)
{
	assert(sl);
	assert(pos >= 0&&pos<=sl->_size);
	CheckSpace(sl);
	for (int i = sl->_size; i>pos; i--)
	{
		sl->_arr[i] = sl->_arr[i - 1];
	}
	sl->_arr[pos] = x;
	sl->_size++;
}

3.8删除指定位置之前的数据

头文件声明:

void SLErase(SL*sl,int pos);

sequence.c实现:

void SLErase(SL* sl, int pos)
{
	assert(sl);
	assert(pos >= 0 && pos < sl->_size);
	for (int i = pos; i<sl->_size; i++)
	{
		sl->_arr[i] = sl->_arr[i+1];
	}
	sl->_size--;
}

3.9完整的源码

sequencelist.h:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int  SLDataType;
typedef struct SequenceList
{
	SLDataType* _arr;
	int _size;
	int _capacity;
}SL;
//初始化
void SLInit(SL*sl);
//销毁
void SLDestroy(SL*sl);
//尾插
void SLPushBack(SL*sl,SLDataType x);
//头插
void SLPushFront(SL* sl, SLDataType x);
//尾删
void SLPopBack(SL*sl);
//头删
void SLPopFront(SL*sl);
//打印顺序表
void SLPrint(SL sl);
//删除指定位置之前的数据
void SLErase(SL*sl,int pos);
//在指定位置前插入数据
void SLInsert(SL * ps, int pos, SLDataType x);
//查找数据
int SLFind(SL* ps, SLDataType x);

sequencelist.c:

#include"SequenceList.h"
void SLInit(SL* sl)
{
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}
void SLDestroy(SL* sl)
{
	free(sl->_arr);
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}
void CheckSpace(SL* sl)
{
	if (sl->_capacity == sl->_size)
	{
		int newcapacity = sl->_capacity == 0 ? 4 : 2 * sl->_capacity;
		SLDataType* tmp = (SLDataType*)realloc(sl->_arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		sl->_arr=tmp;
		sl->_capacity = newcapacity;
	}

}
void SLPushBack(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	sl->_arr[sl->_size++] = x;
}
void SLPopBack(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	--sl->_size;
}
void SLPushFront(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	for(int i=sl->_size;i>0;i--)
	{ 
		sl->_arr[i]=sl->_arr[i - 1];
	}
	sl->_arr[0] = x;
	sl->_size++;
}
void SLPopFront(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	for (int i = 0;i<sl->_size-1; i++)
	{
		sl->_arr[i] = sl->_arr[i + 1];
	}
	sl->_size--;
}
void SLInsert(SL* sl, int pos, SLDataType x)
{
	assert(sl);
	assert(pos >= 0&&pos<=sl->_size);
	CheckSpace(sl);
	for (int i = sl->_size; i>pos; i--)
	{
		sl->_arr[i] = sl->_arr[i - 1];
	}
	sl->_arr[pos] = x;
	sl->_size++;
}
void SLPrint(SL sl)
{
	for (int i = 0; i < sl._size; i++)
	{
		printf("%d ", sl._arr[i]);
	}
	printf("\n");
}
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;
}
void SLErase(SL* sl, int pos)
{
	assert(sl);
	assert(pos >= 0 && pos < sl->_size);
	for (int i = pos; i<sl->_size; i++)
	{
		sl->_arr[i] = sl->_arr[i+1];
	}
	sl->_size--;
}

test.c:

#include"SequenceList.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);
	SLPushFront(&sl, 6);
	SLPushFront(&sl, 7);
	SLPushFront(&sl, 8);
	SLPushFront(&sl, 9);
	SLPrint(sl);
	int find=SLFind(&sl, 3);
	//SLInsert(&sl, find, 15);
	//SLPrint(sl);
	SLErase(&sl,find);
	SLPrint(sl);
	SLDestroy(&sl);

}
int main()
{
	SLtest01();
	return 0;
}

二.通讯录的实现

Contact.h:

#define NAME_MAX 20
#define GENDER_MAX 10
#define ADDRESS_MAX 50
#define TEL_MAX   30
typedef struct PersonInfo
{
	char name[NAME_MAX];
	int age;
	char gender[GENDER_MAX];
	char telphone[TEL_MAX];
	char address[ADDRESS_MAX];
}PI;
typedef struct SequenceList Contact;
//通讯录的初始化
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);

Contact.c:

#include"SequenceList.h"
//通讯录的初始化
void ContactInit(Contact* con)
{
	SLInit(con);
}
//通讯录的销毁
void ContactDesTroy(Contact* con)
{
	SLDestroy(con);
}
//通讯录添加数据
void ContactAdd(Contact* con)
{
	PI pi;
	printf("请输入联系人姓名:\n");
	scanf("%s", pi.name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &pi.age);
	printf("请输入联系人性别:\n");
	scanf("%s", pi.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", pi.telphone);
	printf("请输入联系人家庭住址:\n");
	scanf("%s", pi.address);
	SLPushBack(con,pi);
	printf("联系人信息添加成功!\n");
}
//通讯录删除数据
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");
}
int FindByName(Contact* con,char name[NAME_MAX])
{
	for (int i = 0; i < con->_size; i++)
	{
		if (0 == strcmp(con->_arr[i].name, name))
		{
			return i;
		}
	}
	return -1;
}
//通讯录的修改
void ContactModify(Contact* con)
{
	printf("请输入要修改的联系人姓名:\n");
	char name[NAME_MAX];
	scanf("%s", name);
	int find = FindByName(con,name);
	if (find<0)
	{
		printf("查找的联系人数据不存在!\n");
		return;
	}
	printf("请输入要修改的新的联系人姓名:\n");
	scanf("%s", con->_arr[find].name);
	printf("请输入要修改的新的联系人年龄:\n");
	scanf("%d", &con->_arr[find].age);
	printf("请输入要修改的新的联系人性别:\n");
	scanf("%s", con->_arr[find].gender);
	printf("请输入要修改的新的联系人电话:\n");
	scanf("%s", con->_arr[find].telphone);
	printf("请输入要修改的新的联系人家庭住址:\n");
	scanf("%s", con->_arr[find].address);
	printf("修改成功!\n");

}
//通讯录查找
void ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名\n");
	scanf("%s", name);

	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("要查找的联系人数据不存在!\n");
		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].telphone,
		con->_arr[find].address
	);
}
//展示通讯录数据
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].telphone,
			con->_arr[i].address
		);
	}
}

Sequencelist.h:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include"Contact.h"

typedef PI  SLDataType;
typedef struct SequenceList
{
	SLDataType* _arr;
	int _size;
	int _capacity;
}SL;
void SLInit(SL*sl);
void SLDestroy(SL*sl);
void SLPushBack(SL*sl,SLDataType x);
void SLPushFront(SL* sl, SLDataType x);
void SLPopBack(SL*sl);
void SLPopFront(SL*sl);
//void SLPrint(SL sl);
//void SLErase(SL*sl,int pos);
// 指定位置之前插⼊ / 删除数据
//void SLInsert(SL * ps, int pos, SLDataType x);
//int SLFind(SL* ps, SLDataType x);

Sequencelist.c:

#include"SequenceList.h"
void SLInit(SL* sl)
{
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}
void SLDestroy(SL* sl)
{
	free(sl->_arr);
	sl->_arr = NULL;
	sl->_capacity = sl->_size = 0;
}
void CheckSpace(SL* sl)
{
	if (sl->_capacity == sl->_size)
	{
		int newcapacity = sl->_capacity == 0 ? 4 : 2 * sl->_capacity;
		SLDataType* tmp = (SLDataType*)realloc(sl->_arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		sl->_arr=tmp;
		sl->_capacity = newcapacity;
	}

}
void SLPushBack(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	sl->_arr[sl->_size++] = x;
}
void SLPopBack(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	--sl->_size;
}
void SLPushFront(SL* sl, SLDataType x)
{
	assert(sl);
	CheckSpace(sl);
	for(int i=sl->_size;i>0;i--)
	{ 
		sl->_arr[i]=sl->_arr[i - 1];
	}
	sl->_arr[0] = x;
	sl->_size++;
}
void SLPopFront(SL* sl)
{
	assert(sl);
	assert(sl->_size > 0);
	for (int i = 0;i<sl->_size-1; i++)
	{
		sl->_arr[i] = sl->_arr[i + 1];
	}
	sl->_size--;
}
//void SLInsert(SL* sl, int pos, SLDataType x)
//{
//	assert(sl);
//	assert(pos >= 0&&pos<=sl->_size);
//	CheckSpace(sl);
//	for (int i = sl->_size; i>pos; i--)
//	{
//		sl->_arr[i] = sl->_arr[i - 1];
//	}
//	sl->_arr[pos] = x;
//	sl->_size++;
//}
//void SLPrint(SL sl)
//{
//	for (int i = 0; i < sl._size; i++)
//	{
//		printf("%d ", sl._arr[i]);
//	}
//	printf("\n");
//}
//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;
//}
void SLErase(SL* sl, int pos)
{
	assert(sl);
	assert(pos >= 0 && pos < sl->_size);
	for (int i = pos; i<sl->_size; i++)
	{
		sl->_arr[i] = sl->_arr[i+1];
	}
	sl->_size--;
}

test.c:

#include"SequenceList.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);
//	SLPushFront(&sl, 6);
//	SLPushFront(&sl, 7);
//	SLPushFront(&sl, 8);
//	SLPushFront(&sl, 9);
//	SLPrint(sl);
//	int find=SLFind(&sl, 3);
//	//SLInsert(&sl, find, 15);
//	//SLPrint(sl);
//	SLErase(&sl,find);
//	SLPrint(sl);
//	SLDestroy(&sl);
//
//}
//void SLtest02()
//{
//	SL sl;
//	SLInit(&sl);
//	SLPushBack(&sl, 1);
//	SLPushBack(&sl, 2);
//	SLPushBack(&sl, 3);
//	SLPushBack(&sl, 4);
//	SLPushBack(&sl, 5);
//	SLPrint(sl);
//
//	SLDestroy(&sl);
//}
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();
		printf("请选择您的操作:\n");
		scanf("%d", &op);

		//要根据对应的op执行不同的操作
		switch (op)
		{
		case 1:
			ContactAdd(&con);
			break;
		case 2:
			ContactDel(&con);
			break;
		case 3:
			ContactModify(&con);
			break;
		case 4:
			ContactFind(&con);
			break;
		case 5:
			ContactShow(&con);
			break;
		case 0:
			printf("退出通讯录....\n");
			break;
		default:
			printf("输入错误,请重新选择您的操作!\n");
			break;
		}
	} while (op != 0);

	ContactDesTroy(&con);
	return 0;
}

运行界面:

给个三连吧,谢谢大家!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值