过程
在第pos个位置插入
short int ListInsert(SeqList &L, int pos, int elem) {
if (pos < 1 || pos > L.length + 1) {
return (short int) 1;
} else if (L.length >= InitSize) {
return (short int) 2;
}
for (int i = L.length; i >= pos; i--) {
L.data[i] = L.data[i - 1];
}
L.data[pos - 1] = elem;
L.length++;
return (short int) 3;
}
删除第pos个元素
short int ListDelete(SeqList &L,int pos, int &e) {
if (pos < 1 || pos > L.length) {
return (short int) 1;
} else if (L.length == 0) {
return (short int) 2;
}
e = L.data[pos - 1];
for (int i = pos; i < L.length; i++) {
L.data[i - 1] = L.data[i];
}
L.data[L.length - 1] = 0;
L.length--;
return (short int) 3;
}
查找
按值查找
int LocateElem(SeqList L, int elem) {
int pos = -1;
for (int i = 0; i < L.length; i++) {
if (elem == L.data[i - 1]) {
pos = i;
break;
}
}
return pos;
}
按照位序查找
int GetElem(SeqList L, int pos) {
if (pos < 1 || pos > L.length) return INT_MIN;
return L.data[pos - 1];
}
整体代码
#include <iostream>
#include <limits>
using namespace std;
#define InitSize 10
typedef struct {
int data[InitSize];
int length;
}SeqList;
short int ListInsert(SeqList &L, int pos, int elem);
void initList(SeqList &L);
short int ListDelete(SeqList &L,int pos, int &e);
void print(SeqList L);
int GetElem(SeqList L, int pos);
int LocateElem(SeqList L, int elem);
int main() {
SeqList L;
initList(L);
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 3;
L.length = 3;
print(L);
short int flag;
flag = ListInsert(L, 4, 4);
if (flag == 1) {
printf("插入位置不合法......\n");
} else if (flag == 2) {
printf("当前无位置可插入......\n");
} else if (flag == 3) {
printf("插入成功,当前表内内容:");
print(L);
system("pause");
}
int e;
flag = ListDelete(L,3,e);
if (flag == 1) {
printf("删除位置不合法!\n");
} else if (flag == 2) {
printf("当前表中已经没有元素可以删除!");
} else if (flag == 3) {
printf("删除成功,删除的元素是:%d\n",e);
printf("当前表中元素:\a");
print(L);
}
int res = GetElem(L, 9);
if (res == INT_MIN) {
cout << "查找的位置不合法!" << endl;
} else {
cout << "第9个数是" << res << endl;
}
res = LocateElem(L, 1);
if (res == -1) {
cout << "元素未找到!" << endl;
} else {
cout << "元素所在位置为" << res << endl;
}
return 0;
}
short int ListInsert(SeqList &L, int pos, int elem) {
if (pos < 1 || pos > L.length + 1) {
return (short int) 1;
} else if (L.length >= InitSize) {
return (short int) 2;
}
for (int i = L.length; i >= pos; i--) {
L.data[i] = L.data[i - 1];
}
L.data[pos - 1] = elem;
L.length++;
return (short int) 3;
}
void initList(SeqList &L) {
L.length = 0;
}
void print(SeqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
short int ListDelete(SeqList &L,int pos, int &e) {
if (pos < 1 || pos > L.length) {
return (short int) 1;
} else if (L.length == 0) {
return (short int) 2;
}
e = L.data[pos - 1];
for (int i = pos; i < L.length; i++) {
L.data[i - 1] = L.data[i];
}
L.data[L.length - 1] = 0;
L.length--;
return (short int) 3;
}
int GetElem(SeqList L, int pos) {
if (pos < 1 || pos > L.length) return INT_MIN;
return L.data[pos - 1];
}
int LocateElem(SeqList L, int elem) {
int pos = -1;
for (int i = 0; i < L.length; i++) {
if (elem == L.data[i - 1]) {
pos = i;
break;
}
}
return pos;
}