| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:大家帮我看看啊,链表问题,我没有积分,不好意思了
只看楼主 加入收藏
guoqingchun
Rank: 2
等 级:论坛游民
帖 子:30
专家分:68
注 册:2008-6-19
结帖率:100%
收藏
 问题点数:0 回复次数:3 
大家帮我看看啊,链表问题,我没有积分,不好意思了
我想实现建立一个单链表,然后将其输出,但是为什么每个结点都输出2遍啊
#include<stdio.h>
struct node{
 int data;
 struct node * next;   
};
struct node * createList(){
 struct node *head,*p,*r;
 int x;
 char ch;
 p=(struct node*)(malloc(sizeof(struct node)));
 p->next=NULL;//生成头结点  
 head=p;
 r=p;//建立单链表表尾指针  
 while(ch!='#')/为建立与否的标识符  
 {
  scanf("%d",&x);
  p=(struct node*)(malloc(sizeof(struct node)));
  p->data=x;
  p->next=NULL;//产生新结点
  r->next=p;//连接新结点  
  r=p;
  ch=getchar();
 }   
 return head;
}
int main()
{
 struct node * head,*p;
 head=createList();
 p=head->next;
 while(p->next!=NULL)
 {
  printf("%d->",p->data);
  p=p->next;   
 }
 printf("%d\n",p->data);
 system("pause");
 return 1;
}
 
搜索更多相关主题的帖子: 链表 积分 
2010-10-27 16:13
Cy_Chance
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2007-10-15
收藏
得分:0 
注意你的scanf() 和getchar()......,每次你输入结束符"#"的时候都多创建了一个冗余节点,而且这个节点都会等于上次的赋值
2010-10-27 17:33
xx342508809
Rank: 2
等 级:论坛游民
帖 子:89
专家分:51
注 册:2010-7-28
收藏
得分:0 
刚看完链表还不太懂了~~~printf("%d->",p->data);
这个地方
2010-10-27 19:53
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<malloc.h>//加个头结点
struct node{
int data;
struct node * next;  
};
struct node * createList(){
struct node *head,*p,*r;
int x;
char ch;
p=(struct node*)(malloc(sizeof(struct node)));
p->next=NULL;//生成头结点 
head=p;
r=p;//建立单链表表尾指针
ch=getchar(); 

while(ch!='#')//为建立与否的标识符 
{
    
  scanf("%d",&x);
   fflush(stdin);//清楚键盘缓存
  p=(struct node*)(malloc(sizeof(struct node)));
  p->data=x;
  p->next=NULL;//产生新结点
  r->next=p;//连接新结点 
  r=p;
  ch=getchar();

}  
return head;
}
int main()
{
struct node * head,*p;
head=createList();
p=head->next;
while(p->next!=NULL)
{
  printf("%2d",p->data);

 // printf("\n");
  p=p->next;  
}
printf("%2d\n",p->data);
//system("pause");
return 1;
}
2010-10-27 20:12
快速回复:大家帮我看看啊,链表问题,我没有积分,不好意思了
数据加载中...
 
   



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

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