数据结构——练习之学生信息操作
2017-04-08 12:22
441 查看
数据结构——练习之学生信息操作
实现的功能如下:源码:
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> /**定义学生数据对象的结构体**/ struct stu_node{ char name[10]; int score; struct stu_node *next; }; typedef struct stu_node Student; //将结构体别名 /**函数体声明**/ Student* CreateList(int n); void PrintList(Student* s); int InsertList(Student* s,int i,char name[],int score,int n); int FindList(Student* s,char name[]); void DeleteList(Student* s,int j); void ChangeList(Student* s,int j,char name[],int score); /**创建链表**/ Student* CreateList(int n){ Student *head; Student *pre,*p; //pre指向当前元素,p指向前一个元素 int i; head = (Student *)malloc(sizeof(Student)); //分配空间 head->next = NULL; //创建空链表 pre = head; for(i = 0;i<n;i++){ printf("input name of the %d student:",i); p = (Student *)malloc(sizeof(Student));//为当前元素分配空间 scanf("%s",&p->name); printf("input score of the %d student:",i); scanf("%d",&p->score); pre->next = p; pre = p; } p->next = NULL; PrintList(head); return head; } /**输出链表**/ void PrintList(Student* s){ Student *p; p = s->next; while(p){ printf("%s,%d",p->name,p->score); p = p->next; printf("\n"); } } /**插入元素**/ int InsertList(Student* s,int i,char name[],int score,int n){ int j = 0; Student *pre,*ne; pre = s; if(i<1||i>n+1){ printf("Error!"); }else{ while(j<i-1){ pre = pre->next; j++; } ne = (Student *)malloc(sizeof(Student)); strcpy(ne->name,name); ne->score = score; ne->next = pre->next; pre->next = ne; n++; } PrintList(s); return n; } /**按名字查找结点**/ int FindList(Student* s,char name[]){ Student *pre = s; int i =0; while(pre){ if(strcmp(pre->name,name)==0){ return i; }else{ i++; pre = pre->next; } } return -1; } /**删除节点**/ void DeleteList(Student* s,int j){ Student *pre = s; int i = 0; while(i<j-1){ pre = pre->next; i++; } Student *p = pre->next; pre->next = p->next; printf("Deleted student (%s,%d)\n",p->name,p->score); free(p); PrintList(s); } /**改变链表节点**/ void ChangeList(Student* s,int j,char name[],int score){ Student *pre = s; int i = 0; while(i<j){ pre = pre->next; i++; } printf("Changed student (%s,%d) to (%s,%d)\n",pre->name,pre->score,name,score); strcpy(pre->name,name); pre->score = score; PrintList(s); } void main(){ Student *student; int i =1,n=0,j; char name[10]; char old_name[10]; int score; while(i){ printf("1--建立信息表\t"); printf("2--更改信息表\n"); printf("3--查找信息表\t"); printf("4--删除学生信息\n"); printf("5--打印信息表\t"); printf("6--添加学生信息\n"); scanf("%d",&i); switch (i) { case 1: printf("Input the number of students\t"); scanf("%d",&n); if(n<0){ printf("Error!\n"); break; } student = CreateList(n); break; case 2: printf("Input the name of student to change:"); scanf("%s",&old_name); j =FindList(student,old_name); if(j==-1){ printf("Can't Find student %s\n",old_name); }else{ printf("Input the student's name\t"); scanf("%s",&name); printf("Input the student's score\t"); scanf("%d",&score); ChangeList(student,j,name,score); } break; case 3: printf("Input the name of student to find:"); scanf("%s",&old_name); j =FindList(student,old_name); if(j==-1){ printf("Can't Find student %s\n",old_name); }else{ printf("The student's position is %d\n",j); } break; case 4: printf("Input the name of student to delete:"); scanf("%s",&old_name); j =FindList(student,old_name); if(j==-1){ printf("Can't Find student %s\n",old_name); }else{ DeleteList(student,j); n--; } break; case 5: if(n>0) PrintList(student); else printf("The list is empty\n"); break; case 6: printf("Input the student's name\t"); scanf("%s",&name); printf("Input the student's score\t"); scanf("%d",&score); printf("Input the position to insert:"); int p; scanf("%d",&p); n = InsertList(student,p,name,score,n); break; default: printf("Please choose from the menu!"); break; } } }
相关文章推荐
- (2013-4-1)数据结构实验一:打印成绩最高和最低的学生信息
- 数据结构实验1 链表 实现学生信息存储,功能 增删改查,实现 结构类型
- 数据结构课程设计——学生信息管理系统
- 数据结构课程设计--学生信息管理系统
- 数据结构课程设计-学生信息管理
- 数据结构课程设计-------学生信息管理系统
- 第三章数程序设计初步--控制结构综合项目4-1文件操作初体验练习1从文件读取数据
- 数据结构课程设计-学生信息管理
- 学生信息管理系统-数据结构课程设计
- 第三章数程序设计初步--控制结构综合项目4-2文件操作初体验练习3将数据写入文件
- 数据结构课程设计论文--学生信息管理系统
- 数据结构课程设计---学生信息管理系统
- c++练习004之数据结构(顺序表操作)
- 数据结构课程设计——学生信息管理系统
- 第三章数程序设计初步--控制结构综合项目4-2文件操作初体验练习4将数据写入文件
- 数据结构实验一:基于线性表的学生信息管理(顺序结构)
- 数据结构课程设计论文--学生信息管理系统
- 数据结构课程设计-学生信息管理(改)
- 数据结构课程设计-学生各方面信息管理系统
- 数据结构课程设计【学生信息管理系统(烟大版)】