线性表-顺序表

#include<iostream>
#include<vector>

using namespace std;

//结构定义
#define maxSize 50
typedef struct{
    int data[maxSize];
    int length;
}SqList;

//插入元素
bool insertList(SqList &L,int i,int e){
    //顺序表是否已满
    if(L.length >= maxSize){
        return false;
    }
    //i是否合法
    if(i > L.length || i < 1){
        return false;
    }

    for(int index = L.length; index >= i; index--){
        data[index] = data[index-1];
    }
    data[i-1] = e;
    L.length++;

    return true;
}

//删除元素
bool ListDelete(SqList &L,int i,int &e){
    //顺序表是否为空
    if(L.length <= 0){
        return false;
    }
    //i是否合法
    if(i < 1 || i > L.length){
        return false;
    }

    e = L.data[i-1];
    for(int index = i - 1; index < L.length-1; index++){
        L.data[index] = L.data[index+1];
    }
    L.length--;
    return true;
}

//定位元素
int LocateElem(SqList &L,int e){
    for(int index = 0; index < L.length; index++){
        if(L.data[index] == e){
            return index+1;
        }
    }
    return 0;
}

   

原文地址:https://www.cnblogs.com/buaaZhhx/p/12366367.html