zoukankan      html  css  js  c++  java
  • 线性表-顺序表

    #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;
    }
    

       

  • 相关阅读:
    CF101B Buses
    CF1C Ancient Berland Circus
    学习笔记 莫比乌斯反演简单整理
    P3768 简单的数学题
    P2508 [HAOI2008]圆上的整点
    CF19E Fairy
    P1295 [TJOI2011]书架
    CF1148B Born This Way
    CF13E Holes
    CF1148C Crazy Diamond
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/12366367.html
Copyright © 2011-2022 走看看