顺序表的增删查,基于王道数据结构:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef int ElemType;
//静态分配
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
//动态分配
#define InitSize 100
typedef struct{
ElemType *data;
int capacity; //动态数组的最大容量
int length;
}SeqList;
//i代表插入的位置,从1开始
bool ListInsert(SqList &L,int i,ElemType e)
{
if(i<1||i>L.length+1) //判断要插入的位置是否合法
return false;
if(L.length>=MaxSize)
return false;
for(int j=L.length;j>=i;j--) //移动顺序表中的元素
L.data[j]=L.data[j-1];
L.data[i-1]=e; //数组下标从零开始,插入第一个位置,访问的下标为0
L.length++;
return true;
}
//删除使用元素e的引用的目的是拿出对应的值
boo