| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 937 人关注过本帖
标题:[求助]那位大哥大嫂幫小弟解決一下簡單鏈表問題
只看楼主 加入收藏
C爷们
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-8-5
收藏
 问题点数:0 回复次数:12 
[求助]那位大哥大嫂幫小弟解決一下簡單鏈表問題
自己做的,總是不對,剛學結構体,做什麽錯什麽。
這個程序是創建一個動態鏈表,然後輸出。但只創建了兩個節點就結束循環了。我哪裏出錯了呢?
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
char num[11];
char name[20];
struct student *next;
};
void main()
{void print(struct student *head);
struct student *t1,*t2,*head;
head=t1=t2=NULL;
head=t2=t1=(struct student*) malloc(LEN);
scanf("%s,%s",head->num,head->name);
t1->num[10]='\0';
if (t1->num!="stop")
{
t1=t2->next=( struct student*) malloc(LEN);
scanf("%s,%s",t1->num,t1->name);
t1->num[10]='\0';
t2=t1;
}
else
t1->next=NULL;
print(head);
}

void print(struct student *head)
{
struct student *p;
printf("\nNow,these record are:\n");
p=head;
if (head!=NULL)
do
{printf("%s,%s",p->num,p->name);
p=p->next;
}
while (p!=NULL);
}
搜索更多相关主题的帖子: 解決 大嫂 
2007-08-05 11:03
viky2003
Rank: 5Rank: 5
等 级:职业侠客
帖 子:375
专家分:383
注 册:2007-4-11
收藏
得分:0 
void print(struct student *head);这函数应该放到main外面声明!!!错误1!!!



head=t2=t1=(struct student*) malloc(LEN);//三个指针指向同一个地址。。。。。
scanf("%s,%s",head->num,head->name);
t1->num[10]='\0';//这是什么阿??没有必要吧
if (t1->num!="stop")
{
t1=t2->next=( struct student*) malloc(LEN);
scanf("%s,%s",t1->num,t1->name);
t1->num[10]='\0';
t2=t1;
}
else
t1->next=NULL;
print(head);
}
程序没有可读性!!!创建链表方法很多很多!!
还是这样吧:
struct student *phead,*t1=NULL;
phead=(struct student *)malloc(LEN);
scanf("%s,%s",phead->num,phead->name);
phead->next=NULL;
while(phead->num!="stop")
{
t1=(struct student *)malloc(LEN); 只要首节点的num不是stop创建两个节点一个首节点一个t1尾节点。
scanf("%s,%s",t1->num,phead->name);
t1->next=NULL;
break;

}
phead->next=t1;
print(phead);

要练习算法就来http:///!!有挑战哦!!
2007-08-05 17:01
李天飞
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-8-2
收藏
得分:0 

感觉你思维混乱,其中有些过程是不必要的
创建连表 主要分两部分,第一部分是要建立头接点,第2是后面的其他节点,其他节点要在while里完成的
而且不要忘了给每个节点中的指针赋值,给最后一个指针赋空值,这样就行了 这就是思路了

2007-08-05 20:02
C爷们
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-8-5
收藏
得分:0 

好的,謝謝大哥啦,我在整理一下。

2007-08-05 20:44
crazyboy216
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-6-28
收藏
得分:0 
if (t1->num!="stop")
各位兄台,请问有上面的这种写法吗??例如数组也数组的比较应该用strcmp()吧
2007-08-06 12:53
viky2003
Rank: 5Rank: 5
等 级:职业侠客
帖 子:375
专家分:383
注 册:2007-4-11
收藏
得分:0 
恩!!!失误!!strcmp(t1-&gt;num,"stop");

要练习算法就来http:///!!有挑战哦!!
2007-08-06 14:22
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 
书上有例子的,先进先出和先进后出的都有
先看看吧

2007-08-06 15:40
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
回复:(viky2003)恩!!!失误!!strcmp(t1->nu...

大哥,能不能举例说一下strcmp(phead->num,"stop");
怎么用!?

2007-08-06 16:00
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
这个意思是说把字符stop复制给phead-&gt;num.

~~我的明天我知道~~
2007-08-06 16:22
viky2003
Rank: 5Rank: 5
等 级:职业侠客
帖 子:375
专家分:383
注 册:2007-4-11
收藏
得分:0 

int strcmp( const char *str1, const char *str2 );

功能:比较字符串str1 and str2, 返回值如下:

返回值
解释

less than 0
str1 is less than str2

equal to 0
str1 is equal to str2

greater than 0
str1 is greater than str2

例如:

printf( "Enter your name: " );
scanf( "%s", name );
if( strcmp( name, "Mary" ) == 0 )
printf( "Hello, Dr. Mary!\n" );


要练习算法就来http:///!!有挑战哦!!
2007-08-06 17:28
快速回复:[求助]那位大哥大嫂幫小弟解決一下簡單鏈表問題
数据加载中...
 
   



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

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