| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:关于链表输出
只看楼主 加入收藏
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
结帖率:95%
收藏
 问题点数:0 回复次数:7 
关于链表输出
#include "stdio.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
 int score;
 struct student *next;
};
struct student listA,listB;
int n,sum=0;

struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
printf("Input 0 to end.\n");
printf("Input number,score:\n");
p1=p2=(struct student *)malloc(LEN);
scanf("%d,%d",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{if(n==0)head=p1;
 else p2->next=p1;
 p2=p1;
 n++;
 p1=(struct student *)malloc(LEN);
 scanf("%ld,%d",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

struct student *insert(struct student *ah,struct student *bh)
{
}

void print(struct student *head)
{struct student *p;
 printf("\nThere are %d records:\n",sum);
 p=head;
 if(p!=NULL)
  do
  {printf("%ld%5d\n",p->num,p->score);
   p=p->next;
  }
  while(p!=NULL);
}
main()
{struct student *ahead,*bhead,*abh;
 printf("\nInput list a:\n");
 ahead=creat();
 sum=sum+n;
printf("\nInput list b:\n");
 bhead=creat();
 sum=sum+n;
abh=insert(ahead,bhead);
 print(ahead);
    getch();
}
为什么sum输出还为0;还有第一条记录输出老是出错,帮忙看看,谢谢。
搜索更多相关主题的帖子: 输出 链表 
2010-02-02 04:27
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
  把你的程序放到VC++环境下运行  把我VC++都卡了    关都关不掉    还得强制结束任务
呵呵  我也就前天刚看完链表   
首先  我看到你这程序最大的疑惑是         
struct student *insert(struct student *ah,struct student *bh)
{
}
这个insert函数声明了要返回struct student *类型的值 你这个函数却空在这  啥都没 不知道其作用
2010-02-02 17:24
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:0 
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;                        <-------------这里的n不能再定义了,否则局部变量n屏蔽了全局变量n.所以sum仍为0.
printf("Input 0 to end.\n");
printf("Input number,score:\n");
p1=p2=(struct student *)malloc(LEN);
scanf("%d,%d",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{if(n==0)head=p1;
else p2->next=p1;
p2=p1;
n++;
p1=(struct student *)malloc(LEN);
scanf("%ld,%d",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

2010-02-02 17:33
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
  对对                 前面也应该加个头文件#include "malloc.h"                  
2010-02-02 17:51
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
可是运行了下
图片附件: 游客没有浏览图片的权限,请 登录注册
那输出的num后面的二进制是怎么回事  地址吗
2010-02-02 18:00
windflyer
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-10-17
收藏
得分:0 
abh=insert(ahead,bhead);
print(ahead);

为什么不输出abh?
2010-02-02 19:06
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:0 
struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
printf("Input 0 to end.\n");
printf("Input number,score:\n");
p1=p2=(struct student *)malloc(LEN);
scanf(" %ld ,%d",&p1->num,&p1->score);    //将%d-->%ld, 另外:输入的时候一定不要忘记输入逗号。其他没有错误,修改之后我运行是没有问题的。
。。。
2010-02-02 20:28
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
  我把,号忘了     学了后面忘前面
2010-02-03 12:18
快速回复:关于链表输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012931 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved