| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 717 人关注过本帖
标题:如何查找结点?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:9 
如何查找结点?
#include "stdafx.h"

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *next;
};

int main(int argc, char* argv[])
{
    struct student a,b,c,*head,*p;//如何查找b的前一个结点a,b的后一个结点c;按照下标值查找
    a.num=99101;a.score=89.5;
    b.num=99103;b.score=90;
    c.num=99105;c.score=91.5;
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
    return 0;
}
搜索更多相关主题的帖子: 查找 
2009-08-21 09:16
龙心
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:14
专家分:159
注 册:2009-8-20
收藏
得分:3 
q=p->next.//另起指针q
while(q!=NULL&&q!=&b)
{  
   p=q;
   q=q->next;
 }
 if(q==NULL)
{
   return 0;
}
else
{
   return p;
}
2009-08-21 09:42
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
这样还是运行不了?是不是代码我写错了?
#include "stdafx.h"#include "stdio.h"#include "string.h"#include "stdlib.h"#include "malloc.h"#define NULL 0#define LEN sizeof(struct student)struct student {    long num;    float score;    struct student *next;};int main(int argc, char* argv[]){    struct student a,b,c,*head,*p,*q;    a.num=99101;a.score=89.5;    b.num=99103;b.score=90;    c.num=99105;c.score=91.5;    head=&a;    a.next=&b;    b.next=&c;    c.next=NULL;    p=head;    do    {        printf("%ld%5.1f\n",p->num,p->score);        p=p->next;    }while(p!=NULL);    q=p->next.    while(q!=NULL&&q!=&b)    {          p=q;        q=q->next;    }    if(q==NULL)    {         return 0;    }    else    {            return p;    }     return 0;}
2009-08-21 10:00
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *next;
};

int main(int argc, char* argv[])
{
    struct student a,b,c,*head,*p,*q;
    a.num=99101;a.score=89.5;
    b.num=99103;b.score=90;
    c.num=99105;c.score=91.5;
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);

    q=p->next.
    while(q!=NULL&&q!=&b)
    {  
        p=q;
        q=q->next;
    }
    if(q==NULL)
    {
         return 0;
    }
    else
    {   
        return p;
    }
    return 0;
}
2009-08-21 10:00
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
程序还是运行不了
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *next;
};

int main(int argc, char* argv[])
{
    struct student a,b,c,*head,*p,*q;
    a.num=99101;a.score=89.5;
    b.num=99103;b.score=90;
    c.num=99105;c.score=91.5;
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do
    {
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);

    q=p->next;
    while(q!=NULL&&q!=&b)
    {  
        p=q;
        q=q->next;
    }
    if(q==NULL)
    {
         return 0;
    }
    else
    {   
         return 1;
    }
    return 0;
}
2009-08-21 10:17
soler
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:181
专家分:1077
注 册:2005-7-16
收藏
得分:3 
程序代码:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *next;
};

int main(int argc, char* argv[])
{
    struct student a,b,c,*head,*p,*q;
    a.num=99101;a.score=89.5;
    b.num=99103;b.score=90;
    c.num=99105;c.score=91.5;
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;//当前结点为p,前一个结点为q;当前结点指向头结点。
    do
    {

        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
/*************************************** 
    q=p->next.
    while(q!=NULL&&q!=&b)
    {
        p=q;
        q=q->next;
    }  /*这个语句是用来查找当前结点是否为空或者为b结点,
       如果不是,自动指向下一个,直到找到位置。/*
    if(q==NULL)
    {
         return 0;
    }
    else
    {
        return p;
    }
********************************************/
    return 0;
}
2009-08-21 13:25
Holy_Bitch
Rank: 2
等 级:论坛游民
帖 子:6
专家分:41
注 册:2009-8-19
收藏
得分:3 
这样当然有问题啊,你的指针p从显示循环中出来后,p已经为空啦,怎么能就直接
q=p->next呢
2009-08-21 13:52
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
那该如何修改代码呢?
2009-08-21 15:29
soler
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:181
专家分:1077
注 册:2005-7-16
收藏
得分:0 
对于你的几个结点,,假设这些结点是按照这个顺序链接在一起:a--->b--->c--->NULL  
 a.num=99101;a.score=89.5;
 b.num=99103;b.score=90;
 c.num=99105;c.score=91.5;
如果你要查找b结点。你可以通过b.num或者b.score
你可以这样写
struct student *head,*p,*q;//head头结点,p为前一个结点,q为当前节点
head = &a;
q = head;//当前结点指向头结点。
while(q != NULL && q->num != 99103)//当前结点的num是否等于你要找的b.num,如果不等,当前结点指向下一个结点。
{
    p = q;
    q = q->next;
}                                  //此处循环结束时,如果找到b结点,p为&b,如果找不到结点,p为NULL
if(q != NULL)
    printf("%ld%5.1f\n",q->num,q->score);
else
   printf("Cannot find the node!\n");
你的代码不知道要表示什么意思,所以不好帮你修改,
你的代码只不过是copy过来的,别人是以调用函数形式写的。。。
先去好好看看书吧。。。

2009-08-21 17:35
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
这样做还是不行,只能够查找出一个结点,有没有其他的做法?
2009-08-24 15:01
快速回复:如何查找结点?
数据加载中...
 
   



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

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