顺序表插入

cpp 复制代码
#include <iostream>


#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define MaxSize 10
typedef struct {
    int data[MaxSize];
    int length;
}SeqList;

void InitList(SeqList &L ){
    for(int i = 0 ; i < L.length ; i++){
        L.data[i] = 0;
    }
}
int InserList(SeqList &L , int i, int e){
    if(i > MaxSize || i<1){
        return -1;
    }
    if(MaxSize==L.length){
        return -2;
    }

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

void  show(SeqList L){
    for(int i = 0 ; i< MaxSize ; i++){
        printf("List 【%d】:%d \n",i,L.data[i]);
    }

}

int main(){
    SeqList L;
    InitList(L);
    InserList(L,0,888);
    printf("%d \n",InserList(L,0,888));
    printf("length: %d \n",L.length);
    show(L);




    return 0;
}
相关推荐
lisw053 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术4 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs5 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057595 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
GoAI16 小时前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试
Pniubi17 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
choumou_M19 小时前
算法_1:由浅入深学习快速排序QuickSort
学习·算法·排序算法
Coder_Shenshen20 小时前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
心仪久了会沦陷1 天前
C语言数据结构入门——树与二叉树详解:概念、堆实现与链式遍历
c语言·数据结构·算法