C语言:单链表的查询操作,查询链表的某一结点。

简介: C语言:单链表的查询操作,查询链表的某一结点。

目录

老规矩,先看看结果是不是你想要的:

代码分析:

第一步:声明

第二步:输入函数

第三步:(关键)查询操作的函数

第四步:输出

第五步:主函数

完整代码:


 

老规矩,先看看结果是不是你想要的:



代码分析:

       注:由于输入与输出函数与单链表的删除与插入相同,所以这里不在一一解释!

第一步:声明

#include<stdio.h>
#include<stdlib.h>
struct student     //声明结构体类型
{
  int num;
  float score;
  struct student *next;
};
int n; //全局变量
struct student *h;//此处不同,这里的链表为全局变量


第二步:输入函数

struct student *creat()
{
  struct student *head;
  struct student *p1,*p2;
  p1=(struct student *)malloc(sizeof(struct student));
  p2=p1;
  printf("请输入学生学号、成绩 :");
  scanf("%d%f",&p1->num,&p1->score);
  head=NULL;
  n=0;
  while(p1->num!=0)
  {
    n++;
    if(n==1)
    {
      head=p1;
    }
    else
    {
      p2->next=p1;
    }
    p2=p1;
    p1=(struct student *)malloc(sizeof(struct student));
    printf("请输入学生学号、成绩:");
    scanf("%d%f",&p1->num,&p1->score);
  }
  p2->next=NULL;
  return head;
}


第三步:(关键)查询操作的函数

struct student *refers(struct student *hl)
{
  struct student* p1, * p2, * p0; //声明结构体类型的变量
  int num; 
  int ch;
  p0 = NULL; //初始化为NULL
  while (1)
  {
    p1 = p2 = hl; //此处需要注意,不能放在while的外面
    printf("\n你是否需要查询(Y/N):");
    do {
      ch = getchar();
    } while (ch != 'Y' && ch != 'N'); //当输入不符时,循环输入
    if (ch == 'Y') 
    {
      printf("\n请输入你要查询的学号:");
      scanf("%d", &num); //满足条件开始输入
      while (p1->num != num && p1->next != NULL) //判断结点在何处
      {
        p2 = p1;
        p1 = p1->next;
      }
      if (p1->num == num) //结点处
      {
        p0 = p1; //将p1的数据赋值给p0
        printf("\n你的查询结果:"); 
        printf("\n\t学号:\t成绩:");
        printf("\n\t%d\t%.2f", p0->num, p0->score); //直接打印结果
      }
      else
      {
        printf("未找到相关数据!"); 
      }
    }
    else {
      printf("\n你已退出查询!");
      break;  //此处退出循环
    }
  }
  return h; //返回值为整个单链表
}


第四步:输出

void print(struct student *h)
{
  struct student *p;
  p=h;
  if(h!=NULL)
  {
    printf("\n结果是:");
    do{
      printf("\n\t%d\t%.2f",p->num,p->score);
      p=p->next;
    }while(p!=NULL);
  }
}


第五步:主函数

int main()
{
  struct student *creat();
  struct student *refers(struct student *hl); 
  void print(struct student *h);
  h=creat();
  print(h);
  refers(h); //直接调用函数即可
  return 0;
}

完整代码:

#include<stdio.h>
#include<stdlib.h>
struct student 
{
  int num;
  float score;
  struct student *next;
};
int n;
struct student *h;
int main()
{
  struct student *creat();
  struct student *refers(struct student *hl); 
  void print(struct student *h);
  h=creat();
  print(h);
  refers(h);
  return 0;
}
struct student *creat()
{
  struct student *head;
  struct student *p1,*p2;
  p1=(struct student *)malloc(sizeof(struct student));
  p2=p1;
  printf("请输入学生学号、成绩 :");
  scanf("%d%f",&p1->num,&p1->score);
  head=NULL;
  n=0;
  while(p1->num!=0)
  {
    n++;
    if(n==1)
    {
      head=p1;
    }
    else
    {
      p2->next=p1;
    }
    p2=p1;
    p1=(struct student *)malloc(sizeof(struct student));
    printf("请输入学生学号、成绩:");
    scanf("%d%f",&p1->num,&p1->score);
  }
  p2->next=NULL;
  return head;
}
struct student *refers(struct student *hl)
{
  struct student* p1, * p2, * p0;
  int num;
  int ch;
  p0 = NULL;
  while (1)
  {
    p1 = p2 = hl;
    printf("\n你是否需要查询(Y/N):");
    do {
      ch = getchar();
    } while (ch != 'Y' && ch != 'N');
    if (ch == 'Y')
    {
      printf("\n请输入你要查询的学号:");
      scanf("%d", &num);
      while (p1->num != num && p1->next != NULL)
      {
        p2 = p1;
        p1 = p1->next;
      }
      if (p1->num == num)
      {
        p0 = p1;
        printf("\n你的查询结果:");
        printf("\n\t学号:\t成绩:");
        printf("\n\t%d\t%.2f", p0->num, p0->score);
      }
      else
      {
        printf("未找到相关数据!");
      }
    }
    else {
      printf("\n你已退出查询!");
      break;
    }
  }
  return h;
}
void print(struct student *h)
{
  struct student *p;
  p=h;
  if(h!=NULL)
  {
    printf("\n结果是:");
    do{
      printf("\n\t%d\t%.2f",p->num,p->score);
      p=p->next;
    }while(p!=NULL);
  }
}


相关文章
|
13天前
链表的中间结点
链表的中间结点
169 57
|
11天前
|
存储 算法 C语言
数据结构基础详解(C语言):单链表_定义_初始化_插入_删除_查找_建立操作_纯c语言代码注释讲解
本文详细介绍了单链表的理论知识,涵盖单链表的定义、优点与缺点,并通过示例代码讲解了单链表的初始化、插入、删除、查找等核心操作。文中还具体分析了按位序插入、指定节点前后插入、按位序删除及按值查找等算法实现,并提供了尾插法和头插法建立单链表的方法,帮助读者深入理解单链表的基本原理与应用技巧。
|
11天前
|
存储 C语言
C语言程序设计核心详解 第九章 结构体与链表概要详解
本文档详细介绍了C语言中的结构体与链表。首先,讲解了结构体的定义、初始化及使用方法,并演示了如何通过不同方式定义结构体变量。接着,介绍了指向结构体的指针及其应用,包括结构体变量和结构体数组的指针操作。随后,概述了链表的概念与定义,解释了链表的基本操作如动态分配、插入和删除。最后,简述了共用体类型及其变量定义与引用方法。通过本文档,读者可以全面了解结构体与链表的基础知识及实际应用技巧。
|
11天前
|
存储 算法 C语言
C语言手撕实战代码_循环单链表和循环双链表
本文档详细介绍了用C语言实现循环单链表和循环双链表的相关算法。包括循环单链表的建立、逆转、左移、拆分及合并等操作;以及双链表的建立、遍历、排序和循环双链表的重组。通过具体示例和代码片段,展示了每种算法的实现思路与步骤,帮助读者深入理解并掌握这些数据结构的基本操作方法。
|
11天前
|
算法 C语言 开发者
C语言手撕实战代码_单链表
本文档详细介绍了使用C语言实现单链表的各种基本操作和经典算法。内容涵盖单链表的构建、插入、查找、合并及特殊操作,如头插法和尾插法构建单链表、插入元素、查找倒数第m个节点、合并两个有序链表等。每部分均配有详细的代码示例和注释,帮助读者更好地理解和掌握单链表的编程技巧。此外,还提供了判断子链、查找公共后缀等进阶题目,适合初学者和有一定基础的开发者学习参考。
|
17天前
|
存储 测试技术 C语言
C语言实现链表的各种功能
本文详细介绍了如何使用C语言实现链表的各种功能,包括链表节点结构的定义与操作函数的实现。链表作为一种常用的数据结构,具有节点自由插入删除、动态变化等特点。文中通过`link_list.h`和`link_list.c`两个文件,实现了链表的初始化、插入、删除、查找、修改等核心功能,并在`main.c`中进行了功能测试。这些代码不仅展示了链表的基本操作,还提供了丰富的注释帮助理解,适合作为学习链表的入门资料。
|
6天前
|
C语言
C语言里的循环链表
C语言里的循环链表
|
1月前
|
算法
LeetCode第19题删除链表的倒数第 N 个结点
该文章介绍了 LeetCode 第 19 题删除链表的倒数第 N 个结点的解法,通过使用快慢双指针,先将快指针移动 n 步,然后快慢指针一起遍历,直到快指针到达链尾,从而找到倒数第 N 个结点的前一个结点进行删除,同时总结了快慢指针可减少链表遍历次数的特点。
LeetCode第19题删除链表的倒数第 N 个结点
|
1月前
|
存储 C语言
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
|
1月前
【刷题记录】链表的中间结点
【刷题记录】链表的中间结点