大神救救c++

cj云 2021-05-05 08:41:12
#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错误
...全文
111 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzjhjxj 2021-05-05
  • 打赏
  • 举报
回复
创建顺序表,申请的 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(无参数):主要是用于舍弃输入流中的不需要的字符,或者舍弃回车。
cj云 2021-05-05
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj的回复:
供参考:
#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; 
}*/
大佬,我想请教你两个问题 1.为什么你的代码中L.e lem(0)开始运行不会崩溃而我的从L. elem(1)开始会崩溃。 2. 在主函数有一个cin>>a, 在listinsert函数还有一个cin. get() ,这是为什么
cj云 2021-05-05
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj的回复:
供参考:
#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; 
}*/
大佬,我想请教你两个问题 1.为什么你的代码中L.e lem(0)开始运行不会崩溃而我的从L. elem(1)开始会崩溃。 2. 在主函数有一个cin>>a, 在listinsert函数还有一个cin. get() ,这是为什么
qzjhjxj 2021-05-05
  • 打赏
  • 举报
回复
供参考:
#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; 
}*/

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧