顺序表的基本操作

初始化InitList操作

插入ListInsert操作

删除ListDelete操作

查找:

按位查找GetElem操作

按值查找LocateElem操作

注:具体实现操作及说明可以参考一下代码及注释哦

(静态数组):

#include<stdio.h>
#define MaxSize 10//定义最大表长
#include<stdlib.h>
typedef struct {
	int data[MaxSize];//静态数组保存数据元素
	int length;//顺序表长度
}SeqList;
//初始化新的顺序表
void InitList(SeqList& L) {
	for (int i = 0; i < MaxSize; i++) {
		L.data[i] = 0;//将所有的元素设默认初始值
	}
	L.length = 0;//表长为空
}
//输出打印顺序表,可以不用创建打印函数,加上是为了方便测试输出
void SqListPrint(SeqList& L) {
	for (int i = 0; i < L.length; i++) {
		printf("data[%d]=%d\n", i, L.data[i]);
	}
}
//顺序表的插入,第i个位置上插入e
bool ListInsert(SeqList &L,int i,int e) {
	if (i<1 || i>L.length + 1)//判断插入的位置范围是否有效
		return false;
	if (L.length >= MaxSize)//存储空间已满不能插入
		return false;
	for (int j = L.length; j >= i; j--) {
		L.data[j] = L.data[j - 1];//将第i个元素之后的每个元素后移一位
	}
	L.data[i-1] = e;//位置i除放入e
	L.length++;
	return true;
}
//顺序表的删除操作
bool ListDelete(SeqList& L, int i, int& e) {
	if (i<1 || i>L.length)//判断范围是否有效
		return false;
	e = L.data[i - 1];//将要删除的元素传给e
	for (int j = i; j < L.length; j++)//第i个位置后的元素依次前移
		L.data[j - 1] = L.data[j];
	L.length--;
	return true;
}
//顺序表按位查找位序为i的值并返回元素值
int GetElem(SeqList L, int i) {
	return L.data[i - 1];
}
//顺序表按值查找值为e的值然后返回其位序
int LocateElem(SeqList L, int e) {
	for (int i = 0; i < L.length; i++) //遍历整个表找到等于e的值
		if (L.data[i] == e)
			return i + 1;
	return 0;
}
void test() {//测试
	SeqList L;
	InitList(L);
	ListInsert(L, 1, 2);
	ListInsert(L, 2, 11);
	ListInsert(L, 3, 10);
	ListInsert(L, 4, 15);
	ListInsert(L, 5, 6);
	ListInsert(L, 6, 9);
	ListInsert(L, 7, 8);
	SqListPrint(L);
	printf("修改后的顺序表:\n");
	printf("需要查找元素值的位序为:%d\n", LocateElem(L, 15));
	printf("按位序查找的元素值为:%d\n", GetElem(L, 3));
	int e=0;//获取已经删除的值
	if (ListDelete(L, 5, e))
		printf("已删除,删除的元素值为=%d:\n", e);
	else
		printf("位序i不合法,删除失败\n");
	SqListPrint(L);
	printf("删除后表长为:%d\n", L.length);
}
int main() {
	test();
	return 0;
}

(动态数组):

#include<stdio.h>
#define InitSize 10
#include<stdlib.h>
typedef struct{
	int* data;//动态分配数组的指针
	int MaxSize;
	int length;
}SeqList;
void InitList(SeqList& L) {//用malloc函数申请一片连续的存储空间
	L.data = (int*)malloc(sizeof(int)*InitSize);
	L.length = 0;
	L.MaxSize = InitSize;
}
//增加动态数组的长度
void IncreaseSize(SeqList& L, int len) {
	int* p = L.data;
	L.data = (int*)malloc(sizeof(int) * (len + L.MaxSize));
	for (int i = 0; i < L.length; i++) {
		L.data[i] = p[i];//将原数据复制到新空间中
	}
	L.MaxSize += len;//顺序表长度增加len
	free(p);
}
//输出打印顺序表,可以不用创建打印函数,加上是为了方便测试输出
void SqListPrint(SeqList& L) {
	for (int i = 0; i < L.length; i++) {
		printf("data[%d]=%d\n", i, L.data[i]);
	}
}
//顺序表的插入,第i个位置上插入e
bool ListInsert(SeqList& L, int i, int e) {
	if (i<1 || i>L.length + 1)//判断插入的位置范围是否有效
		return false;
	//存储空间已满扩大空间
	if (L.length >= L.MaxSize) {
		printf("超出最大空间,已重新分配较大空间\n");
		IncreaseSize(L, 5);
	}		
	for (int j = L.length; j >= i; j--) {
		L.data[j] = L.data[j - 1];//将第i个元素之后的每个元素后移一位
	}
	L.data[i - 1] = e;//位置i除放入e
	L.length++;
	return true;
}
//顺序表的删除操作
bool ListDelete(SeqList& L, int i, int& e) {
	if (i<1 || i>L.length)//判断范围是否有效
		return false;
	e = L.data[i - 1];//将要删除的元素传给e
	for (int j = i; j < L.length; j++)//第i个位置后的元素依次前移
		L.data[j - 1] = L.data[j];
	L.length--;
	return true;
}
//顺序表按位查找位序为i的值并返回元素值
int GetElem(SeqList L, int i) {
	return L.data[i - 1];
}
//顺序表按值查找值为e的值然后返回其位序
int LocateElem(SeqList L, int e) {
	for (int i = 0; i < L.length; i++) //遍历整个表找到等于e的值
		if (L.data[i] == e)
			return i + 1;
	return 0;
}
void test() {//测试
	SeqList L;
	InitList(L);
	ListInsert(L, 1, 2);
	ListInsert(L, 2, 11);
	ListInsert(L, 3, 10);
	ListInsert(L, 4, 15);
	ListInsert(L, 5, 6);
	ListInsert(L, 6, 9);
	ListInsert(L, 7, 8);
	ListInsert(L, 8, 21);
	ListInsert(L, 9, 4);
	ListInsert(L, 10,16);
	ListInsert(L, 11, 18);
	SqListPrint(L);
	printf("修改后的顺序表:\n");
	printf("需要查找元素值的位序为:%d\n", LocateElem(L, 15));
	printf("按位序查找的元素值为:%d\n", GetElem(L, 3));
	int e = 0;//获取已经删除的值
	if (ListDelete(L, 5, e))
		printf("已删除,删除的元素值为=%d:\n", e);
	else
		printf("位序i不合法,删除失败\n");
	SqListPrint(L);
	printf("删除后表长为:%d\n", L.length);
	printf("当前数组最大容量:%d\n", L.MaxSize);//当前数组最大容量
	IncreaseSize(L, 5);//测试动态增加内存是否可行
	printf("动态增加之后的容量:%d\n", L.MaxSize);//动态增加之后的容量
}
int main() {
	test();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值