| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 832 人关注过本帖
标题:链表的查找操作问题
取消只看楼主 加入收藏
niu91
Rank: 2
等 级:论坛游民
帖 子:64
专家分:44
注 册:2009-7-25
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:1 
链表的查找操作问题
程序代码:
#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
niu91
Rank: 2
等 级:论坛游民
帖 子:64
专家分:44
注 册:2009-7-25
收藏
得分:0 
没有给ch开辟内存空间,导致错误
int ch[5];
这样定义就可以了
2011-10-12 10:28
快速回复:链表的查找操作问题
数据加载中...
 
   



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

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