顺序表的静态实现【数组】

我觉得我写的你们肯定都能看得懂!

#include <stdio.h>
#define MAXSIZE 10
#define ERROR1 -1
#define ERROR2 -2
#define OK 0
typedef int ElemType;
typedef int Status;
typedef struct seqlist
{
	ElemType data[MAXSIZE];
	int length;
}SEQLIST;
int index = 0, e = 0,res = 0,operation = 0;
void Menu()
{
	puts("**************************");
	puts("【1.初始化顺序表】");
	puts("【2.判断当前顺序表是否为空】");
	puts("【3.获取当前顺序表的长度】");
	puts("【4.增】");
	puts("【5.删】");
	puts("【6.改】");
	puts("【7.查】");
	puts("【8.遍历】");
	puts("【9.置空】");
	puts("【10.退出】");
	puts("**************************\n");
}
//初始化顺序表

void InitializeSeqlistElem(SEQLIST *L)
{
	
	L->length = 0;
	puts("成功完成初始化!\n");
}
//判断当前顺序表是否为空

Status IsEmptySeqlist(SEQLIST *L)
{
	if (L->length == 0)return OK;
	else return ERROR1;
}
//获取当前顺序表的长度

Status GetSeqlistLength(SEQLIST* L)
{
	return L->length;
}
//增
Status InsertSeqlistElem(SEQLIST *L, int index, ElemType e)
{
	
	if (L->length >= MAXSIZE)return ERROR1;
	if (index<1 || index>L->length + 1)return ERROR2;
	for (int i = L->length - 1; i > index - 1; i--)
		L->data[i + 1] = L->data[i];
	L->data[index - 1] = e;
	++L->length;
	return OK;
}
//删
Status DeleteSeqlistElem(SEQLIST *L, int index, ElemType e)
{
	
	
	if (L->length == 0)return ERROR1;
	if (index<1 || index>L->length)return ERROR2;
	for (int i = index - 1; i < L->length; i++)
		L->data[i] = L->data[i + 1];
	--L->length;
	return OK;
}
//改
Status UpdateSeqlistElem(SEQLIST *L, int index,ElemType e)
{
	if (L->length == 0)return ERROR1;
	if (index<1 || index>L->length)return ERROR2;
	printf("请输入修改后的元素值:"); scanf_s("%d", &e);
	L->data[index - 1] = e; return OK;
}

//查
Status LocateSeqlistElem(SEQLIST *L, int index, ElemType e)
{
	int ope = 0;
	puts("【1.按值查找  2.按下标查找】");
	printf("请输入待查方式ope:"); scanf_s("%d", &ope);
	if (ope == 1)
	{
		printf("按值查找-请输入待查元素的值:"); scanf_s("%d", &e);
		for (int i = 0; i < L->length; i++)
		{
			if (L->data[i] == e)
			{
				printf("待查元素的下标为:%d\n", i);
				return OK;
				break;
			}
		}
		return ERROR1;
	}	
	else if (ope == 2)
	{
		printf("按下标查找-请输入待查元素的下标:"); scanf_s("%d", &index);
		if (index<1 || index>L->length - 1)
			return ERROR2;
		else
         printf("当前下标对应的的元素为:%d", L->data[index]);
	}
	else
	{
		printf("当前查找方式有误,请重新输入!");
	}
}
//遍历
void DisplaySeqlistElem(SEQLIST *L)
{
	if (L->length == 0)puts("当前顺序表为空!");
	for (int i = 0; i < L->length; i++)
		printf("%d ", L->data[i]);
	puts("\n");
}
//置空
Status EmptySeqlistElem(SEQLIST *L)
{	
	L->length = 0;
	puts("置空完成!\n");
	return L->length;
}
int main()
{
	Menu();
	SEQLIST L;//创建一个顺序表
	while (ERROR1)
	{
		printf("请输入相关操作数operation:"); scanf_s("%d", &operation);
		switch (operation)
		{
		case 1:
			//初始化顺序表
			puts("【初始化顺序表】");
			InitializeSeqlistElem(&L);
			break;
		case 2:
			//判断当前顺序表是否为空
			puts("【判断当前顺序表是否为空】");
			res = IsEmptySeqlist(&L);
			if (res == -1)puts("当前顺序表为非空!\n");
			if (res == 0)puts("当前顺序表为空!\n");
			break;
		case 3:
			//获取当前顺序表的长度
			puts("【获取当前顺序表的长度】");
			res = GetSeqlistLength(&L);
			printf("当前顺序表的长度为%d\n", res);
			puts(" ");
			break;
		case 4:
			//增
			puts("【增】");
			printf("请输入待插入元素的位置:"); scanf_s("%d", &index);
			printf("请输入待插元素的值:"); scanf_s("%d", &e);
			res = InsertSeqlistElem(&L, index, e);
			if (res == -1)puts("当前顺序表已满!\n");
			else if (res == -2)puts("当前待插元素的位置不合理!\n");
			else puts("插入成功!\n");
			break;
		case 5:
			//删
			puts("【删】");
			printf("请输入待删入元素的位置:"); scanf_s("%d", &index);
			res = DeleteSeqlistElem(&L, index, 0);
			if (res == -1)puts("当前顺序表为空!\n");
			else if (res == -2)puts("当前待删元素的位置不合理!\n");
			else puts("删除成功!\n");
			break;
		case 6:
			//改
			puts("【改】");
			printf("请输入待修改元素的位置:"); scanf_s("%d", &index);
			res = UpdateSeqlistElem(&L, index, e);
			if (res == -1) puts("当前顺序表为空!\n");
			if (res == -2)puts("当前待修改下标不合理!\n");
			if (res == 0)puts("修改成功!\n");
			break;
		case 7:
			//查
			puts("【查】");
			res = LocateSeqlistElem(&L, index, e);
			if (res == 0)puts("成功找出!");
			if (res == -1)puts("查无此数!");
			if (res == -2)puts("当前待查下标不合理");
			puts("\n");
			break;
		case 8:
			//遍历
			puts("【遍历】");
			DisplaySeqlistElem(&L);
			break;
		case 9:
			//置空
			puts("【置空】");
			res = EmptySeqlistElem(&L);
			printf("当前顺序表长度为:%d\n", res);
			break;
		case 10:
			puts("【退出】");
			puts("成功退出!\n");
			return OK;
		default:
			puts("输入有误,请重新输入相关操作数!");
			return OK;
		}
	}
	return 0;
}

代码图片
调试图片

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页