| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 832 人关注过本帖
标题:链表的查找操作问题
只看楼主 加入收藏
niu91
Rank: 2
等 级:论坛游民
帖 子:64
专家分:44
注 册:2009-7-25
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:6 
链表的查找操作问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NULL 0
#define LEN sizeof(struct student)

//定义节点结构
struct student
{
    char no[7];
    int score;
    struct student *next;
};

//create()函数:创建一个具有头结点的单链表
//返回值:返回单链表的头指针

struct student *create(void)
{
    struct student *head=NULL,*p1,*p2=NULL;
    int count=0;
    while(1)
    {
        p1=(struct student *)malloc(LEN);

        printf("编号%d:",count+1);
        scanf("%6s",p1->no);
        if(strcmp(p1->no,"000000")==0)
        {
            free(p1);
            break;
        }

        printf("成绩%d:",count+1);
        
        scanf("%d",&p1->score);
        count++;

        p1->next=NULL;

        if(count==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
    }
    return(head);
}
void print(struct student *head)
{
    struct student *p;
    printf("学生们的成绩是:\n");
    p=head;
    while(p!=NULL)
    {
        printf("%s,%d\n",p->no,p->score);
        p=p->next;
    }
}
struct student *FindNode(struct student *head,int i)//依节点查找
{
    struct student *p=head;
    int j=1;
    while(p->next!=NULL&&i>j)
    {
        p=p->next;
        j++;
    }
    if(i==j)
        return p;
    else
        return NULL;
}
struct student *Find(struct student *head,char *key)//依值查找
{
    struct student *p=head;
    while(p!=NULL)
        if(strcmp(p->no,key)!=0)
            p=p->next;
        else
            break;
    return p;
}
void main()
{
    int *ch;
    struct student *pt;
    pt=create();


    print(pt);    
    scanf("%s",ch);
    
    pt=Find(pt,ch);
    printf("%s,%d\n",pt->no,pt->score);

}





//求解 ,运行的 时候,出现了错误
//是什么原因呢
2011-10-11 20:26
鸿飞冥冥
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:48
专家分:124
注 册:2011-8-14
收藏
得分:1 
主函数里面的*ch类型应该定义成char,,而且你写的*Find函数有问题,,输入学号查找不了,,应该是那个字符串指针传来传去的没按定义的用,,水平有限,,坐等大牛

Sickcat
2011-10-11 22:13
不是不可能
Rank: 2
等 级:论坛游民
帖 子:20
专家分:23
注 册:2011-9-1
收藏
得分:2 
创建链表函数最后要加p2->next=NULL;当年输入p1->no="00000"时,创建链表结束,此时p2为尾节点,它应指向为空,若不然,链表不知道如何结束
2011-10-11 22:46
lansane
Rank: 2
等 级:论坛游民
帖 子:17
专家分:58
注 册:2011-5-19
收藏
得分:2 
把主函数的ch类型改了,另外 pt=Find(pt,ch);当ch不存在于链表中的时候,返回的是NULL指针,这样你接下来打印是肯定错的,
if(pt==NULL) printf("不存在");
else
printf("%s,%d\n",pt->no,pt->score);

2011-10-11 22:59
lansane
Rank: 2
等 级:论坛游民
帖 子:17
专家分:58
注 册:2011-5-19
收藏
得分:5 
以下是引用不是不可能在2011-10-11 22:46:33的发言:

创建链表函数最后要加p2->next=NULL;当年输入p1->no="00000"时,创建链表结束,此时p2为尾节点,它应指向为空,若不然,链表不知道如何结束
p1->next=NULL;

        if(count==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
每次创建一个节点的时候,next指针都被给了NULL值,所以链表是知道结尾的
2011-10-11 23:03
niu91
Rank: 2
等 级:论坛游民
帖 子:64
专家分:44
注 册:2009-7-25
收藏
得分:0 
没有给ch开辟内存空间,导致错误
int ch[5];
这样定义就可以了
2011-10-12 10:28
lin471306489
Rank: 4
等 级:业余侠客
帖 子:136
专家分:247
注 册:2011-8-16
收藏
得分:0 
主函数里面的*ch类型应该定义成char

#include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 
#define NULL 0
 #define LEN sizeof(struct student)
 
//定义节点结构
 struct student
 {
     char no[7];
     int score;
     struct student *next;
 };
 
//create()函数:创建一个具有头结点的单链表
 //返回值:返回单链表的头指针
 
struct student *create(void)
 {
     struct student *head=NULL,*p1,*p2=NULL;
     int count=0;
     while(1)
     {
         p1=(struct student *)malloc(LEN);
 
        printf("编号%d:",count+1);
         scanf("%6s",p1->no);
         if(strcmp(p1->no,"000000")==0)           // 比较字符长度
         {
             free(p1);
             break;
         }
 
        printf("成绩%d:",count+1);
         
         scanf("%d",&p1->score);
         count++;
 
        p1->next=NULL;
 
        if(count==1)
             head=p1;
         else
             p2->next=p1;
         p2=p1;
     }
     return(head);
 }
 void print(struct student *head)
 {
     struct student *p;
     printf("学生们的成绩是:\n");
     p=head;
     while(p!=NULL)
     {
         printf("%s,%d\n",p->no,p->score);
         p=p->next;
     }
 }
 struct student *FindNode(struct student *head,int i)//依结点查找
 {
     struct student *p=head;
     int j=1;
     while(p->next!=NULL&&i>j)
     {
         p=p->next;
         j++;
     }
     if(i==j)
         return head;
     else
         return NULL;
 }
 struct student *Find(struct student *head,char *key)//依值查找
 {
     struct student *p=head;
     while(p!=NULL)
     {
         if(key != p->no && p->no !=NULL)     // 要找到相应的值,当然用strcmp比较长度是不能实现的,  
                                              //
             p=p->next;           
     }
     if(key == p->no)
         strcpy(p->no, key);
     
     return head;
 }
 void main()
 {
     char *ch;
     struct student *pt;
     pt=create();
 

    print(pt);
    printf("输入查找的值:");
    scanf("%s",&ch);            // scanf函数的使用
   
    pt=Find(pt,ch);
    printf("%s,%d\n",pt->no,pt->score);
 
}
 
唉,指针,内存很难掌握。但是更加要去用它。希望大家能继续努力找错更加多的错误
2011-10-12 13:51
快速回复:链表的查找操作问题
数据加载中...
 
   



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

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