刚刚学数据结构,为何程序老是停止运行
刚学数据结构,为何程序老是停止运行
最近在专研数据结构,为什么我写的程序一遇到判定是否是NULL的就停止运行了?我用的是visual studio 2013 express
比如以下这个打印链表List中所有元素的函数,老是一运行就显示“程序停止运行”,何解?
void PrintList(List L){
Node p = L->Next;
if (L->Next == NULL){
printf("此表为空表\n");
return;
}
while (p!=NULL)
{
printf("第%个元素为%\n", ++i, p->Element);
p = p->Next;
}
}
------解决方案--------------------
L和L->next是不一样的。
L如果为空,L->next就是非法操作,程序就会终止。
------解决方案--------------------
我觉得你是不是应该检查下你定义的结构体,和你传到这里void PrintList(List L)的参数之类的有没有问题,
printf("第%个元素为%\n", ++i, p->Element); 这个printf 编译过了?
------解决方案--------------------
最近在专研数据结构,为什么我写的程序一遇到判定是否是NULL的就停止运行了?我用的是visual studio 2013 express
比如以下这个打印链表List中所有元素的函数,老是一运行就显示“程序停止运行”,何解?
void PrintList(List L){
Node p = L->Next;
if (L->Next == NULL){
printf("此表为空表\n");
return;
}
while (p!=NULL)
{
printf("第%个元素为%\n", ++i, p->Element);
p = p->Next;
}
}
------解决方案--------------------
L和L->next是不一样的。
L如果为空,L->next就是非法操作,程序就会终止。
------解决方案--------------------
我觉得你是不是应该检查下你定义的结构体,和你传到这里void PrintList(List L)的参数之类的有没有问题,
printf("第%个元素为%\n", ++i, p->Element); 这个printf 编译过了?
------解决方案--------------------
Node *head;
Node *create()//建立链表,这是第一步;
{
Node *p,*q;
head=NULL;
int temp;
cout<<"Now create the list,Input the data,end by -1:"<<endl;
cin>>temp;
while(temp!=-1)
{
p=new Node;
p->Element=temp;
if(head==NULL)
head=p;
else
{
q->Next=p;
}
q=p;
cin>>temp;
}
if(head!=NULL)
q->Next=NULL;
return head;
}
void PrintList(List L){
int i = 0;
Node *p = L;
if (p == NULL){
printf("此表为空表\n");
return;
}
while (p!=NULL)
{
printf("第%d个元素为%d\n", ++i, p->Element);
p = p->Next;
}
}