数据结构——练习之学生信息操作

来源:互联网 发布:普华永道审计知乎 编辑:程序博客网 时间:2024/06/11 03:11

数据结构——练习之学生信息操作

实现的功能如下:
源码:
#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);elseprintf("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;}}}


0 0
原创粉丝点击