c++ 链表程序

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:11:21
#include<iostream.h>

struct Lnode{
double data;
Lnode* next;
};

void ShowList(Lnode* list){
if(list){
cout<<list->data<<endl;
if(list->next)
ShowList(list->next);
}
}

void AddToEnd(Lnode* new1,Lnode* h){
if(h==NULL){
h=new1;
new1->next=NULL;
}
else
AddToEnd(new1,h->next);
}

Lnode *GetNode(){
Lnode *item;
item=new Lnode;
if(item){
item->next=NULL;
item->data=0.0;
}
else
cout<<"Nothing allocated\n";
return item;
}

void main(){
Lnode * head=NULL;
Lnode * temp;
temp=GetNode();
while(temp){
cout<<"Data: ";
cin>>temp->data;
if(temp->data>0){
AddToEnd(temp,head);
}
else
break;
temp=GetNode();
}
Show

导致你首指针为空的原因就是AddToEnd函数中你把new1赋给了h,这里的h是形参,这样赋没用的,temp的地址并没有传给head。
你的void AddToEnd(Lnode* new1,Lnode* h)这个函数很有问题……再好好检查检查吧……