链表 查询信息

写一程序用来按学生姓名查询其排名和平均成绩。n个学生的排名,姓名和成绩数据从键盘输入。
 查询可连续进行, 直到键入查询关键字为0时结束程序。
【输入说明】
输入第一行1个整数,表示n。接下来n行输入表示n名学生的信息。之后输入的数据表示要查询的学生姓名。
 查询关键字为0时结束程序。
【输出说明】
若能查询到数据,则显示排名、姓名和成绩,数据间使用空格间隔。若不能查询,则显示“Not found XXX!”。
【输入样例】
2
1 60 zhangsan
2 50 lisi
zhangsan
wangwu
0
【输出样例】
1 zhangsan 60
Not found wangwu!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct student)
struct student
{
    int rank;
    char name[10];
    int score;
    struct student* next;
};
struct student* create(int n);//创建n个节点的链表
struct student* find_list();   //建立需要查询的数据的链表
void find_print(struct student*headnode_a, struct student* headnode_b);         //查询并且打印链表
int main()
{
    int n;
    printf("请输入创建的名单个数\n");
    scanf("%d", &n);
    struct student* list_A = create(n);
    struct student* list_B = find_list();
    find_print(list_A,list_B);
    return 0;
}
struct student* create(int n)//创建n个节点的链表
{
    struct student* headnode = (struct student*)malloc(LEN);
    struct student* pmove = (struct student*)malloc(LEN);
    headnode->next = NULL;
    pmove = headnode;
    for (int i = 0; i < n; i++)
    {
        struct student* newnode = (struct student*)malloc(LEN);
        pmove->next = newnode;
        newnode->next = NULL;
        pmove = pmove->next;
    }
    pmove = headnode;
    printf("请输入学生学号\t平均成绩\t姓名\t\n");
    while (pmove->next)
    {
        scanf("%d %d%s", &pmove->rank, &pmove->score, &pmove->name);
        pmove = pmove->next;
    }
    return headnode;
}
struct student* find_list()   //建立需要查询的数据的链表
{
    struct student* headnode = (struct student*)malloc(LEN);
    struct student* pmove = (struct student*)malloc(LEN);
    headnode->next = NULL;
    pmove = headnode;
    printf("请输入需要查找的学生名字,若结束,则输入‘0’\n");
    while (1)
    {
        struct student* newnode = (struct student*)malloc(LEN);
        pmove->next = newnode;
        newnode->next = NULL;
        scanf("%s", &pmove->name);
        if (!strcmp((pmove->name),"0"))
        {
            break;
        }
        pmove = pmove->next;
    }
    return headnode;
}
void find_print(struct student* headnode_a, struct student* headnode_b)  //查询并且打印链表
{
    struct student* pmove_a = (struct student*)malloc(LEN);
    struct student* pmove_b = (struct student*)malloc(LEN);
    pmove_b = headnode_b;
    printf("查询结果:\n");
    while (pmove_b)    //遍历b链表(待查询)
    {
        pmove_a = headnode_a;
        int i = 0; //判断:   0的含义 遍历a链表没查到 
        while (pmove_a)  //遍历a链表
        {
            if (!strcmp(pmove_a->name, pmove_b->name))
            {
                i = 1; //判断  1的含义 查到了
                printf("%d %s %d\n", pmove_a->rank, pmove_a->name, pmove_a->score);
            }
            pmove_a = pmove_a->next;
        }
        if (i == 0)
        {
            printf("Not found %s!\n", pmove_b->name);
        }
        if (!strcmp(pmove_b->next->name, "0"))//如果下一个名字是 ‘0’ 结束标志 则结束循环
        {
            break;
        }
        pmove_b = pmove_b->next;

    }
}

运行结果

 

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
在C语言中,链表查询可以通过遍历链表来实现。具体的步骤如下: 1. 首先,定义一个指向链表头节点的指针,用于遍历链表。 2. 从头节点开始,依次比较每个节点的值是否与目标值相等。 3. 如果找到匹配的节点,返回该节点的位置或者其他需要的信息。 4. 如果遍历完整个链表都没有找到匹配的节点,则表示目标值不存在于链表中。 这样,我们就可以通过遍历链表来进行查询操作。 需要注意的是,在进行链表查询时,要确保链表的指针不为空,以避免出现空指针异常。另外,在遍历链表时,可以利用循环结构来实现,直到遍历到链表的末尾或者找到匹配的节点为止。 总结起来,链表查询操作可以通过遍历链表,依次比较每个节点的值来实现。这是一个常见且基础的链表操作,对于理解链表的概念和应用非常重要。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [一看就懂-c语言链表的查找,删除,清空【初阶】](https://blog.csdn.net/weixin_64524066/article/details/122373656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [C语言链表超详解](https://blog.csdn.net/k666499436/article/details/124787990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值