顺序表
要求:
定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:
(1) 根据指定学生个数,逐个输入学生信息;
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和成绩;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录;
(7) 统计表中学生个数。
typedef struct { char stuID[ID_SIZE]; //学生学号 char stuName[NAME_SIZE]; //学生姓名 double stuScore; //学生成绩 } StuData;
1 typedef struct { 2 Student *elem; //指向数据元素的基地址 3 int length; //线性表的当前长度 4 }SqList;
完整代码:
1 /* 2 * 顺序表 3 * 一个简陋的学生信息管理程序 4 * Data: 10/13/2017 20:42 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <unistd.h> 11 #include <stddef.h> 12 #include <errno.h> 13 #include <inttypes.h> 14 #define STU_NUM_MAX 100 15 #define ID_SIZE 8 16 #define NAME_SIZE 20 17 18 typedef struct 19 { 20 char stuID[ID_SIZE]; //学生学号 21 char stuName[NAME_SIZE]; //学生姓名 22 double stuScore; //学生成绩 23 } StuData; 24 typedef StuData* stuPtr; 25 26 typedef struct 27 { 28 StuData *elem; //指向动态分配的内存的首地址 29 int length; //保存已存储的数据据元素的数目 30 } SqList; 31 32 void welcome(int *p_choose); 33 /* 34 * 功能:输出欢迎界面,并提示用户执行相应的操作 35 * 参数:指向choose的指针,通过指针改变变量choose的值,根据其值执行相应的操作 36 * 返回值:无 37 */ 38 39 void InitList(SqList *p_seq); 40 /* 41 * 功能:一次性分配所有的储存空间,初始化 42 * 参数:SqList的指针 43 * 返回值:无 44 */ 45 46 void add(SqList *p_seq); 47 /* 48 * 功能: 49 * 50 * 51 */ 52 53 stuPtr info_input(stuPtr info); 54 /* 55 * 功能:对数组赋值,其长度不超过len 56 * 参数:stuID: 指向数组的指针 size_t 57 * 返回值:传入的指针 58 */ 59 60 void NodeDelete(SqList *p_seq, int locate); 61 /* 62 * 功能:删除指定序号的数据元素 63 * 参数:p_seq: SqList的指针 locate: 序号(第几个) 64 * 返回值:无 65 */ 66 67 StuData *search(stuPtr p, size_t len, char *target); 68 /* 69 * 功能:根据指定的字符串遍历查找是否存在相应的ID or Name 70 * 参数:p: 指向第一个顺序元素 len: 已存储的数据元素的长度 target: 需要查找的字符 71 * 返回值:指向查找到的节点,不存在则返回NULL 72 */ 73 74 void print(StuData *elem, size_t len); 75 /* 76 * 功能:打印一定长度的数据元素 77 * 参数:elem: 指向某个数据元素 len: 需要打印多少个数据元素(长度) 78 * 返回值:无 79 */ 80 81 void save(FILE *stream, stuPtr p, size_t len); 82 /* 83 * 功能:将输入的信息保存到文件中 84 * 参数:stream: 指定的文件输入流 p: 指向第一个数据元素 len: 数据元素的长度 85 * 返回值:无 86 */ 87 88 89 int main(void) 90 { 91 int choose; 92 char ans = 'y'; 93 SqList L; 94 95 InitList(&L); 96 system("color 2F"); 97 while (1) 98 { 99 fflush(stdin); 100 ans = 'y'; 101 welcome(&choose); 102 switch (choose) 103 { 104 case 1: 105 { 106 while (ans == 'y') 107 { 108 if (L.length >= STU_NUM_MAX) 109 { 110 printf("\a\n\tWarning: Memory is full!\n"); 111 break; 112 } 113 else 114 { 115 //info_input(&info); 116 add(&L); 117 printf("\n\nAdd succeefully! stu's num %u\n", L.length); 118 printf("Continue?[y]\n"); 119 fflush(stdin); 120 ans = getchar( ); 121 if (ans == '\n') 122 { 123 ans = 'y'; 124 } 125 system("cls"); 126 } 127 } 128 break; 129 } 130 case 2: 131 { 132 int locate; 133 while (ans == 'y') 134 { 135 printf("Please enter the node number you want to delete: "); 136 scanf("%d", &locate); 137 NodeDelete(&L, locate); 138 printf("\a\n\n\t\tDelete Successfully\n"); 139 printf("Continue?[y]"); 140 fflush(stdin); 141 ans = getchar( ); 142 if (ans == '\n') 143 { 144 ans = 'y'; 145 } 146 system("cls"); 147 } 148 break; 149 } 150 case 3: 151 { 152 StuData *locate; 153 char target[NAME_SIZE]; 154 while (ans == 'y') 155 { 156 printf("Please enter the I