顺序存储定义
线性表是顺序存储结构,他是用一段地址连续的存储单元依次存储线性表的数据元素
线性表的顺序存储结构
//线性表顺序存储结构
#define MAXSIZE 20 //存储空间初始分配最大的size
typedef int ElemType; //假定ElemType的类型为int
typedef struct
{
ElemType data[MAXSIZE]; //数组来存储线性表中的元素,长度为MAXSIZE
int length; //当前的长度
}SqList; //设置当前结构体名为SqList
获得元素操作
对于线性表,要想实现GetElem的操作,即将线性表L中的第i个位置的元素范围即可,只要i的取值在MAXSIZE当前length范围内,就把数组(i-1)的下表的值返回即可。
//获得元素的操作
int GetElem(SqList L,int i,ElemType * e)
{
if(L.length == 0 || i<1 || i>L.length)
{
return ERROR;
}
*e = L.data[i-1];
return OK;
}
运行主程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//线性表顺序存储结构
#define MAXSIZE 20 //存储空间初始分配最大的size
#define ERROR 0
#define OK 1
typedef int ElemType; //假定ElemType的类型为int
typedef struct
{
ElemType data[MAXSIZE]; //数组来存储线性表中的元素,长度为MAXSIZE
int length; //当前的长度
}SqList; //设置当前结构体名为SqList
//获得元素的操作
int GetElem(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 InitList(SqList * L)
{
L->length = 0;
return OK;
}
//创建线性表
int CreateList(SqList * L,int size)
{
if(size>MAXSIZE)
{
return ERROR;
}
srand(time(0));
for(int i=0;i<size;i++)
{
L->data[i] = rand()%10 + 1;
L->length++;
}
return OK;
}
//打印线性表
int PrintList(SqList L)
{
for(int i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
printf("\n");
return OK;
}
int main()
{
SqList L;
ElemType e;
InitList(&L); //初始化线性表
CreateList(&L,10); //创建长度为10的随机数线性表
PrintList(L); //打印该线性表
GetElem(L,2,&e); //获得第二个元素的值
printf("%d\n",e);
return 0;
}
运行结果:
插入操作
插入算法的思路:
- 如果插入的位置不合理,返回ERROR。
- 如果线性表的长度大于等于数组长度(线性表的最大长度),返回ERROR
- 如果插入的位置不在表尾,从最后一个元素向前遍历到第i个位置,分别将他们往后移动一个位置,如果在表尾直接插入。
- 将要插入的元素填入位置i处。
- 线性表长度加1。
//线性表插入
int InsertList(SqList * L,int i,ElemType e)
{
if(L->length == MAXSIZE)
{
return ERROR;
}
if(i<1 || i>L->length + 1)
{
return ERROR;
}
if(i<=L->length) //如果插入的位置不在表尾
{
for(int j=L->length;j>=i-1;j--)
{
L->data[j+1] = L->data[j];
}
}
L->data[i-1] = e;
L->length++;
return OK;
}
运行主程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//线性表顺序存储结构
#define MAXSIZE 20 //存储空间初始分配最大的size
#define ERROR 0
#define OK 1
typedef int ElemType; //假定ElemType的类型为int
typedef struct
{
ElemType data[MAXSIZE]; //数组来存储线性表中的元素,长度为MAXSIZE
int length; //当前的长度
}SqList; //设置当前结构体名为SqList
//获得元素的操作
int GetElem(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 InitList(SqList * L)
{
L->length = 0;
return OK;
}
//创建线性表
int CreateList(SqList * L,int size)
{
if(size>MAXSIZE)
{
return ERROR;
}
srand(time(0));
for(int i=0;i<size;i++)
{
L->data[i] = rand()%10 + 1;
L->length++;
}
return OK;
}
//打印线性表
int PrintList(SqList L)
{
for(int i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
printf("\n");
return OK;
}
//线性表插入
int InsertList(SqList * L,int i,ElemType e)
{
if(L->length == MAXSIZE)
{
return ERROR;
}
if(i<1 || i>L->length + 1)
{
return ERROR;
}
if(i<=L->length) //如果插入的位置不在表尾
{
for(int j=L->length;j>=i-1;j--)
{
L->data[j+1] = L->data[j];
}
}
L->data[i-1] = e;
L->length++;
return OK;
}
int main()
{
SqList L;
ElemType e;
InitList(&L); //初始化线性表
CreateList(&L,10); //创建长度为10的随机数线性表
PrintList(L); //打印该线性表
GetElem(L,2,&e); //获得第二个元素的值
printf("%d\n",e);
InsertList(&L,2,8);//第二个位置插入元素8
PrintList(L); //打印该线性表
GetElem(L,2,&e); //获得第二个元素的值
printf("%d\n",e);
return 0;
}
运行结果:
删除操作
删除算法的思路:
- 如果删除的位置不合理,返回ERROR。
- 取出删除的元素。
- 如果删除的位置不是线性表尾则从删除的元素位置开始遍历到最后一个元素的位置,分别将它们都往前移动一个位置,如果删除的位置是线性表尾,就直接移除,长度减1。
- 线性表长度减1。
删除线性表元素
//删除线性表
int DeleteList(SqList * L,int i,ElemType * e)
{
if(L->length == 0) //线性表为空
{
return ERROR;
}
if(i<1 || i>L->length) //删除位置不正确
{
return ERROR;
}
*e = L->data[i-1];
if(i<L->length) //删除的位置不是线性表尾
{
for(int j=i;j<L->length;j++) //将删除位置后的元素向前移动
{
L->data[j-1] = L->data[j];
}
}
L->length--;
return OK;
}
运行主程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//线性表顺序存储结构
#define MAXSIZE 20 //存储空间初始分配最大的size
#define ERROR 0
#define OK 1
typedef int ElemType; //假定ElemType的类型为int
typedef struct
{
ElemType data[MAXSIZE]; //数组来存储线性表中的元素,长度为MAXSIZE
int length; //当前的长度
}SqList; //设置当前结构体名为SqList
//获得元素的操作
int GetElem(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 InitList(SqList * L)
{
L->length = 0;
return OK;
}
//创建线性表
int CreateList(SqList * L,int size)
{
if(size>MAXSIZE)
{
return ERROR;
}
srand(time(0));
for(int i=0;i<size;i++)
{
L->data[i] = rand()%10 + 1;
L->length++;
}
return OK;
}
//打印线性表
int PrintList(SqList L)
{
for(int i=0;i<L.length;i++)
{
printf("%d ",L.data[i]);
}
printf("\n");
return OK;
}
//线性表插入
int InsertList(SqList * L,int i,ElemType e)
{
if(L->length == MAXSIZE)
{
return ERROR;
}
if(i<1 || i>L->length + 1)
{
return ERROR;
}
if(i<=L->length) //如果插入的位置不在表尾
{
for(int j=L->length;j>=i-1;j--)
{
L->data[j+1] = L->data[j];
}
}
L->data[i-1] = e;
L->length++;
return OK;
}
//删除线性表
int DeleteList(SqList * L,int i,ElemType * e)
{
if(L->length == 0) //线性表为空
{
return ERROR;
}
if(i<1 || i>L->length) //删除位置不正确
{
return ERROR;
}
*e = L->data[i-1];
if(i<L->length) //删除的位置不是线性表尾
{
for(int j=i;j<L->length;j++) //将删除位置后的元素向前移动
{
L->data[j-1] = L->data[j];
}
}
L->length--;
return OK;
}
int main()
{
SqList L;
ElemType e;
InitList(&L); //初始化线性表
CreateList(&L,10); //创建长度为10的随机数线性表
PrintList(L); //打印该线性表
GetElem(L,2,&e); //获得第二个元素的值
printf("%d\n",e); //打印第二个元素的值
InsertList(&L,2,8);//第二个位置插入元素8
PrintList(L); //打印该线性表
GetElem(L,2,&e); //获得第二个元素的值
printf("%d\n",e); //打印第二个元素的值
DeleteList(&L,2,&e); //删除第二个位置的元素
PrintList(L); //打印该线性表
printf("%d\n",e); //打印删除的元素
return 0;
}
运行结果:
线性表的优点
- 无须为表示表中元素之间的逻辑关系而增加额外的存储空间
- 可以快速地存储表中任何位置的元素
线性表的缺点
- 插入和删除操作需要移动大量元素
- 当线性表长度变化较大时,难以确定存储空间的容量
- 会造成存储空间的”碎片”