出问题的函数:(调用时不报错,程序运行时自动终止)
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;
}