#ifndef SQLIST
#define SQLIST
#include <iostream>
#include <stdio.h>
#define MaxSize 15
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}
SqList;
bool ListInsert(SqList &L,int i,ElemType e)
{
//插入位置错误
if (i<1||i>L.length+1) //1 2 ... length 为合法位置
return false;
//表满了
if (L.length>=MaxSize) //length == MaxSize时,表满
return false;
// j = length 到 i
for(int j = L.length;j>=i;j--) // 列表 1 2 ... [i i+1 i+2 ... Length] 后移
{
L.data[j] = L.data[j-1]; // 数组 0 1 ... [i-1 i i+1 ... Length-1] 后移
}
L.data[i-1] = e ; //
L.length++;
return true;
}
bool ListDelete(SqList &L,int i,ElemType &e)
{
if (i<1||i>L.length)
return false;
e = L.data[i-1];
// 数组 0 1 ... [i-1 i i+1 ... Length-1]
for(int j=i;j<=L.length-1;j++)
{
L.data[j-1] = L.data[j];
}
L.length--;
return true;
}
int LocateElem(SqList L,ElemType e)
{
for(int j=0;j<L.length;j++)
{
if(L.data[j] == e)
return j+1;
}
return 0;
}
void InitList(SqList &L)
{
L.length = 0;
}
void PrintList(char *name,SqList L)
{
std::cout << name << std::endl;
for(int i=1;i<=L.length;i++)
{
std::cout << L.data[i-1] << " ";
}
std::cout<< std::endl;
}
#endif // SQLIST
数据结构复习 顺序列表
最新推荐文章于 2021-07-01 12:35:29 发布