顺序表的实现及基于顺序表实现通讯录

目录

1.数据结构相关概念

1.什么是数据结构

2.为什么需要数据结构?

2.顺序表

2.1 顺序表与数组的区别和联系

2.2 顺序表的分类

2.3 顺序表的实现

2.3.1初始化

2.3.2 顺序表销毁

2.3.3 尾部插入删除 / 头部插入删除

2.3.4 指定位置之前插入数据

2.3.5 指定位置删除数据

2.3.6 在顺序表中查找

3.基于顺序表实现通讯录

3.1.1 通讯录的初始化,销毁

3.1.2 添加联系人

3.1.3 删除联系人

3.1.4 展示通讯录

3.1.5 通讯录的修改

3.1.6 通讯录的查找


1.数据结构相关概念

1.什么是数据结构

数据结构是由“数据”和“结构”两词组合⽽来。

什么是数据?常⻅的数值1、2、3、4.....、教务系统⾥保存的用户信息(姓名、性别、年龄、学历等。⽹⻚⾥⾁眼可以看到的信息(⽂字、图⽚、视频等等),这些都是数据

2.什么是结构

当我们想要使⽤⼤量使⽤同⼀类型的数据时,通过⼿动定义⼤量的独⽴的变量对于程序来说,可读性⾮常差,我们可以借助数组这样的数据结构将⼤量的数据组织在⼀起,结构也可以理解为组织数据的⽅式。

概念:数据结构是计算机存储,组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么方式构成,以及数据元素之间呈现的结构。

总结:

  1. 能够存储数据(如:顺序表,链表等结构)
  2. 存储的数据能够方便查找

2.为什么需要数据结构?

如图中所⽰,不借助排队的⽅式来管理客⼾,会导致客⼾就餐感受差、等餐时间⻓、餐厅营业混乱等情况。同理,程序中如果不对数据进⾏管理,可能会导致数据丢失、操作数据困难、野指针等情况。通过数据结构,能够有效将数据组织和管理在⼀起。按照我们的⽅式任意对数据进⾏增删改查等操
作。
最基础的数据结构:数组。
【思考】有了数组,为什么还要学习其他的数据结构?
假定数组有10个空间,已经使⽤了5个,向数组中插⼊数据步骤:
求数组的⻓度,求数组的有效数据个数,向下标为数据有效个数的位置插⼊数据(注意:这⾥是否要判断数组是否满了,满了还能继续插⼊吗).....
假设数据量⾮常庞⼤,频繁的获取数组有效数据个数会影响程序执⾏效率。
结论:最基础的数据结构能够提供的操作已经不能完全满⾜复杂算法实现。

2.顺序表

2.1 顺序表与数组的区别和联系

  • 顺序表的底层结构就是数组。
  • 顺序表虽然底层是数组,但提供了很多现成的方法,开箱即用,就变成了一个新的很厉害的数据结构。

顺序表是线性表的一种。

顺序表在物理结构上连续,在逻辑结构上也连续。

线性表:

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

2.2 顺序表的分类

1.静态顺序表

struct SeqList
{
    int arr[100];
    int size//顺序表当前有效数据的个数
}

2.动态顺序表

struct SeqList
{
    int *arr;
    int size;//有效数据的个数
    int capacity;//空间的大小
}

2.3 顺序表的实现

创建两个.c文件和一个.h文件

SeqList.h 存放头文件,结构体和函数的声明。

SeqList.c 实现顺序表。

test.c 测试顺序表。

2.3.1初始化

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr;
	int size;
	int capacity;
}SL;

先创建动态顺序表,这个顺序表放在SeqList.h头文件中

//初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

函数的声明放在SeqList.h中,函数实现放在SeqLish.c中,在test.c中测试

2.3.2 顺序表销毁

void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

在程序最后要把动态申请的内存释放掉。

2.3.3 尾部插入删除 / 头部插入删除

初始化的时候size和capacity都为0,要插入数据空间不够,所以要先申请空间

//判断capacity的空间够不够
void SLCheckCapacity(SL* ps)
{
	assert(ps);//等价于assert(ps!=NULL);

	if (ps->capacity == ps->size)
	{
		int newCapacity = 0;
		newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//三目操作符开辟2倍的有效空间
		//开辟空间
		SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity* sizeof(SLDataType));
		//开辟失败
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = temp;
		ps->capacity = newCapacity;

	}

}

尾插:

void SLPushBack(SL* ps, SLDataType x)
{
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->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];
	}
	ps->arr[0] = x;
	ps->size++;
}

打印代码:

void SLPrintf(SL s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d ", s.arr[i]);
	}
	printf("\n");
}

测试结果

尾删:

void SLPopBack(SL* ps)//尾删
{
	assert(ps);
	assert(ps->size);//顺序表不能为空
	--ps->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->size--;
}

头删测试:

2.3.4 指定位置之前插入数据

//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{

	assert(ps);
	assert(pos >= 0 && pos <= ps->size);//pos==ps->size时相当于尾插
	//插入空间,判断空间是否够不够
	SLCheckCapacity(ps);

	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

测试运行代码:

2.3.5 指定位置删除数据

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);//此时pos不能等于size

	for (int i = pos; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

测试运行代码:

2.3.6 在顺序表中查找

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
//定义顺序表的结构
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//静态顺序表
//struct SeqList
//{
//	int arr[100];
//	int size;
//};
//动态顺序表

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效空间数
	int capacity;//空间大小
}SL;

void SLInit(SL* ps);//初始化

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

//头部插⼊删除 / 尾部插⼊删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);


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

SeqList.c

#include"SeqList.h"

//初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//判断capacity的空间够不够
void SLCheckCapacity(SL* ps)
{

	if (ps->capacity == ps->size)
	{
		int newCapacity = 0;
		newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		//开辟空间
		SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity* sizeof(SLDataType));
		//开辟失败
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = temp;
		ps->capacity = newCapacity;

	}

}

void SLPushBack(SL* ps, SLDataType x)//尾插
{
	assert(ps);
	SLCheckCapacity(ps);
	//ps->arr[ps->size] = x;
	//ps->size++;
	ps->arr[ps->size++] = x;
}

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];
	}
	ps->arr[0] = x;
	ps->size++;
}

void SLPrintf(SL s)//打印顺序表
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d ", s.arr[i]);
	}
	printf("\n");
}

void SLPopBack(SL* ps)//尾删
{
	assert(ps);
	assert(ps->size);//顺序表不能为空
	--ps->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->size--;
}


//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{

	assert(ps);
	assert(pos >= 0 && pos <= ps->size);//pos==ps->size时相当于尾插
	//插入空间,判断空间是否够不够
	SLCheckCapacity(ps);

	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);//此时pos不能等于size

	for (int i = pos; i < ps->size-1; 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;
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void test()
{
	SL sl;
	SLInit(&sl);//初始化

	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPrintf(sl);

	int find = SLFind(&sl, 3);
	if (find > 0)
	{
		printf("这个数的下标为%d", find);
	}
	else
	{
		printf("找不到");
	}

	/*SLErase(&sl, 3);
	SLPrintf(sl);*/



	//在指定位置之前插入数据
	//SLInsert(&sl, 4, 99);
	//SLPrintf(sl);
	//SLPopFront(&sl);
	//SLPrintf(sl);
	//SLPopFront(&sl);
	//SLPrintf(sl);
	//SLPopFront(&sl);
	//SLPrintf(sl);
	//SLPopFront(&sl);
	//SLPrintf(sl);

	/*SLPushFront(&sl, 6);
	SLPushFront(&sl, 7);
	SLPrintf(sl);*/

	SLDestroy(&sl);//销毁顺序表

}

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

3.基于顺序表实现通讯录

通讯录中的信息应该有姓名,性别,年龄,电话,地址。

通讯录时将顺序表中的SLDataType改为struct类型

再创建两个文件:Contact.h   Contact.c

创建结构体

#define NAME_MAX 20
#define GENDER_MAX 10//male female
#define TEL_MAX 20
#define ADDR_MAX 20


typedef struct PersonInfo
{
	char name[NAME_MAX];//姓名
	char gender[GENDER_MAX];//性别
	int age;//年龄
	char tel[TEL_MAX];//电话
	char addr[ADDR_MAX];//地址
}PeoInfo;

将SeqList.h中的SLDataType改为PeoInfo类型,再将SeqList.c中的打印和查找注释掉,将test.c中的测试也注释掉。

在通讯录有关函数的声明时类型为SL* ,但在Contact.h中没有声明SL类型所以要前置声明(不能再Contact.h中包含SeqList.h,如果包含会矛盾)。

Contact.h

#pragma once

#define NAME_MAX 20
#define GENDER_MAX 10//male female
#define TEL_MAX 20
#define ADDR_MAX 20

typedef struct PersonInfo
{
	char name[NAME_MAX];//姓名
	char gender[GENDER_MAX];//性别
	int age;//年龄
	char tel[TEL_MAX];//电话
	char addr[ADDR_MAX];//地址
}PeoInfo;

typedef struct SeqList 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);//展示通讯录

3.1.1 通讯录的初始化,销毁

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

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

3.1.2 添加联系人

void ContactAdd(Contact* con)
{
	PeoInfo Info;
	printf("请输入要添加联系人的姓名:");
	scanf("%s", Info.name);

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

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

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

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

	SLPushBack(con,Info);
}

3.1.3 删除联系人

int FindByName(Contact* con, char* name)//根据姓名查找
{
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->arr[i].name, name) == 0)
		{
			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");

}

3.1.4 展示通讯录

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

{
	printf("%s %s %s %s %s", "姓名","性别","年龄","电话","地址\n");
	for (int i = 0; i < con->size; i++)
	{
		printf("%s %s %d %s %s\n", con->arr[i].name,
			con->arr[i].gender,
			con->arr[i].age,
			con->arr[i].tel,
			con->arr[i].addr);
	}

}

3.1.5 通讯录的修改

void ContactModify(Contact* con)//通讯录的修改
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人姓名\n");
	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("%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);

    printf("修改成功\n");
}

3.1.6 通讯录的查找

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

	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("找不到联系人姓名\n");
		return;
	}
	printf("%s %s %s %s %s", "姓名", "性别", "年龄", "电话", "地址\n");
	printf("%s %s %d %s %s\n", con->arr[find].name,
		con->arr[find].gender,
		con->arr[find].age,
		con->arr[find].tel,
		con->arr[find].addr);
}

以上就是通讯录的实现。

最后主函数如下:

void menu()
{
	printf("****************通讯录****************\n");
	printf("******1.增加联系人   2.删除联系人*****\n");
	printf("******3.修改联系人   4.查找联系人*****\n");
	printf("******5.展示联系人   0.退出***********\n");
	printf("**************************************\n");

}

int main()
{
	int input = -1;
	Contact con;
	ContactInit(&con);
	do 
	{
		menu();
		printf("请选择你的操作:");
		scanf("%d", &input);

		switch (input)
		{
		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("输入错误,请重新输入");
			break;
		}

	} while (input);

	ContactDesTroy(&con);
	return 0;
}

  • 14
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值