#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 9
typedef char typeelem;
typedef int status;
typedef struct no
{
typeelem *elem;
int len;
} sqlist;
status initlist(sqlist &L);//構造一個空的線線性表操作
status shuzhi(sqlist &L);
status quezhi(sqlist &L, typeelem &e);
status bianli(sqlist L);
status listinsert(sqlist&L , typeelem &a);
//status clearlist(sqlist &L, typeelem &b);
status dstroylist(sqlist &L);
//status listempt(sqlist L);
int main()
{
sqlist L;
typeelem e;
typeelem a;
initlist(L);
shuzhi(L);
quezhi(L, e);
bianli(L);
cout << e << endl;
cout << "\b請輸入要插入的值a" << endl;
cin >> a;
listinsert(L, a);
bianli(L);
//clearlist(L, b);
dstroylist(L); //bianli(L);
return ok;
}
status initlist(sqlist &L)
{
L.elem = new typeelem[max];
if(!L.elem)
{
cout << "\b開劈失敗" << endl;
return error;
}
else
{L.len = max - 1;}
}
status shuzhi(sqlist &L)
{
int i;
for (i = 1; i<=L.len; i++)
{
cin >>L.elem[i];
}
return ok;
}
status quezhi(sqlist &L, typeelem &e)
{
int i;
cout << "\b請輸入要取出幾號元素0-(max-1)" << endl;
cin >> i;
e = L.elem[i];
return ok;
}
status bianli(sqlist L)
{
cout << "\b現在開始遍歷整個線性表" << endl;
int i;
if(!L.elem)
{
cout << "表空" << endl;
return error;
}
else
for (i = 1; i<=L.len; i++)
{
cout << L.elem[i] << endl;
}
return ok;
}
status listinsert(sqlist &L, typeelem &a)//插入元素的操作
{
int i;
int j;
cin >> i;
if(i<1||i>L.len+1)
{
cout << "\b輸入不存在" << endl;
return error;
}
cout << "\b請輸入要插入的元素" << endl;
for (j = L.len+1; j>i; j--)//原陣列空間分配了9個空間,只用了8個
L.elem[j] = L.elem[j-1];/*這里隊j可以理解為指標一樣的東西,假設我要在第6個陣列空間插入元素,需要把第6個空間置空,把6
以后空間的值依次往后移(包括6)*/
L.elem[i] = a;
L.len++;
return ok;
}
status dstroylist(sqlist &L)
{
if(L.elem)
{
delete[] (L.elem);
L.elem = NULL;
L.len = 0;
return ok;
}
else {
cout << "表不存在" << endl;
return error;
}
}
/*status clearlist(sqlist &L, typeelem &b)//清空順序表
{
L.elem = NULL;//原本開辟的是陣列空間,要清空表只需保留指標,使陣列空間為空
return ok;
}*/
求大神解答為什么destroylist函式中delete會報trace/breakpoint trap錯誤


uj5u.com熱心網友回復:
供參考:#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 9
typedef char typeelem;
typedef int status;
typedef struct no
{
typeelem *elem;
int len;
} sqlist;
status initlist(sqlist &L);//構造一個空的線線性表操作
status shuzhi(sqlist &L);
status quezhi(sqlist &L, typeelem &e);
status bianli(sqlist L);
status listinsert(sqlist&L , typeelem a);
//status clearlist(sqlist &L, typeelem &b);
status dstroylist(sqlist &L);
//status listempt(sqlist L);
int main()
{
sqlist L;
typeelem e;
typeelem a;
initlist(L);
shuzhi(L);
quezhi(L, e);
cout << e << endl;
bianli(L);
cout << "\b請輸入要插入的值a" << endl;
cin >> a;
listinsert(L, a);
bianli(L);
//clearlist(L, b);
dstroylist(L); //bianli(L);
return ok;
}
status initlist(sqlist &L)
{
L.elem = new typeelem[max];
if(!L.elem)
{
cout << "\b開劈失敗" << endl;
return error;
}
else
{L.len = max - 1;}
}
status shuzhi(sqlist &L)
{
int i;
for (i = 0; i<=L.len-1; i++)
{
cin >>L.elem[i];
}
return ok;
}
status quezhi(sqlist &L, typeelem &e)
{
int i;
cout << "\b請輸入要取出幾號元素1-" << L.len << endl;
cin >> i;
if(i<1||i>L.len)
{
cout << "\b輸入不存在" << endl;
return error;
}
e = L.elem[i-1];
return ok;
}
status bianli(sqlist L)
{
cout << "\b現在開始遍歷整個線性表" << endl;
int i;
if(!L.elem)
{
cout << "表空" << endl;
return error;
}
else
for (i = 0; i<=L.len-1; i++)
{
cout << L.elem[i] << endl;
}
return ok;
}
status listinsert(sqlist &L, typeelem a)//插入元素的操作
{
int i;
int j;
cout << "\b請輸入要插入的位置:" << endl;
cin.get();
cin >> i;
if(i<1||i>max)
{
cout << "\b輸入不存在" << endl;
return error;
}
for (j = L.len; j>i-1; j--)//原陣列空間分配了9個空間,只用了8個
L.elem[j] = L.elem[j-1];/*這里隊j可以理解為指標一樣的東西,假設我要在第6個陣列空間插入元素,需要把第6個空間置空,把6
以后空間的值依次往后移(包括6)*/
L.elem[i-1] = a;
L.len++;
return ok;
}
status dstroylist(sqlist &L)
{
if(L.elem)
{
delete[] (L.elem);
L.elem = NULL;
L.len = 0;
return ok;
}
else {
cout << "表不存在" << endl;
return error;
}
}
/*status clearlist(sqlist &L, typeelem &b)//清空順序表
{
L.elem = NULL;//原本開辟的是陣列空間,要清空表只需保留指標,使陣列空間為空
return ok;
}*/
uj5u.com熱心網友回復:
大佬,我想請教你兩個問題
1.為什么你的代碼中L.e lem(0)開始運行不會崩潰而我的從L. elem(1)開始會崩潰。
2. 在主函式有一個cin>>a, 在listinsert函式還有一個cin. get() ,這是為什么
uj5u.com熱心網友回復:
大佬,我想請教你兩個問題
1.為什么你的代碼中L.e lem(0)開始運行不會崩潰而我的從L. elem(1)開始會崩潰。
2. 在主函式有一個cin>>a, 在listinsert函式還有一個cin. get() ,這是為什么
uj5u.com熱心網友回復:
創建順序表,申請的 L.elem = new typeelem[max]陣列空間,陣列空間的下標都是從0開始的。然后后面對陣列的操作都是[1-8]之間,當執行到listinsert(sqlist &L, typeelem a)函式,
插入元素的操作時,for (j = L.len+1; j>i; j--) L.elem[j] = L.elem[j-1]; 這里j=L.len+1=8+1=9,陣列下標越界了。
cin.get(無引數):主要是用于舍棄輸入流中的不需要的字符,或者舍棄回車。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282937.html
標籤:C++ 語言
上一篇:求助C#將功能寫成類
