欧卡2中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

需要三步,才能开始

只需两步,慢速开始

玩欧卡就用莱仕达V10方向盘欧卡2入门方向盘选莱仕达V9莱仕达折叠便携游戏方向盘支架欢迎地图Mod入驻
查看: 6154|回复: 0
收起左侧

链表的查找操作问题

[复制链接]
amy328 发表于 2012-1-7 02:29 | 显示全部楼层 |阅读模式
主函数里面的*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(&quot;编号%d:&quot;,count+1);
         scanf(&quot;%6s&quot;,p1->no);
         if(strcmp(p1->no,&quot;000000&quot;)==0)           // 比较字符长度
         {
             free(p1);
             break;
         }

        printf(&quot;成绩%d:&quot;,count+1);
         
         scanf(&quot;%d&quot;,&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(&quot;学生们的成绩是:\n&quot;);
     p=head;
     while(p!=NULL)
     {
         printf(&quot;%s,%d\n&quot;,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(&quot;输入查找的值:&quot;);
    scanf(&quot;%s&quot;,&ch);            // scanf函数的使用
   
    pt=Find(pt,ch);
    printf(&quot;%s,%d\n&quot;,pt->no,pt->score);

}

唉,指针,内存很难掌握。但是更加要去用它。希望大家能继续努力找错更加多的错误
                                                                                                        <div id="post_rate_div_2020679">

联系我们|手机版|欧卡2中国 ( 湘ICP备11020288号-1 )

GMT+8, 2024-4-20 09:44 , Processed in 0.036010 second(s), 7 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表