帮忙调试一下:

point_to 2002-12-23 10:07:46
不要只指出几个错误的解答,只要最终的符合本来思想的代码。谢谢!!


#include "stdafx.h"
#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 *head)
{
if(head==NULL)
{
head=new1;
new1->next=NULL;
}
else
AddToEnd(new1, head->next);
}
Lnode *GetNode()
{
Lnode * item;
item=new Lnode;
if(item)
{
item->next=NULL;
item->data=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();
}
ShowList(head);
}
...全文
46 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rushman 2002-12-23
  • 打赏
  • 举报
回复
完整的,测试过的程序:
//用BCB测试,所以前一个include 注释掉了
//#include "stdafx.h"
#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 * *head)//用指针的地址调用
{
if((*head)==NULL)//相应的改一下
{
*head=new1;//源程序中赋值的对象是栈中的临时变量,
//调用函数中的值没有被修改
new1->next=NULL;
}
else
AddToEnd(new1, &((*head)->next));//改一下
}

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

void main()
{
Lnode *head=NULL;//链首
Lnode *temp;
char ch;//添加一个临时变量
temp=GetNode();
while (temp)
{
cout<<"data:";
cin>>temp->data;
//需要将结束字符清掉
cin.clear();
do{
cin.get(ch);
}while(ch > 10);//---
if(temp->data>0)
AddToEnd(temp,&head); //用指针的地址调用
else
break;
temp=GetNode();
}
ShowList(head);
}
rushman 2002-12-23
  • 打赏
  • 举报
回复
还有一点
#include "stdafx.h"
#include "iostream.h"
这里头文件一般是用
#include <stdafx.h>
#inlcude <iostream.h>
如果环境特殊也可以用引号。
rushman 2002-12-23
  • 打赏
  • 举报
回复
把下面两个函数改写一下:
//void AddToEnd(Lnode *new1,Lnode *head)
void AddToEnd(Lnode *new1,Lnode * *head)//用指针的地址调用
{
if(head==NULL)
{
*head=new1;//源程序中赋值的对象是栈中的临时变量,
//调用函数中的值没有被修改
new1->next=NULL;
}
else
AddToEnd(new1, head->next);
}

void main()
{
Lnode *head=NULL;//链首
Lnode *temp;
char ch;//添加一个临时变量
temp=GetNode();
while (temp)
{
cout<<"data:";
cin>>temp->data;
//需要将结束字符清掉
cin.clear()
do{
cin.get(ch);
}while(ch > 10);//---
if(temp->data>0)
AddToEnd(temp,&head); //用指针的地址调用
else
break;
temp=GetNode();
}
ShowList(head);
}
point_to 2002-12-23
  • 打赏
  • 举报
回复
#include "stdafx.h"
#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 *head)
{
if(head==NULL)
{
head=new1;
new1->next=NULL;
}
else
AddToEnd(new1, head->next);
}
Lnode *GetNode()
{
Lnode * item;
item=new Lnode;
if(item)
{
item->next=NULL;
item->data=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();
}
ShowList(head);
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧