顺序存储
SqList.h

#include <cstdlib>
#include<iostream>
using namespace std;
#define MaxSize 50
#define ElementType int
typedef struct {
    ElementType *data;
    int length;
} SqList;

//初始化一个线性表
SqList* initNewSqList();

//遍历线性表
void printAll(SqList* list);

//在第i个位置进行插入操作
bool ListInsert(SqList*list,int i,ElementType e);

//append操作,即在末尾追加元素
bool Append(SqList* list,ElementType e);

//删除某个位置的元素
bool DeleteElement(SqList* list,int i, ElementType* e);

//寻找元素出现的第一个位置
int LocationElem(SqList* list,ElementType e);

SqList.cpp

#include "SqList.h"

SqList* initNewSqList(){
    auto* list =(SqList*)malloc(sizeof(SqList));
    list->data=(ElementType*)malloc(sizeof(ElementType)*MaxSize);
    list->length=0;
    return list;
}

bool ListInsert(SqList* list,int i,ElementType e){
    if(i<1||i>list->length+1)
        return false;
    if(list->length>=MaxSize)
        return false;
    for(int j=list->length;j>=i;j--)
        list->data[j]=list->data[j-1];
    list->data[i-1]=e;
    list->length++;
    return true;
}

bool Append(SqList *list, ElementType e) {
    if(list->length>=MaxSize)
        return false;
    list->data[list->length]=e;
    list->length++;
    return true;
}

void printAll(SqList *list) {
    for(int i=0;i<list->length;i++){
        cout<<list->data[i]<<" ";
    }
    cout<<endl;

}

bool DeleteElement(SqList *list, int i,ElementType* e) {
    if(i>list->length)
        return false;
    (*e)=list->data[i-1];
//    cout<<"debug"<<list->data[i-1]<<endl;
    for(int j=i;j<list->length;j++)
        list->data[j-1]=list->data[j];
    list->length--;
    return true;
}

int LocationElem(SqList* list,ElementType e){
    for(int i=0;i<list->length;i++){
        if(list->data[i]==e){
            return i+1;
        }
    }
    return 0;
}

main.cpp,用于测试

#include "SqList.h"

int main()
{
    auto list=initNewSqList();
    cout<<list->length<<endl;
    ListInsert(list,1,19);
    ListInsert(list,2,17);
    ListInsert(list,1,77);
    Append(list,1000);
    printAll(list);
    ElementType e;
    cout<<DeleteElement(list,1,&e);
    printAll(list);
    cout<<e<<endl;
    cout<<LocationElem(list,17);
    return 0;
}

cMakeLists

# cmake_minimum_required(VERSION <specify CMake version here>)
project(ForKY)
set(CMAKE_CXX_STANDARD 17)
add_executable(ForKY main.cpp SqList.cpp SqList.h)