数据结构(线性表_顺序表)
学习数据结构中的顺序表后写点笔记
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;
特征:
- 对非空表,a0是表头,无前驱;
- an-1是表尾,无后继;
- 其它的每个元素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!