线性表的顺序表示和实现

数据结构—线性表

数据的逻辑结构:
线性结构、树形结构、图形结构。
数据的存储结构(物理结构&&逻辑结构在内存中的存储形式):
顺序存储结构、链式存储结构。
线性表:最简单的线性结构。
顺序表:顺序存储结构的线性表。
|
用C语言描述,定义结构体SeqList:
//SeqList.h

typedef struct {
	DataType list[MaxSize];//MaxSize最大存储元素个数
	int size;//当前存储元素的个数
} SeqList;

定义MaxSize的大小||DataType的数据类型为学生类
在主文件中调用S而且List.h
//main.cpp

#include<stdio.h>
#define MaxSize 30
typedef struct {
	int id;
	char name[20];
} student;//学生结构体
typedef student DataType;
#include"SeqList.h"

顺序表的操作集合

  1. 初始化ListInitiate(L)
  2. 求当前数据元素个数ListLength(L)
  3. 插入数据元素ListInsert(L, i, x)
  4. 删除数据元素ListDelete(L, i, x)
  5. 向末尾添加数据元素ListAdd(L, x)
  6. 取数据元素ListGet(L, i, x)

//SeqList.h

//初始化
void ListInitiate(SeqList* L) {//参数为指针类型,地址传递
	L->size = 0;
 }
//求当前数据元素个数
int ListLength(SeqList L) {
	return L.size;
}
//插入一个数据元素i
int ListInsert(SeqList *L, int i, DataType x) {
	int j;
	if (L->size >= MaxSize) {
		printf("顺序表已满无法插入!\n");
		return 0;
	}
	else if (i < 0 || i>L->size) {
		printf("参数i不合法!\n");
		return 0;
	}
	else {
		for (j = L->size - 1; j >= i; j--) {
			L->list[j + 1] = L->list[j];//将参数i&&i之后的元素后移一位
		}
		L->list[i] = x;
		L->size++;
		return 1;
	}
}
//删除一个数据元素i
int ListDelete(SeqList* L, int i, DataType* x) {
	int j;
	if (L->size <= 0) {
		printf("顺序表已无数据可删!\n");
	}
	else if (i < 0 || i > L -> size - 1) {
		printf("参数i不合法!\n");
		return 0;
	}
	else {
		*x = L->list[i];
		for (j = i + 1; j <= L->size - 1; j++) {
			L->list[j - 1] = L->list[j];//将参数i之后的元素前移一位
			L->size--;
			return 1;
		}
	}
}
//向集合的最后添加元素
int ListAdd(SeqList *L, DataType x){
	if (L->size == MaxSize) {
		printf("集合元素已达到最大个数!\n");
		return 0;
	}
	else {
		L->list[L->size] = x;
		L->size++;
		return 1;
	}
}
//取数据元素
int ListGet(SeqList L, int i, DataType *x){
	if (L.size <= 0) {
		printf("顺序表已空无数据可删!\n");
		return 0;
	}
	else if (i < 0 || i > L.size - 1) {
		printf("参数i不合法!\n");
		return 0;
	}
	else {
		*x = L.list[i];
		return 1;
	}
}

插入数据元素实现(插入学生赵六)
删除数据元素实现(删除学生李四)
//main.cpp

#include<stdio.h>
#define MaxSize 30
typedef struct {
	int id;
	char name[20];
} student;//学生结构体
typedef student DataType;
#include"SeqList.h"
int main(void) {
	SeqList List;//定义一个SeqList类型的List结构体
	int i;
	student stu;//定义一个student类型的stu结构体
	ListInitiate(&List);//初始化为0
	student zs = { 1, "张三" };
	student ls = {2, "李四"};
	student ww = {3, "王五"};
	student zl = { 4, "赵六" };
	ListAdd(&List, zs);//向末尾添加张三
	ListAdd(&List, ls);//向末尾添加李四
	ListAdd(&List, ww);//向末尾添加王五
	printf("--------------------------\n");
	//取数据元素并输出
	for (i = 0; i < ListLength(List); i++) {
		ListGet(List, i, &stu);
		printf("%d = %s \n", stu.id, stu.name);
	}
	ListInsert(&List, 1, zl);//插入学生赵六
	printf("--------------------------\n");
	for (i = 0; i < ListLength(List); i++) {
		ListGet(List, i, &stu);
		printf("%d = %s \n", stu.id, stu.name);
	}//删除学生李四
	ListDelete(&List, 2, &stu);
	printf("--------------------------\n");
	for (i = 0; i < ListLength(List); i++) {
		ListGet(List, i, &stu);
		printf("%d = %s \n", stu.id, stu.name);
	}
	return 0;
}

最后运行结果显示~

第一个模块添加三个学生
第二个模块插入学生赵六
第三个模块删除学生李四
制作不易,点个赞哦

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

只影~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值