这段代码运行到添加第二个结点data的时候会出现某某内存不能为written异常强制退出 new这一块出有关问题了 大神们 帮帮新手初学者 感激不尽

这段代码运行到添加第二个结点data的时候会出现某某内存不能为written错误强制退出 new这一块出问题了 大神们 帮帮新手菜鸟 感激不尽
#include<iostream>
#include<ctype.h>
#include<string>
using namespace std;
struct node
{
int number;
node *next;
};
node *sort();
node *insert(node *hd,node *b);
void show(node *hd);
int main()
{
node *hd;
hd=sort();
show(hd);
return 0;
}
node *sort()
{
node *head=0,*b;
int num;
cout<<"请依次输入您要排序的数字,输入-1结束\n";
cin>>num;
while(num!=-1)
{
b=new node;
b->number=num;
head=insert(head,b);
cout<<"请依次输入您要排序的数字,输入0结束\n";
cin>>num;
}
return head;
}
void show(node *hd)
{
node *p;
p=hd;
while(p)
{
cout<<p->number<<endl;
p=p->next;
}
}
node *insert(node *hd,node *b)
{
node *p,*q;
if(hd==0)
{
hd=b;
b->next=0;
return hd;
}
if(hd->number>b->number)
{
b->next=hd;
hd=b;
return hd;
}
p=hd;
while(p->number>=b->number&&p->next)
{
q=p;
p=p->next;
}
if(p->number<b->number)
{
q->next=b;
b->next=p;
}
else
{
q->next=b;
b->next=0;
}
return hd;
}

------解决方案--------------------
#include <stdlib.h>
#include<iostream>
#include<ctype.h>
#include<string>
using namespace std;
struct node
{
int number;
node *next;
};
node *sort();
node *insert(node *hd,node *b);
void show(node *hd);
int main()
{
node *hd;
hd=sort();
show(hd);
system("pause");
return 0;
}
node *sort()
{
node *head=0,*b;
int num;
cout<<"请依次输入您要排序的数字,输入-1结束\n";
cin>>num;
while(num!=-1)
{
b=new node;
b->number=num;
head=insert(head,b);
cout<<"请依次输入您要排序的数字,输入-1结束\n";
cin>>num;
}
return head;
}
void show(node *hd)
{
node *p;
p=hd;
while(p)
{
cout<<p->number<<endl;
p=p->next;
}
}
node *insert(node *hd,node *b)
{
node *p,*q;
if(hd==0)//如果链表为空
{
hd=b;
b->next=0;
return hd;
}
if(hd->number>b->number)//升序排列
{
b->next=hd;
hd=b;
return hd;
}

//如果新插入的数比头节点大
p=hd;
/*
while(p->number>=b->number && p->next)
{
q=p;
p=p->next;
}
if(p->number<b->number)
{
q->next=b;
b->next=p;
}
else
{
q->next=b;
b->next=0;
}
*/
while(p != 0 && p->number < b->number)
{
if(p->next == 0)
{
p->next = b;
b->next = 0;
break;
}
else if(p->next->number > b->number)
{
b->next = p->next;
p->next = b;
break;
}
else
{
p = p->next;
}

}
return hd;
}

------解决方案--------------------
你的insert接口有问题
第一个if分支是接收第一个数
第二个if分支处理比头节点小的数
接下就要处理比头节点大的情况了
首先你的while条件已经是错的,应该判断比头大的情况,里面的内容也有问题,这样会把整个链表搞空的