| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:[求助]链表问题
只看楼主 加入收藏
wenyong
Rank: 1
等 级:新手上路
帖 子:251
专家分:0
注 册:2005-8-9
收藏
 问题点数:0 回复次数:3 
[求助]链表问题

#include <stdio.h> #define NULL 0 #define LEN sizeof(struct student) struct student { int num; struct student *next; }; struct student * input(struct student *head) { struct student *p1,*p2,*p; p1=p2=(struct student*)malloc(LEN); scanf("%d",&p1->num); head=NULL; while(p1->num>0) { if(head==NULL) head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf("%d",&p1->num); } p2->next=NULL; if(head!=NULL) p=head; do {printf("%d\n",p->num); p=p->next; }while(p!=NULL); return head ; }

seek(struct student *head) /*查找函数*/ { struct student *p; int k; printf("please input the seeking num:\n"); scanf("%d",&k); if(head!=NULL) p=head; do { if(p->num==k) printf("the num is %d\n",*p); else if(p==NULL) printf("don't exist:\n"); p=p->next; }while(p!=NULL); } main() { struct student *head=NULL; head=input(head); seek(head); getch(); } 这个链表在查询时 如果输入的数不存在就会 出错, 请问怎么改才正确呢 ?

[此贴子已经被作者于2005-9-27 17:38:09编辑过]

搜索更多相关主题的帖子: 链表 
2005-09-27 12:33
wenyong
Rank: 1
等 级:新手上路
帖 子:251
专家分:0
注 册:2005-8-9
收藏
得分:0 
  没人会吗 ?????

2005-09-28 08:52
独孤逍遥
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-25
收藏
得分:0 
怎么了啊?

物以方圆 义薄云天 何以载物 四海纵横
2005-09-28 09:17
jackrain
Rank: 1
等 级:新手上路
帖 子:182
专家分:0
注 册:2005-9-4
收藏
得分:0 
应该是if、else的设置的不好。
改成下面的试试
if(head!=NULL)
 {
  p=head;
  do
   {
     if(p-&gt;num==k)
    {
         printf("the num is %d\n",*p);
         break;
    }
     p=p-&gt;next;
   }while(p!=NULL);
  if(p == NULL)
    printf("don't exits\n");  
}
else
{
   printf("Invalid pointer\n");
  return;
}

程序和身体一样,健壮,再健壮! 我爱C++
2005-09-28 10:00
快速回复:[求助]链表问题
数据加载中...
 
   



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

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