数据结构 线性表 给线性表赋初始值,为什么我的代码在线性表的初始化时,输入数据个数后无法输入线性表的初始值
#include<iostream>
#define elementype int
#define MAXSIZE 5
using namespace std;
struct list { //线性表的建立
elementype* elem;
int length;
};
bool initlist(list& L) { //线性表的初始化
L.elem = new elementype[MAXSIZE];
int a;
cout << "请输入数据个数" << endl;
cin >> a;
cout << "请输入线性表初始值" << endl;
if (a > 0 && a < MAXSIZE) {
for (int i = 0; i < a ; i++) {
cin>>L.elem[i];
}
}
else {
cout << "数据个数错误!";
return 0;
}
if (!L.elem) {
cout << "error" << endl;
return 0;
}
L.length = a;
cout << "线性表初始化成功!" << endl;
return a;
}
int locatelist(const list& L, const elementype& m) {//查找线性表元素
int i;
for (i = 0; i < L.length; i++) {
if (L.elem[i] == m) {
return i + 1;
}
}
return 0;
}
bool insertlist(list& L, const elementype& m, const int i) //线性表的插入
{
if (i<0 || i>L.length) {
cout << "插入位置错误" << endl;
return false;
if (L.length == MAXSIZE) {
cout << "线性表已满" << endl;
return false;
}
if (L.length > 0) {
int j;
for (j = L.length - 1; j >= i; j--) {
L.elem[j + 1] = L.elem[j];
}
L.elem[j] = m;
L.length = L.length + 1;
return true;
}
}
}
bool destorylist(list& L) { //线性表的销毁
if(L.elem){
delete L.elem;
}
return true;
}
bool easelist(list& L, const int i) { //线性表的删除
if (L.length == 0) {
cout << "线性表为空" << endl;
return false;
}
if (i<0 || i>L.length) {
cout << "删除位置错误" << endl;
return false;
}
for (int j = i; j < L.length - 1; j++) {
L.elem[j] = L.elem[j + 1];
}
L.length -= 1;
return true;
}
int main() {
struct list q;
initlist(q);
int n= initlist(q);
//if(initlist(q))
//cout << "原始线性表为:" << endl;
//for (int z = 0; z < n ; z++) {
// cout<<q.elem[z];
//}
elementype a;
cout << "请输入你想查找的元素" << endl;
cin >> a;
if (locatelist(q, a)) {
cout << "查找成功,它的位置在下标" << locatelist(q, a) << endl;
}
else cout << "查找失败" << endl;
int m;
elementype b;
cout << "请输入你想插入的元素" << endl;
cin >> b;
cout << "请输入你想插入的位置" << endl;
cin >> m;
if (insertlist(q, m, b)) {
cout << "插入成功" << endl;
}
else cout << "插入失败" << endl ;
int c;
cout << "请输入你想删除元素的位置" << endl;
cin >> c;
if (easelist(q, c)) {
cout << "删除成功" << endl;
}
else cout << "删除失败" << endl;
return 0;
}