线性表
1.顺序表示
1.1 定义
数组从0开始,顺序表从1开始
#define MaxSize 100 //最大长度
typedef struct{
ElemType *date; //指向数据元素的基地址
int length; //线性表的当前长度
}SqList;
bool InitList_Sq(SqList &L)
{
L.data = new ElemType[MaxSize];
if(!L.data)
exit(OVERFLOW);
L.length = 0;
return true;
}
1.2 基本操作
1.2.1 查找
注意:是返回其位序
//在线性表L中查找值为e的数据元素,并返回其位序
int LocateElem_Sq(SqList L, ElemType e)
{
for(int i = 0; i < L.length; i++)
{
if(L.date[i] == e)
return i+1;
}
return 0;
}
算法平均时间复杂度为O(n)。
1.2.2 插入
//在线性表L中第i个位置插入元素e
//判断插入位置i是否合法
//判断顺序表的存储空间是否已满
//将第n至第i位元素依次向后移动一个位置
//将要插入的元素e放入第i个位置
//表长加1,插入成功返回true
//在线性表L中第i个位置插入元素e
bool ListInsert_Sq(SqList &L, int i, ElemType e)
{
//判断插入位置i是否合法
if( i<1 || i>L.length+1 )