在递增的单链表中把一个数字插入到适当的位置 程序运行时出现下图这种情况搞不清是程序问题还是软件问题
#include<iostream>#include<stdlib.h>
using namespace std;
int n;
typedef struct node
{ int data;
struct node *next;
}node,*list;
void createlist(list *L)
{*L=(list)malloc(sizeof(node));
(*L)->next=NULL;
cout<<"please enter element's number:"<<endl;
list p,r;
r=*L;
cin>>n;
cout<<"enter data:"<<endl;
for(int i=0;i<n;i++)
{p=(list)malloc(sizeof(node));
cin>>p->data;
r->next=p;
r=p;
}
r->next=NULL;
}
void outputlist(list *L)
{
cout<<"your list is:"<<endl;
for(int i=0;i<n;i++)
{cout<<(*L)->next->data<<"\t";
*L=(*L)->next;
}
}
void insertlist(list *L)
{int i;
list q;
cout<<"enter will insert's data:"<<endl;
cin>>i;
while(i>(*L)->next->data)
*L=(*L)->next;
if(*L)
{q=(list)malloc(sizeof(node));
q->data=i;
q->next=(*L)->next;
(*L)->next=q;}
else
{*L=(*L)->next;
q=(list)malloc(sizeof(node));
q->next=(*L)->next;
(*L)->next=q;
}
n=n+1;
cout<<"your list is:"<<endl;
for(int j=0;j<n;j++)
{cout<<(*L)->next->data<<"\t";
*L=(*L)->next;
}
}
int main()
{list L1;
createlist(&L1);
outputlist(&L1);
insertlist(&L1);
return 0;
}