| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:[求助]关于双链表的建立与输出问题
只看楼主 加入收藏
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
结帖率:100%
收藏
 问题点数:0 回复次数:4 
[求助]关于双链表的建立与输出问题

今天刚学完那个双链表的有关知识,可遇到了一些问题。
我这个程序的要求事建一个双链表,头节点事空的。
建好后输出。
我的程序是这样的;
#include <stdio.h>
#include <malloc.h>

struct aa
{
int info;
struct aa *left,*right;
};
struct aa *creat()
{
return NULL;
}
struct aa *insert(struct aa *head,int x)
{
struct aa *p;
p=(struct aa *)malloc(sizeof(struct aa));
p->info=x;
if (!head)
{
p->right=NULL;
p->left=NULL;
}
else
{
head->left=p;
p->right=head;
p->left=NULL;
}
head=p;
return head;

}
void disp(struct aa *head1)
{
struct aa *p;
printf("\n");
p=head1;
if (!p)
printf("\n双链表是空的\n");
else
{
printf("\n双链表各个节点值为:\n");
while(p)
{
printf("%-5d",p->info);
p=p->right;
}
}
printf("\n");
}
void main()
{
struct aa *head,*head1,*p;
int n,m;
head=creat();
printf("please input the one date:\n");
scanf("%d",&n);
head1=insert(head,n);
printf("please input the date again:\n");
scanf("%d",&m);
puts("input the list end with '0':\n");
while (m)
{
p=(struct aa *)malloc(sizeof(struct aa));
p->info=m;
head1->right=p;
p->left=head1;
p->right=NULL;
scanf("%d",&m);
}
disp(head1);
}
程序应该是有问题的,希望各位给看看。

[此贴子已经被作者于2007-4-10 22:15:19编辑过]

搜索更多相关主题的帖子: 双链表 struct 输出 int head 
2007-04-10 22:14
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
struct aa *insert(struct aa *head,int x)
{
struct aa *p;
p=(struct aa *)malloc(sizeof(struct aa));
p->info=x;
if (!head)
{
p->right=NULL;
p->left=NULL;
head=p;
}
else
{
head->left=p;//感觉写反了.
p->right=head;
p->left=head->left;
}
return head;

}

倚天照海花无数,流水高山心自知。
2007-04-11 00:00
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
还是不行!
运行的结果总是只显示首尾两个节点,郁闷。
希望再看看。

~~我的明天我知道~~
2007-04-11 08:24
w362034710
Rank: 1
等 级:新手上路
帖 子:169
专家分:0
注 册:2006-12-2
收藏
得分:0 

#include <stdio.h>
#include <malloc.h>

struct aa
{
int info;
struct aa *left,*right;
};
struct aa *creat()
{
return NULL;
}
struct aa *insert(struct aa *head,int x)
{
struct aa *p;
p=(struct aa *)malloc(sizeof(struct aa));
p->info=x;
if (!head)
{
p->right=NULL;
p->left=NULL;
}
else
{
head->left=p;
p->right=head;
p->left=NULL;
head->right=NULL;
}
head=p;
return head;

}
void disp(struct aa *head1)
{
struct aa *p;
printf("\n");
p=head1;
if (!p)
printf("\n双链表是空的\n");
else
{
printf("\n双链表各个节点值为:\n");
while(p)
{
printf("%-5d",p->info);
p=p->right;
}
}
printf("\n");
}
void main()
{
struct aa *head,*head1,*head2,*p;
int n,m;
head=creat();
printf("please input the one date:\n");
scanf("%d",&n);
head1=insert(head,n);
head2=head1;
printf("please input the date again:\n");
scanf("%d",&m);
puts("input the list end with '0':\n");
while (m)
{
p=(struct aa *)malloc(sizeof(struct aa));
p->info=m;
head1->right=p;
p->left=head1;
p->right=NULL;
head1=p;
head1->left=NULL;
scanf("%d",&m);
}
disp(head2);
return;
}

2007-04-11 12:08
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
今天上机终于弄出来了
真好。

~~我的明天我知道~~
2007-04-11 12:18
快速回复:[求助]关于双链表的建立与输出问题
数据加载中...
 
   



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

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