顺序表的定义及其相关基本操作
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define maxSize 100 5 typedef struct{ 6 int data[maxSize]; // 存放顺序表元素的数组 7 int length; //存放顺序表的长度 8 }Sqlist; //顺序表定义的类型 9 10 // 但在考试中常用如下方式: 11 int A[maxSize]; 12 int n; 13 void initList(Sqlist &L){ 14 L.length = 0; 15 } 16 //在顺序表查找第一个值等于e的元素,并返回其下标 17 int FindElem(Sqlist L, int e){ 18 for(int i = 0;i < L.length; ++i) 19 if(e == L.data[i]) 20 return i; 21 return -1; 22 } 23 //用e返回顺序表中制定位置p(0<=p<=length-1)位置上的元素 24 int getElem(Sqlist L,int p,int &e){ //e要改变,所以用引用型 25 if(p < 0 || p >L.length-1) //p越界错误时返回-1 26 return -1; 27 e = L