【数据结构初阶】顺序表的实现(文末附原码)

⭐博客主页:️CS semi主页
⭐欢迎关注:点赞收藏+留言
⭐系列专栏:数据结构初阶
⭐代码仓库:Data Structure
家人们更新不易,你们的点赞和关注对我而言十分重要,友友们麻烦多多点赞+关注,你们的支持是我创作最大的动力,欢迎友友们私信提问,家人们不要忘记点赞收藏+关注哦!!!


前言

我们在学习数据结构的初期,肯定需要遇到的是时空复杂度和这个顺序表的创建,对于时空复杂度和顺序表具体的讲解会有博客进行讲解,今天我们要讲解的是如何实现一个顺序表。在实现顺序表的时候,我们需要有C语言的基础,我们在实现的时候,是需要先进行顺序表的实现,我们不用着急去写菜单的。接下来,我们将一步一步手把手教。


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

一、思路

在进行制作顺序表的时候,我们需要整理一下思路,思考一下这个顺序表是怎样的?我们可以知道:我们需要实现以下10个步骤,所以接下来跟着我来吧!

在这里插入图片描述


二、制作框架

首先,三板斧是必要的,我们需要来一个头文件和两个源文件,SeqList.h是用来包含头文件和函数的定义的;SeqList.c是用来函数的实现的;test.c是用来测试和最终使用的。
在这里插入图片描述

我们把头文件全部包含在SeqList.h头文件中,这样在其他文件只需要用#include"SeqList.h"即可,因为这样更加清晰,更加明了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这里的制作框架与通讯录的制作不一样,因为我们为了更加方便的调试,我们需要一点点进行调试,就需要我们先不写我们的菜单和具体实现内容,我们先进行测试,所以,具体措施如下:

这个主要是在头文件中进行写代码和定义,我们需要先创建一个结构体进行装这个数组和大小,我们先创建了一个静态顺序表,但是静态顺序表有比较大的缺陷,当数据满了我们就无法插入了,那我们使用动态顺序表;后面是接口函数关于实现上面写的信息。

SeqList.h:

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

#define N 1000
typedef int SLDataType;//改类型方便

静态顺序表:特点:满了就不让插入;缺点:数组给的大小不确定
//typedef struct SeqList 
//{
//	SLDataType a[N];
//	int size;//表示数组中存储了多少个数据
//}SL;

//动态顺序表:特点:满了就不让插入;缺点:数组给的大小不确定
typedef struct SeqList
{
	SLDataType* a;
	int size;//表示数组中存储了多少个数据
	int capacity;//表示数组的实际能存数据的空间容量是多大
}SL;

//接口函数
//初始化
void SeqListInit(SL* ps);
//尾插
void SeqListPushBack(SL* ps, SLDataType x);
//尾删
void SeqListPopBack(SL* ps);
//头插
void SeqListPushFront(SL* ps, SLDataType x);
//头删
void SeqListPopFront(SL* ps);
//打印
void SeqListPrint(SL* ps);
//销毁空间
void SeqListDestory(SL* ps);
//增容函数
void SeqListCheckCapacity(SL* ps);
//查找位置,找到了返回x位置下标,没有找到返回-1
int SeqListFind(SL* ps, SLDataType x);
//在某一个位置上插入数据
void SeqListInsert(SL* ps, int pos, SLDataType x);
//在指定位置删除指定数据
void SeqListErase(SL* ps, int pos);
//排序
void SeqListSortName(void* elem1, void* elem2);
//清空
void SeqListEmpty(SL* ps);

SeqList.c:

#include"SeqList.h"
//函数的实现

test.c:

#include"SeqList.h"

TestSeqList1() {

}

int main() {
	//测试
	TestSeqList1();

	return 0;
}

三、初始化

初始化较为简单,只需要我们将结构体里面的成员全部赋值为0即可。
SeqList.c:
在这里插入图片描述


四、尾插

在这里插入图片描述

因为我们建立的是一个动态数组,我们并没有给这个动态数组进行创造空间,所以我们需要先进行判断是空(容量满了)还是未满的状态,如果没有空间,我们先给空间为4,如果有空间,满了则以两倍的空间方式进行扩容,之后我们再将数据放到数组里,而这里的realloc是当这个空间为NULL的时候,就相当于malloc的作用,我们直接选择增加4个空间,当不为空时候,我们就进行两倍两倍的增容,如下代码:
SeqList.c:

//尾插
void SeqListPushBack(SL* ps, SLDataType x) {
	//容量已经到了顶或者都为0
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//没有空间就扩容失败
		if (tmp == NULL) {
			printf("realloc error\n");
			exit(-1);
		}
		//扩容成功,扩容成功的空间给数组
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->size] = x;
	ps->size++;
}

五、尾删

因为顺序表是连续存放的,而ps->size是这个数组的大小,我们有较为温柔的方法是先判断是否大于0,同样的,我们也可以用断言的方法进行实现。
SeqList.c:

//尾删
void SeqListPopBack(SL* ps) {
	//温柔方式
	if (ps->size > 0) {
		ps->size--;
	}
	//暴力方式
	assert(ps->size > 0);
	ps->size--;

}

六、头插

头插其实就是需要定义一个end的指针,从最末尾的元素往前移动,一直移动到0的位置,每次移动我们都将前面一个数把后面的一个数进行覆盖,最后再在第一个元素读入数据,那同样也是需要进行判断是否增容的,如下的演示图:
在这里插入图片描述
SeqList.c:

//头插
void SeqListPushFront(SL* ps, SLDataType x) {
	//容量已经到了顶或者都为0
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//没有空间就扩容失败
		if (tmp == NULL) {
			printf("realloc error\n");
			exit(-1);
		}
		//扩容成功,扩容成功的空间给数组
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	//挪动数据
	int end = ps->size - 1;
	while (end >= 0) {
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

七、头删

头删顾名思义就是将头部的数据进行删除,那当然我们也需要进行数据的挪动,将数据往后移,我们需要定义一个begin指针,让它从下标为1的位置往后移动,每移动一步将后面一位的数据替代掉前面一位的数据,如下示意图:
在这里插入图片描述

SeqList.c:

//头删
void SeqListPopFront(SL* ps) {
	assert(ps->size > 0);
	//挪动数据
	int begin = 1;
	while (begin < ps->size) {
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

八、查找

至于查找,那可是相当的简单,找得到就返回当前的下标,找不到就返回-1。
SeqList.c:

//查找
int SeqListFind(SL* ps, SLDataType x) {
	for (int i = 0; i < ps->size; i++) {
		if (ps->a[i] == x) {
			return i;
		}
	}
	return -1;
}

九、判断是否增容

这其实就是我们在上面头插尾插中的判断是否需要增容的函数,后面都需要用到我们直接就封装出去,直接调用即可。
SeqList.c:

//增容
void SeqListCheckCapacity(SL* ps) {
	//容量已经到了顶或者都为0
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//没有空间就扩容失败
		if (tmp == NULL) {
			printf("realloc error\n");
			exit(-1);
		}
		//扩容成功,扩容成功的空间给数组
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

十、在某一个位置上插入数据

我们要想实现一下在某一位置上插入数据,那我们就先定义一个end和pos指针,pos指向的是需要插入数据的位置,end从后往前移动找到pos,并且把在pos后面的数据全部往后移一位,并在pos的位置插入数据,如下示意图:
在这里插入图片描述

SeqList.c:

//在某一个位置上插入数据
void SeqListInsert(SL* ps, int pos, SLDataType x) {
	assert(pos <= ps->size && pos >= 0);
	SeqListCheckCapacity(ps);
	//挪动数据
	int end = ps->size - 1;
	while (end >= pos) {
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

这个函数其实可以进行复用,我们直接在头的位置或者尾的位置进行增数据,这样就是头插和尾插了:
在这里插入图片描述
在这里插入图片描述


十一、在指定位置删除指定数据

我们定义一个begin指针,从指定下标的后面一个元素往后一直到头,这段的数据往前覆盖,如下示意图:
在这里插入图片描述
SeqList.c:

//在指定位置删除指定数据
void SeqListErase(SL* ps, int pos) {
	assert(pos < ps->size&& pos >= 0);
	int begin = pos + 1;
	while (begin < ps->size) {
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;

}

既然头插和尾插实现了,那头删和尾删也是同理,我们也可以实现一下:
在这里插入图片描述
在这里插入图片描述

小技巧:插入的时候,指针是从后往前;删除的时候,指针是从前往后。


十二、打印

SeqList.c:

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

十三、测试

我们在有上面这些函数的基础了,我们就可以在test.c文件中进行测试了,这样子我们进行调试更加简单和方便,能够更快速地发现问题的存在,所以,我们有以下测试:
test.c:

//测试1
void TestSeqList1()
{
	SL s1;
	SeqListInit(&s1);
	SeqListPushBack(&s1, 1);
	SeqListPushBack(&s1, 2);
	SeqListPushBack(&s1, 3);
	SeqListPushBack(&s1, 4);
	SeqListPushBack(&s1, 5);

	SeqListPrint(&s1);
	SeqListPopBack(&s1);
	SeqListPopBack(&s1);
	SeqListPopBack(&s1);
	//SeqListPopBack(&s1);
	//SeqListPopBack(&s1);
	//SeqListPopBack(&s1);
	SeqListPrint(&s1);

	SeqListDestory(&s1);
}

//测试2
TestSeqList2() {
	SL s1;
	SeqListInit(&s1);
	SeqListPushBack(&s1, 1);
	SeqListPushBack(&s1, 2);
	SeqListPushBack(&s1, 3);
	SeqListPushBack(&s1, 4);
	SeqListPushBack(&s1, 5);
	SeqListPrint(&s1);

	SeqListPushFront(&s1, 10);
	SeqListPushFront(&s1, 20);
	SeqListPushFront(&s1, 30);
	SeqListPrint(&s1);

	SeqListPopFront(&s1);
	SeqListPopFront(&s1);
	SeqListPopFront(&s1);
	SeqListPopFront(&s1);
	SeqListPrint(&s1);

	SeqListDestory(&s1);

}

//测试3
TestSeqList3() {
	SL s1;
	SeqListInit(&s1);
	SeqListPushBack(&s1, 1);
	SeqListPushBack(&s1, 2);
	SeqListPushBack(&s1, 3);
	SeqListPushBack(&s1, 4);
	SeqListPushBack(&s1, 5);
	SeqListPrint(&s1);

	SeqListDestory(&s1);

}
//测试4
void TestSeqList4() {
	SL s1;
	SeqListInit(&s1);
	SeqListPushBack(&s1, 1);
	SeqListPushBack(&s1, 2);
	SeqListPushBack(&s1, 3);
	SeqListPushBack(&s1, 4);
	SeqListPushBack(&s1, 5);
	SeqListPrint(&s1);

	SeqListInsert(&s1, 2, 30);
	SeqListPrint(&s1);
	int ret = SeqListFind(&s1, 4);
	printf("%d\n", ret);

	SeqListErase(&s1, 0);
	SeqListErase(&s1, 1);
	SeqListPrint(&s1);

	SeqListDestory(&s1);

}
int main() {
	//测试
	TestSeqList1();
	TestSeqList2();
	TestSeqList3();
	TestSeqList4();

	return 0;
}

那当然了,我们建立的动态数组肯定是需要进行销毁空间的,所以我们也写了一个销毁空间的函数:
SeqList.c:

//销毁空间
void SeqListDestory(SL* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

当我们进行完调试以后,我们就可以进行简单的书写菜单和菜单测试的场景了。


十四、实现

我们先进行创建一个菜单,再创建一个枚举,将这些功能对应编号使用switch-case语句填进去,之后我们按照我们的需求将这些函数的功能全部填进去,在我们的排序和清空操作我们在下面进行讲解:
test.c:

void menu() {
	printf("\t\t\t**********************\t\t\t\n");
	printf("\t\t\t****请选择你的操作****\t\t\t\n");
	printf("\t\t\t*****1.头插2.头删*****\t\t\t\n");
	printf("\t\t\t*****3.尾插4.尾删*****\t\t\t\n");
	printf("\t\t\t*****5.查找6.打印*****\t\t\t\n");
	printf("\t\t\t*****7.指插8.指删*****\t\t\t\n");
	printf("\t\t\t*****9.排序10.清空****\t\t\t\n");
	printf("\t\t\t*********0.退出*******\t\t\t\n");
}

enum SeqListTest {
	Exit,
	PushFront,
	PopFront,
	PushBack,
	PopBack,
	Find,
	Print,
	Insert,
	Erase,
	Sort,
	Empty
};

void MenuTest() {
	SL s1;
	SeqListInit(&s1);//初始化
	int input = 0;
	do {
		menu();
		int x = 0;
		int pos = 0;
		scanf("%d", &input);
		switch (input) {
		case Exit:
			printf("程序退出\n");
			break;
		case PushFront:
			printf("请输入你要头插的数据,连续输入,以-1结束\n");
			scanf("%d", &x);
			while (x != -1) {
				SeqListPushFront(&s1, x);
				scanf("%d", &x);
			}
			break;
		case PopFront:
			SeqListPopFront(&s1);
			printf("头删一个成功\n");
			break;
		case PushBack:
			printf("请输入你要尾插的数据,连续输入,以-1结束\n");
			scanf("%d", &x);
			while (x != -1) {
				SeqListPushBack(&s1, x);
				scanf("%d", &x);
			}
			break;
		case PopBack:
			SeqListPopBack(&s1);
			printf("尾删一个成功\n");
			break;
		case Find:
			printf("请输入需要查找的元素\n");
			scanf("%d", &x);
			pos = SeqListFind(&s1, x);
			printf("要查找的元素下标在:%d\n", pos);
			break;
		case Print:
			SeqListPrint(&s1);
			break;
		case Insert:
			printf("请输入插入位置和元素大小\n");
			scanf("%d %d", &x, &pos);
			SeqListInsert(&s1, x, pos);
			break;
		case Erase:
			printf("请输入删除的位置\n");
			scanf("%d", &x);
			SeqListErase(&s1, x);
			printf("删除成功\n");
			break;
		case Sort:
			qsort(s1.a, s1.size, sizeof(s1.a[0]), SeqListSortName);
			printf("排序成功\n");
			break;
		case Empty:
			SeqListEmpty(&s1);
			printf("清空成功\n");
			break;
		default:
			printf("请重新输入\n");
			break;
		}
	} while (input);

	SeqListDestory(&s1);
}

int main() {
	//测试
	//TestSeqList1();
	//TestSeqList2();
	//TestSeqList3();
	//TestSeqList4();

	//正式
	MenuTest();
	return 0;
}

十五、排序和清空

这两个放在一起讲。我们先来看看排序,我们这次用的是qsort函数进行排序:
SeqList.c:
在这里插入图片描述
test.c:
在这里插入图片描述
SeqList.h:
在这里插入图片描述

接下来我们看一下清空,清空是与初始化一样的,是把结构体内部成员全部置空(0)。


十六、原码

SeqList.h:

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

#define N 1000
typedef int SLDataType;//改类型方便

静态顺序表:特点:满了就不让插入;缺点:数组给的大小不确定
//typedef struct SeqList 
//{
//	SLDataType a[N];
//	int size;//表示数组中存储了多少个数据
//}SL;

//动态顺序表:特点:满了就不让插入;缺点:数组给的大小不确定
typedef struct SeqList
{
	SLDataType* a;
	int size;//表示数组中存储了多少个数据
	int capacity;//表示数组的实际能存数据的空间容量是多大
}SL;

//接口函数
//初始化
void SeqListInit(SL* ps);
//尾插
void SeqListPushBack(SL* ps, SLDataType x);
//尾删
void SeqListPopBack(SL* ps);
//头插
void SeqListPushFront(SL* ps, SLDataType x);
//头删
void SeqListPopFront(SL* ps);
//打印
void SeqListPrint(SL* ps);
//销毁空间
void SeqListDestory(SL* ps);
//增容函数
void SeqListCheckCapacity(SL* ps);
//查找位置,找到了返回x位置下标,没有找到返回-1
int SeqListFind(SL* ps, SLDataType x);
//在某一个位置上插入数据
void SeqListInsert(SL* ps, int pos, SLDataType x);
//在指定位置删除指定数据
void SeqListErase(SL* ps, int pos);
//排序
void SeqListSortName(void* elem1, void* elem2);
//清空
void SeqListEmpty(SL* ps);

SeqList.c:

#include"SeqList.h"
//函数的实现

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

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

//销毁空间
void SeqListDestory(SL* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

//增容
void SeqListCheckCapacity(SL* ps) {
	//容量已经到了顶或者都为0
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//没有空间就扩容失败
		if (tmp == NULL) {
			printf("realloc error\n");
			exit(-1);
		}
		//扩容成功,扩容成功的空间给数组
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

//尾插
void SeqListPushBack(SL* ps, SLDataType x) {
	分装函数--增容
	容量已经到了顶或者都为0
	//if (ps->size == ps->capacity) {
	//	int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
	//	SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
	//	//没有空间就扩容失败
	//	if (tmp == NULL) {
	//		printf("realloc error\n");
	//		exit(-1);
	//	}
	//	//扩容成功,扩容成功的空间给数组
	//	ps->a = tmp;
	//	ps->capacity = newcapacity;
	//}
	//SeqListCheckCapacity(ps);
	//ps->a[ps->size] = x;
	//ps->size++;
	SeqListInsert(ps, ps->size, x);
}


//尾删
void SeqListPopBack(SL* ps) {
	//温柔方式
	if (ps->size > 0) {
		ps->size--;
	}
	暴力方式
	//assert(ps->size > 0);
	//ps->size--;
	SeqListErase(ps, ps->size - 1);
}

//头插
void SeqListPushFront(SL* ps, SLDataType x) {
	//SeqListCheckCapacity(ps);
	//容量已经到了顶或者都为0
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//没有空间就扩容失败
		if (tmp == NULL) {
			printf("realloc error\n");
			exit(-1);
		}
		//扩容成功,扩容成功的空间给数组
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	挪动数据
	//int end = ps->size - 1;
	//while (end >= 0) {
	//	ps->a[end + 1] = ps->a[end];
	//	end--;
	//}
	//ps->a[0] = x;
	//ps->size++;
	SeqListInsert(ps, 0, x);
}

//头删
void SeqListPopFront(SL* ps) {
	//assert(ps->size > 0);
	挪动数据
	//int begin = 1;
	//while (begin < ps->size) {
	//	ps->a[begin - 1] = ps->a[begin];
	//	begin++;
	//}
	//ps->size--;
	SeqListErase(ps, 0);
}

//查找
int SeqListFind(SL* ps, SLDataType x) {
	for (int i = 0; i < ps->size; i++) {
		if (ps->a[i] == x) {
			return i;
		}
	}
	return -1;
}

//在某一个位置上插入数据
void SeqListInsert(SL* ps, int pos, SLDataType x) {
	assert(pos <= ps->size && pos >= 0);
	SeqListCheckCapacity(ps);
	//挪动数据
	int end = ps->size - 1;
	while (end >= pos) {
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

//在指定位置删除指定数据
void SeqListErase(SL* ps, int pos) {
	assert(pos < ps->size&& pos >= 0);
	int begin = pos + 1;
	while (begin < ps->size) {
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;

}

//排序
void SeqListSortName(void* elem1, void* elem2) {
	return *(SLDataType*)elem1 - *(SLDataType*)elem2;
}

//清空
void SeqListEmpty(SL* ps) {
	ps->size = ps->capacity = 0;
	ps->a = NULL;
}

test.c:

#include"SeqList.h"

测试1
//void TestSeqList1()
//{
//	SL s1;
//	SeqListInit(&s1);
//	SeqListPushBack(&s1, 1);
//	SeqListPushBack(&s1, 2);
//	SeqListPushBack(&s1, 3);
//	SeqListPushBack(&s1, 4);
//	SeqListPushBack(&s1, 5);
//
//	SeqListPrint(&s1);
//	SeqListPopBack(&s1);
//	SeqListPopBack(&s1);
//	SeqListPopBack(&s1);
//	//SeqListPopBack(&s1);
//	//SeqListPopBack(&s1);
//	//SeqListPopBack(&s1);
//	SeqListPrint(&s1);
//
//	SeqListDestory(&s1);
//}
//
测试2
//TestSeqList2() {
//	SL s1;
//	SeqListInit(&s1);
//	SeqListPushBack(&s1, 1);
//	SeqListPushBack(&s1, 2);
//	SeqListPushBack(&s1, 3);
//	SeqListPushBack(&s1, 4);
//	SeqListPushBack(&s1, 5);
//	SeqListPrint(&s1);
//
//	SeqListPushFront(&s1, 10);
//	SeqListPushFront(&s1, 20);
//	SeqListPushFront(&s1, 30);
//	SeqListPrint(&s1);
//
//	SeqListPopFront(&s1);
//	SeqListPopFront(&s1);
//	SeqListPopFront(&s1);
//	SeqListPopFront(&s1);
//	SeqListPrint(&s1);
//
//	SeqListDestory(&s1);
//
//}
//
测试3
//TestSeqList3() {
//	SL s1;
//	SeqListInit(&s1);
//	SeqListPushBack(&s1, 1);
//	SeqListPushBack(&s1, 2);
//	SeqListPushBack(&s1, 3);
//	SeqListPushBack(&s1, 4);
//	SeqListPushBack(&s1, 5);
//	SeqListPrint(&s1);
//
//	SeqListDestory(&s1);
//
//}
测试4
//void TestSeqList4() {
//	SL s1;
//	SeqListInit(&s1);
//	SeqListPushBack(&s1, 1);
//	SeqListPushBack(&s1, 2);
//	SeqListPushBack(&s1, 3);
//	SeqListPushBack(&s1, 4);
//	SeqListPushBack(&s1, 5);
//	SeqListPrint(&s1);
//
//	SeqListInsert(&s1, 2, 30);
//	SeqListPrint(&s1);
//	int ret = SeqListFind(&s1, 4);
//	printf("%d\n", ret);
//
//	SeqListErase(&s1, 0);
//	SeqListErase(&s1, 1);
//	SeqListPrint(&s1);
//
//	SeqListDestory(&s1);
//
//}

void menu() {
	printf("\t\t\t**********************\t\t\t\n");
	printf("\t\t\t****请选择你的操作****\t\t\t\n");
	printf("\t\t\t*****1.头插2.头删*****\t\t\t\n");
	printf("\t\t\t*****3.尾插4.尾删*****\t\t\t\n");
	printf("\t\t\t*****5.查找6.打印*****\t\t\t\n");
	printf("\t\t\t*****7.指插8.指删*****\t\t\t\n");
	printf("\t\t\t*****9.排序10.清空****\t\t\t\n");
	printf("\t\t\t*********0.退出*******\t\t\t\n");
}

enum SeqListTest {
	Exit,
	PushFront,
	PopFront,
	PushBack,
	PopBack,
	Find,
	Print,
	Insert,
	Erase,
	Sort,
	Empty
};

void MenuTest() {
	SL s1;
	SeqListInit(&s1);//初始化
	int input = 0;
	do {
		menu();
		int x = 0;
		int pos = 0;
		scanf("%d", &input);
		switch (input) {
		case Exit:
			printf("程序退出\n");
			break;
		case PushFront:
			printf("请输入你要头插的数据,连续输入,以-1结束\n");
			scanf("%d", &x);
			while (x != -1) {
				SeqListPushFront(&s1, x);
				scanf("%d", &x);
			}
			break;
		case PopFront:
			SeqListPopFront(&s1);
			printf("头删一个成功\n");
			break;
		case PushBack:
			printf("请输入你要尾插的数据,连续输入,以-1结束\n");
			scanf("%d", &x);
			while (x != -1) {
				SeqListPushBack(&s1, x);
				scanf("%d", &x);
			}
			break;
		case PopBack:
			SeqListPopBack(&s1);
			printf("尾删一个成功\n");
			break;
		case Find:
			printf("请输入需要查找的元素\n");
			scanf("%d", &x);
			pos = SeqListFind(&s1, x);
			printf("要查找的元素下标在:%d\n", pos);
			break;
		case Print:
			SeqListPrint(&s1);
			break;
		case Insert:
			printf("请输入插入位置和元素大小\n");
			scanf("%d %d", &x, &pos);
			SeqListInsert(&s1, x, pos);
			break;
		case Erase:
			printf("请输入删除的位置\n");
			scanf("%d", &x);
			SeqListErase(&s1, x);
			printf("删除成功\n");
			break;
		case Sort:
			qsort(s1.a, s1.size, sizeof(s1.a[0]), SeqListSortName);
			printf("排序成功\n");
			break;
		case Empty:
			SeqListEmpty(&s1);
			printf("清空成功\n");
			break;
		default:
			printf("请重新输入\n");
			break;
		}
	} while (input);

	SeqListDestory(&s1);
}

int main() {
	//测试
	//TestSeqList1();
	//TestSeqList2();
	//TestSeqList3();
	//TestSeqList4();

	//实现
	MenuTest();
	return 0;
}

总结

顺序表劣势:

  1. 中间/头部的插入删除,时间复杂度为O(N)
  2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
  3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。

在进行完顺序表的学习,我们感受到了和通讯录有异曲同工之妙,相当于再次写了一遍通讯录,同样的,我们后期还可以加上文件的处理,将这些数据输出到文件当中,在进行数据结构的学习中当然还有更多更难的知识等待着我们进行探索,现在是开了个头哦~~


客官,来个三连支持一下吧!

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2022horse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值