落眸 2021-10-11 22:39 采纳率: 100%
浏览 223
已结题

Process exited after 25.09 seconds with return value 3221225477,在调用这个函数时程序运行自动终止。

出问题的函数:(调用时不报错,程序运行时自动终止)

void locationdelete(int b){
     struct stu *p,*q;
     q=(struct stu*)malloc(sizeof(struct stu));
     p=head;
     if(b>0&&b<(length())){
    for(int i=1;i<b;i++){
      p=p->next;    
    }
    q=p->next;
    p->next=q->next;
    free(q);
     }
     else
    cout<<"不存在"<<endl; 
}

全部源代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std; 
typedef int INT;
struct stu{
    
    INT x;
    struct stu* next;    
};
struct stu *head;
void createlist(int n){
    struct stu *p1,*p2;
        head=(struct stu*)malloc(sizeof(struct stu));
        head->x=n;
            cout<<"输入x:"<<endl; 
    for(int i=1;i<=n;i++){
        p1=(struct stu*)malloc(sizeof(struct stu));
        cin>>p1->x;
        if(i==1)
        head->next=p1;
        else
        p2->next=p1;
        p2=p1;
    } 
    p2->next=NULL;
    if(head==NULL){
        cout<<"创建失败!"<<endl; 
    }
} 
void show(){
    stu * p;
        p=head;
    while(p->next!=NULL){
    
        p=p->next;
        cout<<p->x<<", "<<endl;
    }
}
void destory(){
    while(head!=NULL){
    struct stu *p;
    p=head;
    head=head->next;
    free(p);
    }
}
int clear(){
     struct stu *p;
     struct stu *q;
         p=head->next;
         if(head==NULL)
         return 0;
         
     while(p!=NULL){
         q=p->next;
         free(p);
         p=q;
     }
     return 1;
}
void judge(){
    if(head->next==NULL){
        cout<<"空表"<<endl; 
    }
    else
    cout<<"线性表非空"<<endl; 
}
int length(){
    struct stu *p;
    int count=1;
     p=head->next;
while(p->next!=NULL){
  p=p->next;
    count++;    
}
if(head->next=NULL)    
  count--;
  return count;
}
void getelement(int b){
    int count=1;
    stu *p;
    p=head->next;
    while(count!=b){
        p=p->next;
        count++;
    }
    cout<<p->x<<endl;
}
void getlocation(int b){
    int count=1;    
    stu *p;
    p=head->next;
    while(p->x!=b&&p->next!=NULL){
        p=p->next;
        count++;    
        }    
if(p->x==b){
cout<<count<<endl;
}    
else        
cout<<"不存在"<<endl;
}
void lastone(int b){
  struct stu *p;
  p=head->next;
  while(p->next&&p->next->x!=b){
      p=p->next;
  }
  if(p->next){
      cout<<p->x<<endl;
  }
  else 
  cout<<"不存在"<<endl; 
}
void nextone(int b){
  struct stu *p;
  p=head->next;
  while(p->next&&p->x!=b){
      p=p->next;
  }
  if(p->next){
      cout<<p->next->x<<endl;
  }
  else 
  cout<<"不存在"<<endl; 
}
void locationinsert(int b,int c){
    struct stu *p,*q;
    q=(struct stu*)malloc(sizeof(struct stu));
    q->x=c;
    p=head;
    if(b>0&&b<=(length())){
       for(int i=1;i<b;i++){
    p=p->next;    
    }
    q->next=p->next;
    p->next=q;
    }
    else
    cout<<"不存在"<<endl; 
} 
void locationdelete(int b){
     struct stu *p,*q;
     q=(struct stu*)malloc(sizeof(struct stu));
     p=head;
     if(b>0&&b<(length())){
    for(int i=1;i<b;i++){
      p=p->next;    
    }
    q=p->next;
    p->next=q->next;
    free(q);
     }
     else
    cout<<"不存在"<<endl; 
}
int main(int argc, char** argv) {
    cout<<"1----初始化一个线性表"<<endl;
    cout<<"2----销毁线性表"<<endl;
    cout<<"3----清空线性表"<<endl;
    cout<<"4----判断线性表是否为空"<<endl;
    cout<<"5----求线性表长度"<<endl;
    cout<<"6----获取线性表中指定位置的元素"<<endl;
    cout<<"7----获取线性表中元素的位置"<<endl;
    cout<<"8----求前驱"<<endl;
    cout<<"9----求后继"<<endl;
    cout<<"10----在线性表指定位置插入元素"<<endl;
    cout<<"11----删除线性表指定位置的元素"<<endl;
    cout<<"12----显示线性表"<<endl;
    cout<<"13----合并两个非递减有序线性表"<<endl;
    cout<<"退出,输入一个负数"<<endl; 
    int a=0;
    while(a>=0){
            cout<<"请输入操作代码:"<<endl;
            cin>>a;
    if(a==1) {
        int n;
            cout<<"请输入链表长度:"<<endl; 
            cin>>n; 
            createlist(n);
    }
    if(a==2){
        destory();
    }        
    if(a==3){
        clear();
   
    }    
     if(a==4) {
         judge();
     }
     if(a==5){
     int count=length();
          cout<<"线性表的长度为:"<<count<<endl; 
     }
     if(a==6){
         int  b;
         cout<<"请输入获取元素位置:"<<endl; 
         cin>>b;
         getelement(b);
     } 
     if(a==7){
         int  b;
         cout<<"请输入获取位置元素:"<<endl; 
         cin>>b;
         getlocation(b);
     }
     if(a==8){
         int b;
         cout<<"请输入元素:"<<endl; 
         cin>>b;
        lastone(b);
     }
     if(a==9){
             int b;
         cout<<"请输入元素:"<<endl; 
         cin>>b;
         nextone(b);
     }
     if(a==10){
         int b,c;
         cout<<"请输入位置:"<<endl; 
         cin>>b;
         cout<<"请输入元素:"<<endl;
         cin>>c; 
         locationinsert(b,c);
     }
     if(a==11){
         int b,c;
         cout<<"请输入位置:"<<endl; 
         cin>>b;
         locationdelete(b);
     } 
    if(a==12){
            show();    
    }
    
    
    }
    return 0;
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-10-11 22:41
    关注

    第78行有明显的问题,把head->next设置为空了啊。肯定不能是赋值语句啊。应该是==吧

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

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 创建了问题 10月11日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥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之后自动重连失效