本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、线性表的顺序存储结构
用一段地址连续的存储单元依次存储线性表的数据元素
二、顺序存储的定义
估算线性表的最大存储容量后,建立一个数组,数组的长度为该最大存储容量。
三、顺序存储的相关操作实现
1.线性表的初始化
顺序存储的线性表初始化只需将表长置为0即可
/*线性表的初始化*/
int InitList(SqList *L)
{
L->length=0;
return OK;
}
2.线性表的清空
类似于线性表的初始化
/*线性表的清空*/
int ClearList(SqList *L)
{
L->length=0;
return OK;
}
3.得到线性表的长度
/*返回线性表的长度*/
int ListLength(SqList L)
{
return L.length;
}
4.返回指定位次的元素
查找指定位次的元素是顺序存储的优势,它的时间复杂度为O(1)。
/*返回线性表中指定位次的元素*/
int GetElement(SqList L,int i,ElemType *e)
{
if(L.length==0||i<1||i>L.length)
return ERROR;
*e=L.data[i-1];
return OK;
}
5.获得第一个与指定元素匹配的位次
一个简单的for循环遍历查找
/*找到第一个与指定元素匹配的元素的位次*/
int LocateElement(SqList L,ElemType e)
{
int i;
if(L.length==0)
return ERROR;
for(i=0;i<L.length;++i)
{
if(L.data[i]==e)
break;
}
if(i>=L.length)
return ERROR;
return i+1;
}
6.遍历线性表中的所有元素
for循环遍历输出
/*遍历线性表中的所有元素*/
int ListTraverse(SqList L)
{
int i;
for(i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
printf("\n");
return OK;
}
7.在指定位置之前插入元素
插入操作相较于链式存储结构时间复杂度更大,也是顺序存储的一大劣势。
/*在指定位置插入元素*/
int ListInsert(SqList *L,int i,ElemType e)
{
int k;
if(L->length==MAXSIZE)
return ERROR;
if(i<1||i>L->length+1)
return ERROR;
if(i<=L->length)
{
for(k=L->length;k>=i;k--)
{
L->data[k]=L->data[k-1];
}
}
L->data[i-1]=e;
L->length++;
return OK;
}
8.删除指定位置的元素
删除操作和插入操作具有异曲同工之处。
/*删除指定位置的元素*/
int ListDelete(SqList *L,int i,ElemType *e)
{
int k;
if(L->length==0)
return ERROR;
if(i<1||i>L->length)
return ERROR;
*e=L->data[i-1];
if(i<L->length)
{
for(k=i-1;k<L->length-1;k++)
{
L->data[k]=L->data[k+1];
}
}
L->length++;
return OK;
}
9.得到两个线性表公有的元素
/*得到两个线性表公有的元素,并返回*/
SqList ListUnion(SqList La,SqList Lb)
{
SqList *Lc;
InitList(Lc);
int i,j;
ElemType e;
int La_len,Lb_len,Lc_len=0;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
for(i=1;i<=La_len;i++)
{
GetElement(La,i,&e);
if(LocateElement(La,e))
{
ListInsert(Lc,++Lc_len,e);
}
}
}
四、具有代码实现
具体代码是对线性表的线性存储结构的一些相关操作的实现,以及一个简单的使用测试。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE]; /*存储数据元素的线性表*/
int length; /*该线性表当前的长度-即当前存储元素的个数*/
}SqList;
/*线性表的初始化*/
int InitList(SqList *L)
{
L->length=0;
return OK;
}
/*线性表的清空*/
int ClearList(SqList *L)
{
L->length=0;
return OK;
}
/*返回线性表的长度*/
int ListLength(SqList L)
{
return L.length;
}
/*返回线性表中指定位次的元素*/
int GetElement(SqList L,int i,ElemType *e)
{
if(L.length==0||i<1||i>L.length)
return ERROR;
*e=L.data[i-1];
return OK;
}
/*找到第一个与指定元素匹配的元素的位次*/
int LocateElement(SqList L,ElemType e)
{
int i;
if(L.length==0)
return ERROR;
for(i=0;i<L.length;++i)
{
if(L.data[i]==e)
break;
}
if(i>=L.length)
return ERROR;
return i+1;
}
/*遍历线性表中的所有元素*/
int ListTraverse(SqList L)
{
int i;
for(i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
printf("\n");
return OK;
}
/*在指定位置插入元素*/
int ListInsert(SqList *L,int i,ElemType e)
{
int k;
if(L->length==MAXSIZE)
return ERROR;
if(i<1||i>L->length+1)
return ERROR;
if(i<=L->length)
{
for(k=L->length;k>=i;k--)
{
L->data[k]=L->data[k-1];
}
}
L->data[i-1]=e;
L->length++;
return OK;
}
/*删除指定位置的元素*/
int ListDelete(SqList *L,int i,ElemType *e)
{
int k;
if(L->length==0)
return ERROR;
if(i<1||i>L->length)
return ERROR;
*e=L->data[i-1];
if(i<L->length)
{
for(k=i-1;k<L->length-1;k++)
{
L->data[k]=L->data[k+1];
}
}
L->length++;
return OK;
}
/*得到两个线性表公有的元素,并返回*/
SqList ListUnion(SqList La,SqList Lb)
{
SqList *Lc;
InitList(Lc);
int i,j;
ElemType e;
int La_len,Lb_len,Lc_len=0;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
for(i=1;i<=La_len;i++)
{
GetElement(La,i,&e);
if(LocateElement(La,e))
{
ListInsert(Lc,++Lc_len,e);
}
}
}
void ListTest()
{
SqList L;
int i,j,k;
i=InitList(&L);
if(i==1)
{
printf("The length of the linear table after initialization is:%d\n",L.length);
}
for(i=1;i<=6;i++)
{
ListInsert(&L,i,i);
}
printf("After inserting 1-6 at the end of the table, the elements in the linear table are:\n");
ListTraverse(L);
i=ClearList(&L);
printf("After clearing the linear table, the length of the linear table is:%d\n",L.length);
for(i=1;i<=10;i++)
{
ListInsert(&L,1,i);
}
printf("After inserting 1-10 into the header, the elements in the linear table are:\n");
ListTraverse(L);
int len=L.length;
printf("The length of the list is %d\n",len);
}
int main()
{
ListTest();
return 0;
}
五、样例输出
The length of the linear table after initialization is:0
After inserting 1-6 at the end of the table, the elements in the linear table are: 1 2 3 4 5 6
After clearing the linear table, the length of the linear table is:0
After inserting 1-10 into the header, the elements in the linear table are: 10 9 8 7 6 5 4 3 2 1
The length of the list is 10
六、写在最后
这里是数据结构个人学习的笔记记录,如有问题欢迎指正说明。