c++线性顺序表的错误

cj云 2021-05-05 08:30:04
#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;  }*/ 这里的destroy 函数操作中delete显示trace/breakpoint trap错误,希望有大神解答
...全文
91 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cj云 2021-05-05
  • 打赏
  • 举报
回复
引用 1 楼 focuslight的回复:
可能空间开的max和使用的不匹配(注意使用时不要越界),就会出现未知错误,,或者是 Trace/breakpoint trap 异常
我用断点检查一下发现运行到destroy函数时的L. elem 的地址与刚开始开辟的地址不同,这怎么回事
Isnis-fallen 2021-05-05
  • 打赏
  • 举报
回复
可能空间开的max和使用的不匹配(注意使用时不要越界),就会出现未知错误,,或者是 Trace/breakpoint trap 异常

64,678

社区成员

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

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