数据结构(线性表_顺序表)

数据结构(线性表_顺序表)

学习数据结构中的顺序表后写点笔记

1.基本概念

线性表是包含若干数据元素的一个线性序列

记为: L=(a0, … ai-1, ai, ai+1 … an-1)
L为表名,ai (0≤i≤n-1)为数据元素;
n为表长,n>0 时,线性表L为非空表,否则为空表。

顺序表是将 L=(a0, … ai-1, ai, ai+1 … an-1)中的各元素依次存储于计算机一片连续的存储空间,在c中可用一位数组类型来描述顺序表的顺序结构

typedef int data_t;
#define N 128
typedef struct{
	data_t data[N];
	int last;
}sqlist,*sqlink;

在这里插入图片描述
特征:

  1. 对非空表,a0是表头,无前驱;
  2. an-1是表尾,无后继;
  3. 其它的每个元素ai有且仅有一个直接前驱ai-1和一个直接后继ai+1。

顺序存储结构的不足:对表的插入和删除等运算的时间复杂度较差。

2.功能实现

2.1 头文件

定义结构体定义结构体
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2 功能函数实现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.3 test-实现效果部分

int test1();//去除线性表中重复元素
int test2();//合并线性表

int main()
{
	int ret;
	printf("去除线性表中重复元素:\n");
	ret = test1();
	printf("ret1=%d\n\n",ret);
	printf("合并线性表:\n");
	ret = test2();
	printf("ret2=%d\n",ret);	
	return 0;
}

int test1()
{
	sqlink L;
	L = list_create();
	if(L == NULL){
		printf("created failed!\n");
		return -1;
	}
	list_intsert(L,1,0);
	list_intsert(L,1,1);
	list_intsert(L,1,2);
	list_intsert(L,2,3);
	list_intsert(L,3,4);
	list_intsert(L,4,5);
	list_intsert(L,4,6);
	list_intsert(L,5,7);
	list_intsert(L,6,8);
	list_show(L);
	list_purge_samedata(L);
	printf("去除重复元素后:\n");
	list_show(L);
	list_free(L);
	return 0;
}

int test2()
{
	sqlink La,Lb;
	La = list_create();
	if(La== NULL){
		printf("La created failed!\n");
		return -1;
	}
	Lb = list_create();
	if(Lb == NULL){
		printf("Lb created failed!\n");
		return -1;
	}
	list_intsert(La,1,0);
	list_intsert(La,2,1);
	list_intsert(La,3,2);
	list_intsert(La,4,3);
	list_intsert(Lb,4,0);
	list_intsert(Lb,5,1);
	list_intsert(Lb,6,2);
	list_intsert(Lb,7,3);
	printf("合并前:\n");
	list_show(La);
	list_show(Lb);
	list_merge(La,Lb);
	printf("合并后:\n");
	list_show(La);
	list_show(Lb);	
	list_free(La);
	list_free(Lb);
	return 0;
}

运行结果:在这里插入图片描述

Thank you for your visit!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值