Enndme· 2023-03-25 16:17 采纳率: 70%
浏览 8
已结题

关于线性表的运行问题

为什么运行到一半的时候就自动结束了呢


#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define MAXSIZE 100
typedef struct
{
    ElemType *elem;
    int length;
}SqList;
Status InitSqList(SqList &L)
{

    L.elem=new ElemType[MAXSIZE];
    if(L.elem=NULL)exit(OVERFLOW);
        L.length=0;
    return OK;

}
Status CreateSqList(SqList &L,int n)
{
    for(int i=0;i<n;i++)
        cin>>L.elem[i];
    L.length=n;
    return OK;
}
void TraverSqList(SqList L)
{

    cout<<"顺序表中的元素为:";
  for(int i=0;i<L.length;i++)
    cout<<L.elem[i]<<" ";
  cout<<endl;

}
Status LocateElem(SqList L,ElemType &e)
{

  for( int i=0;i<L.length;i++)
    if(L.elem[i]==e)
  return i+1;
  return 0;

}
Status InsertList(SqList &L,int i,ElemType e)
{
    if(i<1||i>L.length)
        return ERROR;
    if(i>L.length==MAXSIZE)
    return ERROR;
    for(int j=L.length-1;j>=i-1;j--)
    L.elem[j+1] =L.elem[j];
    L.elem[i-1]=e;
   ++L.length;
    return OK;

}
Status DeleteList(SqList &L,int i)
{

   if(i<1||i>L.length)
   return ERROR;
   for(int j=i;j<L.length-1;j++)
    L.elem[j-1]=L.elem[j];
  --L.length;
   return OK;

}
void menu()
{
    cout<<"        ************        "<<endl;
    cout<<"        0:退出              "<<endl;
    cout<<"        1:初始化            "<<endl;
    cout<<"        2:建表              "<<endl;
    cout<<"        3:遍历              "<<endl;
    cout<<"        4:按值查找元素      "<<endl;
    cout<<"        5:插入              "<<endl;
    cout<<"        6:删除              "<<endl;
    cout<<"        ***********         "<<endl;

}
int main()
{
    SqList L;
   int choose,i,e,n;
    menu();
    while(1)
    {
 cout<<"选择要执行的基本操作:";
 cin>>choose;
 switch(choose)
 {
 case 1:
  InitSqList(L);
  break;
 case 2:
  cout<<"输入表中元素个数:";
  cin>>n;
  CreateSqList(L,n);
  break;
 case 3: 
 TraverSqList(L);
  break;
 case 4:
  cout<<"输入要查找元素的值:"<<endl;
  cin>>e;
  if(LocateElem(L,e))
  cout<<"该元素的位置是第"<<LocateElem(L,e)<<"位"<<endl;
  else
  cout<<"该元素不存在!"<<endl;
  break;
 case 5:
  cout<<"输入要插入元素的位置和值:"<<endl;
  cin>>i>>e;
  if(InsertList(L,i,e)==OK)
  TraverSqList(L);
  else
  cout<<"不能插入!"<<endl;
  break;
 case 6:
  cout<<"输入要删除元素的位置:"<<endl;
  cin>>i;
  if(DeleteList(L,i)==OK)
  TraverSqList(L);
  else
  cout<<"删除位置不合法"<<endl;
  break;
 case 0:
 cout<<"操作结束"<<endl;
    exit(0);
  }
 }

    return OK;

}

img

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2023-03-25 17:01
    关注

    看到最后一行的return value了么?是个乱七八糟很大值,就是崩溃了
    19行,if(L.elem=NULL)
    你这里把L.elem设置为空了啊,应该是if(L.elem==NULL)啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月25日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改