数据结构的算法怎么算?

线性表的顺序表示和实现:如建立、查找、插入和删除

初始化顺序表L;

依次采用尾插法插入a,b,c,d,e元素;

输出顺序表L;

输出顺序表L的长度;

判断顺序表L是否为空;

输出顺序表L的第3个元素;

输出元素a的位置;

在第4个元素位置上面插入f元素;

输出顺序表L;

删除L的第3个元素;

输出顺序表L;

释放顺序表



```c
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型

#define MAXSIZE 100            //顺序表可能达到的最大长度

typedef struct {
    ElemType *elem; //存储空间的基地址
    int length; //当前长度
} SqList;

Status InitList(SqList &L) { //算法2.1 顺序表的初始化
    //构造一个空的顺序表L
    
    L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
    if (!L.elem)
        exit(OVERFLOW); //存储分配失败退出
    L.length = 0; //空表长度为0
    int i,n;
    cout<<"请输入n的值:"<<endl;
    cin>>n;
    cout<<"请输入他们的值:"<<endl;
    for(int i;i<n;i++)
    {
        cin>>L.elem[i] ;
        L.length++;
    }
    return OK;
}

Status GetElem(SqList L, int i, ElemType &e) {//算法2.2 顺序表的取值
if(i<1||i>L.length ) return ERROR;
e=L.elem[i-1] ;
cout<<e<<endl;
return OK;
    //TODO
}

int LocateElem(SqList L, ElemType e) { //算法2.3 顺序表的查找
    //顺序表的查找
    
    for(int i=0;i<L.length ;i++)
    if(L.elem[i]==e) return i+1;
    return 0;
    //TODO
    
}

Status ListInsert(SqList &L, int i, ElemType e) { //算法2.4 顺序表的插入
    //在顺序表L中第i个位置之前插入新的元素e
    //i值的合法范围是1<=i<=L.length+1
    if((i<1)||(i>L.length +1)) return ERROR;
    if(L.length ==MAXSIZE)return ERROR;
    for(int j=L.length -1;j>=i-1;j--)
    L.elem[j+1]=L.elem [j];
    L.elem[i-1]=e;
    ++L.length ;
    return OK;
    
//    //TODO


}

Status ListDelete(SqList &L, int i) { //算法2.5 顺序表的删除
    //在顺序表L中删除第i个元素,并用e返回其值
    //i值的合法范围是1<=i<=L.length

    //TODO
    
    if((i<1)||(i>L.length ))return ERROR;
    for(int j=i;j<=L.length -1;j++)
    L.elem[j-1]=L.elem [j];
    --L.length ;
    return OK;
    
}
void VisitList(SqList L)
{ 
    int i;
    for(i=0;i<L.length;i++)
cout<<L.elem[i]<<"  ";
    cout<<endl;
}


int main()
{SqList S;
    int a;
InitList(S)    ;
cout<<"输出顺序表的全部值:"; 
VisitList(S);
if(LocateElem(S,28)==0) 
cout<<"没有找到28"<<endl;
else
cout<<"找到了28,位置在:" << LocateElem(S,28)<<endl;
if(LocateElem(S,25)==0)
cout<<"没有找到25"<<endl;
else
cout<<"找到了25,位置在:" << LocateElem(S,25)<<endl;
cout<<"输出第5个插入9后的值:"; 
ListInsert(S,5,9);
VisitList(S);
cout<<"输出删除第5值的顺序表的所有值:";
ListDelete(S,5);
VisitList(S);
cout<<"输出第三个位置的值为:";
GetElem(S,3,a);
cout<<"数据表的长度为:"; 
cout<<S.length ;
    //TODO
    return OK;
}



```