链表示例学生成绩2_第1页
链表示例学生成绩2_第2页
链表示例学生成绩2_第3页
链表示例学生成绩2_第4页
链表示例学生成绩2_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、说明:一共五个文件 puts("<a> 添加项目 <s>显示所有项目"); puts("<c> 显示所有数量 <d>删除项目"); puts("<f> 查找 <x>修改学生信息");puts("<q> 退出");Student.h/* 简单的链表示例程序 学生信息管理*/* stdbool.h头文件如果编译不过 自己定义bool true false等*/#ifndef _STUDENT_H_ #define _STUDENT_H_

2、#include<stdbool.h>#include<stdio.h>#define SIZE_NAME 20/* 学生信息结构*/typedef struct student char nameSIZE_NAME; int num; char sex; float score; struct student *next;STU,*P_STU;/* 链表的头指针和尾指针及数量*/typedef struct nod P_STU head; P_STU last; int count;ND,*P_ND;/* 函数原型*/char character(); /菜单的字符选

3、择void Initialize_List(P_ND node); /初始化链表bool Add_Item(P_ND node,P_STU item); /添加新项目void Show_AllItem(P_ND node); /显示所有项目bool Delete_AllItem(P_ND node); /删除所有项目bool Delete_Item(P_ND node); /删除项目P_STU find_STU(int,P_ND); /查找bool amend_STU(P_STU); /修改学生信息void show_one(P_STU); /显示单个学生信息#endifMain.c#incl

4、ude<stdio.h>#include<conio.h>#include"student.h"int main() ND node; STU item; char ch; Initialize_List(&node); while(ch=character() if(ch='Q') break; if(ch='A') puts("输入学生姓名: "); gets(); puts("输入学生性别: "); item.sex=getchar(); fflu

5、sh(stdin); puts("输入学生学号,不超过5位数: "); fscanf(stdin,"%d",&item.num); puts("输入学生成绩: "); fscanf(stdin,"%f",&item.score); Add_Item(&node,&item); if(ch='S') Show_AllItem(&node); if(ch='C') fprintf(stdout,"共有 %d 个项目n",node

6、.count); if(ch='D') if(Delete_Item(&node) puts("删除成功!"); else puts("删除失败!"); if(ch='F') int number=0; fputs("输入要查找该生的学号: ",stdout); scanf("%d",&number); find_STU(number,&node); if(ch='X') int number=0; P_STU stu; fputs("

7、;输入要修改该生的学号: ",stdout); scanf("%d",&number); if(stu=find_STU(number,&node)!=NULL) amend_STU(stu); puts("DONE!"); Delete_AllItem(&node); getch(); return 0;Menu.c#include<stdio.h>#include<string.h>#include<ctype.h>char character() char ch; puts(&q

8、uot;<a> 添加项目 <s>显示所有项目"); puts("<c> 显示所有数量 <d>删除项目"); puts("<f> 查找 <x>修改学生信息"); puts("<q> 退出"); while(ch=getchar() fflush(stdin); /刷新缓冲区 ch=toupper(ch); /转换为大写字母 if(strchr("ACDQSFX",ch)=NULL) continue; else return

9、 ch; return ch='Q'Delete.c#include<stdio.h>#include<stdlib.h>#include"student.h"/* 定义一个结构来保存 被找到项目的前后指针*/typedef struct Front_Rear_point STU *front; STU *rear; FR;bool Delete_Item(ND *node) STU *current=node->head; int num; int flag=0; FR frp; frp.front=node->head

10、; /结构体的 frp.rear=node->head; /初始化 if(node->head=NULL) puts("数据为空!"); return false; fprintf(stdout,"输入要删除该学生信息的学号: "); scanf("%d",&num); while(current!=NULL) if(current->num=num) if(node->head=current)&&(node->head->next=NULL) /如果链表头的 puts(&

11、quot;链表中只有一个项目!"); /next是NULL,说明 free(current); /链表里只有1项 node->head=NULL; /这里是初始化链表 node->last=NULL; /的功能,如能调用 node->count-; /初始化函数该怎么做 return true; if(node->last=current) frp.front->next=NULL; /如果要删除的项目 free(current); /在链表的最后 node->count-; node->last=frp.front; return true

12、; if(node->head=current)&&(node->head->next!=NULL) node->head=node->head->next; /如果要删除的item free(current); /是链表的头 node->count-; return true; frp.front->next=frp.rear->next;/要删除的item free(current); /在链表的其他地方 node->count-; return true; else frp.front=current; curr

13、ent=current->next; frp.rear=current; if(+flag)=node->count) puts("未找到要删除的项目! "); return false; return false;Student.c-#include<stdio.h>#include<stdbool.h>#include<stdlib.h>#include"student.h"/* 初始化 */void Initialize_List(P_ND node) node->count=0; node-&

14、gt;head=NULL; node->last=NULL;/* 添加项目 */bool Add_Item(P_ND node,P_STU item) P_STU current; if(current=(P_STU)malloc(sizeof(STU)=NULL) puts("内存分配失败,程序终止!"); exit(1); *current=*item; current->next=NULL; if(node->head=NULL) node->head=current; else node->last->next=current;

15、node->last=current; node->count+; return true;/* 删除所有项目 */bool Delete_AllItem(P_ND node) P_STU current=node->head; P_STU temp; if(node->head=NULL) puts("数据为空!"); return true; while(current!=NULL) temp=current->next; free(current); node->count-; current=temp; if(node->c

16、ount!=0) puts("删除出错,数量错误!"); else return true; return 0;/* 显示所有项目 */void Show_AllItem(P_ND node) P_STU current=node->head; if(node->head=NULL) puts("数据为空!"); system("cls"); while(current!=NULL) fprintf(stdout,"姓名: %sn",current->name); fprintf(stdout,&

17、quot;性别: %cn",current->sex); fprintf(stdout,"学号: %dn",current->num); fprintf(stdout,"成绩: %.2fn",current->score); puts("n-"); current=current->next; fprintf(stdout,"总共有 %d 项n",node->count); puts("-n");/* 查找 */P_STU find_STU(int num

18、ber,P_ND node) P_STU current=node->head; if(node->head=NULL) puts("无数据!nn"); return NULL; while(current->num!=number) current=current->next; if(current=NULL) puts("未找到该生!"); return NULL; show_one(current); return current;/* 修改学生信息 */bool amend_STU(P_STU stu) STU temp; int flag=1; char ch='Y' puts("请重新输入该生信息:"); while(flag) fflush(stdin); fputs("学生姓名: ",stdout); gets(); fputs("学生学号: ",stdout); scanf("%d",&temp.num); fflush(stdin); fputs("学生性别: ",stdout

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论