数据结构——顺序表和通讯录的实现

目录

前言

1. 线性表

2.顺序表

2.1 概念与结构

2.1.1 静态顺序表

2.1.2 动态顺序表

2.2 接口实现

2.2.1 初始化、检查空间、打印、销毁

2.2.2 尾插,头插

2.2.3 尾删,头删

2.2.4 查找,插入,删除(指定位置)

2.3 顺序表的测试

2.3.1测试尾插,尾删

2.3.2 检测头插,头删

2.3.3 测试查找,插入,删除

3. 通讯录实现

3.1 界面设计

3.2 数据结构的设计

3.3 接口实现

3.3.1 初始化和销毁

3.3.2 添加,打印功能

3.3.3 查找,删除,修改功能

4. 通讯录源代码

4.1 SeqList.h

4.2 SeqList.c

4.3 Contact.h

4.4Contact.c

4.5Contest.c

4.6 test .c

总结


前言

今天开启数据结构篇章,后续与顺序表相关的是链表,会持续更新。顺序表是入门的数据结构,从顺序表开始,了解如何存储并管理数据,可以尝试敲敲代码!


1. 线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使
用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
线性表在物理上存储时,通常以数组和链式结构的形式存储。

数据结构——顺序表和通讯录的实现_第1张图片

2.顺序表

2.1 概念与结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存
。在数组上完成数据的增删查改。

顺序表一般可以分为:静态顺序表和动态顺序表。

2.1.1 静态顺序表

静态顺序表:使用定长数组存储元素。

//静态顺序表
#define N 10
typedef int SLDataType;

struct SeqList
{
	SLDataType arr[N];//定长数组
	int size;       //有效数据个数
};

静态的缺点很明显,因为数组的长度已经确定,如果数据较少会造成浪费,如果数据较多却无法扩容。所以静态顺序表在实际情况中很少使用。

2.1.2 动态顺序表

动态顺序表:使用动态开辟的数组存储。

//动态顺序表
typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr; //指向动态开辟的数组
	int size;        //有效数据个数
	int capacity;    //空间容量
}SL;

相比于静态顺序表,动态顺序表就可以控制容量大小。

2.2 接口实现

顺序表在实现的时候会有三个文件,以下是放在SeqList.h下的,内容主要为数据结构的设计核函数的声明。其中将所要存储的数据重命名成SLDataType,因为之后如果要存储数据类型不是整型,是字符或者一个结构体时,只需要在重命名这里进行修改就可以了。

#include 
#include 
#include 

//动态顺序表
typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr; //存储数据的底层结构
	int size;        //有效数据个数
	int capacity;    //空间容量
}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 x);
//删除指定位置数据
void SLErase(SL* ps, int pos);
//查找数据的位置
void SLFind(SL* ps, SLDataType x);

2.2.1 初始化、检查空间、打印、销毁

顺序表的初始化,只需要将结构体中的指针置为空指针,容量和大小赋值为零即可。

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

 检查空间,如果空间不足需要扩容,但是一开始容量为零,需要借助三目操作符,第一次把容量扩充为一个值。之后,扩充容量可以控制在一倍倒两倍之间是最好的。

void SLCheckCapacity(SL* ps)
{
	//空间不够
	if (ps->size == ps->capacity)
	{
		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;
	}
}

顺序表的打印一个循环就搞定

void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

顺序表的销毁,释放掉动态开辟的空间,并且将指针值为空。

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

2.2.2 尾插,头插

顺序表的尾插,只需要在有效数据后插入即可,但是先要判断容量大小是否足够。

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	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++;
}

2.2.3 尾删,头删

顺序表的尾删只需要把记录存储数据大小的size减一就好,不用改动数据。不过得加断言,判断size是否大于零。

void SLPopBack(SL* ps)
{
	assert(ps && ps->size);
	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.2.4 查找,插入,删除(指定位置)

顺序表的查找可以通过遍历比较数据,返回对应的下标。

void SLFind(SL* ps, SLDataType x)
{
	assert(ps);
    //遍历顺序表
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;//假设找不到返回-1
}

顺序表在指定位置的插入,可以配合SLFind函数,可以在插入之前进行断言,限制pos的大小。

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

	//向pos位置挪动数据
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

2.3 顺序表的测试

在test.c文件中,可以封装几个测试函数,之后的代码不会再写main函数。

2.3.1测试尾插,尾删

#include "SeqList.h"

void SLTest1()
{   //检测尾插,尾删
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);
    SLDestroy(&s1);
}


int main1()
{
	SLTest1();
	return 0;
}

2.3.2 检测头插,头删

void SLTest2()
{
	SL sl;
	SLInit(&sl);
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPrint(&sl);

	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPrint(&sl);

}

2.3.3 测试查找,插入,删除

void SLTest1()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	
    int pos = 0;
    pos = SLFind(&s1, 3);
    if (pos == -1)
    {
        printf("未找到该数据");
        return;
    }

    //插入数据
    SLInsert(&s1, pos, 5);
	SLPrint(&sl);
    //删除数据
    SLErase(&s1, pos);
	SLPrint(&sl);
    SLDestroy(&s1);
}

3. 通讯录实现

当我们完成了顺序表的各种接口,我们可以尝试利用动态顺序表写一个通讯录。首先要创建三个文件,Contact.c,Contact.h和Contest.c。并且要将SeqList.h文件中数据类型改成Info。Contact.h文件中包含SeqList.h的头文件,然后Contact.c和Contest.c只用包含SeqList.h的头文件。还需要将SLFind这个函数注释掉,因为这段代码是整型数据的查找功能,不符合结构体数据类型。

typedef Info SLDataType

3.1 界面设计

在Contest.c文件中可以写个do while循环重复操作过程,封装一个菜单提示各种功能的按键。

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("请选择您的操作:");
		scanf("%d", &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:
			break;
		}
	} while (op != 0);
	ContactDestory(&con);

	return 0;
}

 数据结构——顺序表和通讯录的实现_第2张图片

3.2 数据结构的设计

在Contact.h中,先创建一个通讯录结构体,放入各种通讯录基本信息,利用define定义这些信息最大字符数。使用顺序表的前置声明,在重命名为Contact。

#include 

#define NAME_MAX 100
#define GENDER_MAX 10
#define TELE_MAX 12
#define ADDR_MAX 100

//通讯录数据类型
typedef struct PersonInfo
{
	char name[NAME_MAX];
	int age;
	char gender[GENDER_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}Info;

//使用顺序表的前置声明
struct SeqList;
typedef struct SeqList Contact;

//通讯里提供的操作
//初始化通讯录
void ContactInit(Contact* con);
//销毁通讯录
void ContactDestory(Contact* con);
//增加联系人
void ContactAdd(Contact* con);
//删除联系人
void ContactDel(Contact* con);
//修改联系人
void ContactModify(Contact* con);
//查找联系人
void ContactFind(Contact* con);
//打印联系人信息
void ContactShow(Contact* con);

3.3 接口实现

3.3.1 初始化和销毁

可以直接调用顺序表中的函数。

void ContactInit(Contact* con)
{
	SLInit(con);
}

void ContactDestory(Contact* con)
{
	SLDestroy(con);
}

3.3.2 添加,打印功能

添加联系人需要依次获取用户输入的信息。

void ContactAdd(Contact* con)
{
	//创建联系人结构体变量
	Info info;

	//printf("请输入联系人的数据\n");
	printf("请输入联系人姓名:\n");
	scanf("%s", info.name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &(info.age));
	printf("请输入联系人性别:\n");
	scanf("%s", info.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", info.tele);
	printf("请输入联系人地址:\n");
	scanf("%s", info.addr);

	//保存到数据道通讯录里
	SLPushBack(con, info);
	printf("添加联系人操作成功\n");
}

打印联系人只需要遍历顺序表即可。

void ContactShow(Contact* con)
{
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
			con->arr[i].name,
			con->arr[i].age,
			con->arr[i].gender,
			con->arr[i].tele,
			con->arr[i].addr);
	}
}

数据结构——顺序表和通讯录的实现_第3张图片

3.3.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 ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入查找的联系人的姓名:");
	scanf("%s", name);

	int findIndex = FindByName(con, name);
	if (findIndex < 0)
	{
		printf("要查找的联系人不存在\n");
	}
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");

	printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
			con->arr[findIndex].name,
			con->arr[findIndex].age,
			con->arr[findIndex].gender,
			con->arr[findIndex].tele,
			con->arr[findIndex].addr);
	
}

删除联系人和修改联系人也类似。

void ContactDel(Contact* con)
{
	//先查找
	printf("请输入删除的联系人的姓名:");
	char name[NAME_MAX];
	scanf("%s", name);

	int findIndex = FindByName(con, name);
	if (findIndex >= 0)
	{
		SLErase(con, findIndex);
		printf("删除操作成功\n");
	}
	else
	{
		printf("要删除的联系人不存在!\n");
		return;
	}

}

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

	int findIndex = FindByName(con, name);
	if (findIndex < 0)
	{
		printf("要修改的联系人不存在!\n");
	}
	printf("请输入联系人姓名:\n");
	scanf("%s", con->arr[findIndex].name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &(con->arr[findIndex].age));
	printf("请输入联系人性别:\n");
	scanf("%s", con->arr[findIndex].gender);
	printf("请输入联系人电话:\n");
	scanf("%s", con->arr[findIndex].tele);
	printf("请输入联系人地址:\n");
	scanf("%s", con->arr[findIndex].addr);

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

4. 通讯录源代码

4.1 SeqList.h

#pragma once
#include 
#include 
#include 
#include "Contact.h"

//动态顺序表
typedef Info SLDataType;
//typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr; //存储数据的底层结构
	int size;        //有效数据个数
	int capacity;    //空间容量
}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 x);
//删除指定位置数据
void SLErase(SL* ps, int pos);
//查找数据的位置
void SLFind(SL* ps, SLDataType x);

4.2 SeqList.c

#include "SeqList.h"

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

void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void SLCheckCapacity(SL* ps)
{
	//空间不够
	if (ps->size == ps->capacity)
	{
		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;
	}
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	//空间足够
	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 SLPopBack(SL* ps)
{
	assert(ps && 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);
	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位置挪动数据
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//void 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 SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

4.3 Contact.h

#pragma once
#include 

#define NAME_MAX 100
#define GENDER_MAX 10
#define TELE_MAX 12
#define ADDR_MAX 100

//通讯录数据类型
typedef struct PersonInfo
{
	char name[NAME_MAX];
	int age;
	char gender[GENDER_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}Info;

//使用顺序表的前置声明
struct SeqList;
typedef struct SeqList Contact;

//通讯里提供的操作
//初始化
void ContactInit(Contact* con);
//销毁
void ContactDestory(Contact* con);
//增加联系人
void ContactAdd(Contact* con);
//删除联系人
void ContactDel(Contact* con);
//修改联系人
void ContactModify(Contact* con);
//查找联系人
void ContactFind(Contact* con);
//打印联系人信息
void ContactShow(Contact* con);

4.4Contact.c

#include "SeqList.h"

void ContactInit(Contact* con)
{
	SLInit(con);
}

void ContactDestory(Contact* con)
{
	SLDestroy(con);
}

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 ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入查找的联系人的姓名:");
	scanf("%s", name);

	int findIndex = FindByName(con, name);
	if (findIndex < 0)
	{
		printf("要查找的联系人不存在\n");
	}
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");

	printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
			con->arr[findIndex].name,
			con->arr[findIndex].age,
			con->arr[findIndex].gender,
			con->arr[findIndex].tele,
			con->arr[findIndex].addr);
	
}

void ContactAdd(Contact* con)
{
	//创建联系人结构体变量
	Info info;

	//printf("请输入联系人的数据\n");
	printf("请输入联系人姓名:\n");
	scanf("%s", info.name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &(info.age));
	printf("请输入联系人性别:\n");
	scanf("%s", info.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", info.tele);
	printf("请输入联系人地址:\n");
	scanf("%s", info.addr);

	//保存到数据道通讯录里
	SLPushBack(con, info);
	printf("添加联系人操作成功\n");
}

void ContactDel(Contact* con)
{
	//先查找
	printf("请输入删除的联系人的姓名:");
	char name[NAME_MAX];
	scanf("%s", name);

	int findIndex = FindByName(con, name);
	if (findIndex >= 0)
	{
		SLErase(con, findIndex);
		printf("删除操作成功\n");
	}
	else
	{
		printf("要删除的联系人不存在!\n");
		return;
	}

}

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

	int findIndex = FindByName(con, name);
	if (findIndex < 0)
	{
		printf("要修改的联系人不存在!\n");
	}
	printf("请输入联系人姓名:\n");
	scanf("%s", con->arr[findIndex].name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &(con->arr[findIndex].age));
	printf("请输入联系人性别:\n");
	scanf("%s", con->arr[findIndex].gender);
	printf("请输入联系人电话:\n");
	scanf("%s", con->arr[findIndex].tele);
	printf("请输入联系人地址:\n");
	scanf("%s", con->arr[findIndex].addr);

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

void ContactShow(Contact* con)
{
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
			con->arr[i].name,
			con->arr[i].age,
			con->arr[i].gender,
			con->arr[i].tele,
			con->arr[i].addr);
	}
}

4.5Contest.c

#include "SeqList.h"

//通讯录菜单
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("请选择您的操作:");
		scanf("%d", &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:
			break;
		}
	} while (op != 0);
	ContactDestory(&con);

	return 0;
}

4.6 test .c

void SLTest1()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);


}

void SLTest2()
{
	SL sl;
	SLInit(&sl);
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPrint(&sl);

	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPrint(&sl);

}

void SLTest1()
{
	SL sl;
	SLInit(&sl);
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);

	int pos = 0;
	pos = SLFind(&s1, 3);
	if (pos == -1)
	{
		printf("未找到该数据");
		return;
	}

	//插入数据
	SLInsert(&s1, pos, 5);
	SLPrint(&sl);
	//删除数据
	SLErase(&s1, pos);
	SLPrint(&sl);
	SLDestroy(&s1);
}

int main()//使用通讯录时需要注释掉
{
	//SLTest1();
	//SLTest2();
	SLTest3();

	return 0;
}


总结

今天内容还是蛮多的,希望你在看的同时,也能上手敲敲代码,感受数据结构的设计。多多重复,百炼成钢!

创作不易,希望这篇文章能给你带来启发和帮助,如果喜欢这篇文章,请留下你的三连哦,你的支持的我最大的动力!!!

数据结构——顺序表和通讯录的实现_第4张图片

你可能感兴趣的:(数据结构,c语言)