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);
  }
}


相关文章
|
1月前
|
C语言
对链表使用插入排序的C语言实现示例
对链表使用插入排序的C语言实现示例
|
1月前
19 删除链表的倒数第 N 个结点
19 删除链表的倒数第 N 个结点
|
2月前
|
存储 编译器 C语言
【数据结构】C语言实现带头双向循环链表万字详解(附完整运行代码)
【数据结构】C语言实现带头双向循环链表万字详解(附完整运行代码)
11 0
|
2月前
|
存储 编译器 C语言
【数据结构】C语言实现单链表万字详解(附完整运行代码)
【数据结构】C语言实现单链表万字详解(附完整运行代码)
46 0
|
10天前
|
存储 编译器 C语言
C语言进阶第十课 --------文件的操作-1
C语言进阶第十课 --------文件的操作
|
12天前
|
C语言
链表的插入、删除和查询—C语言
链表的插入、删除和查询—C语言
|
15天前
|
C语言
C语言用头插法建立单链表
C语言用头插法建立单链表
5 0
|
17天前
|
存储 C语言
C语言中字符串的引用与数组元素操作
C语言中字符串的引用与数组元素操作
21 0
|
25天前
|
存储 算法 C语言
C语言线性链表
【4月更文挑战第10天】C程序线性链表
21 0
数据结构|双向链表|带头结点|头插|尾插|尾删|头删
数据结构|双向链表|带头结点|头插|尾插|尾删|头删