目录
老规矩,先看看结果是不是你想要的:
代码分析:
注:由于输入与输出函数与单链表的删除与插入相同,所以这里不在一一解释!
第一步:声明
#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); } }