重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

线性表有什么用?

时间:2020-09-14 05:00:06 作者:重庆seo小潘 来源:
线性表有的用法: 用顺序表举例说明 要求: 定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能: (1) 根据指定学生个数,逐个输入学生信息; (2) 逐个显示学生表中所有学生的相关信息; (3) 根据姓名进行查找,返回此学生的学号和

线性表有什么用?

线性表有的用法:

用顺序表举例说明

要求:

定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:

(1) 根据指定学生个数,逐个输入学生信息;

(2) 逐个显示学生表中所有学生的相关信息;

(3) 根据姓名进行查找,返回此学生的学号和成绩;

(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);

(5) 给定一个学生信息,插入到表中指定的位置;

(6) 删除指定位置的学生记录;

(7) 统计表中学生个数。typedef struct{char stuID[ID_SIZE];//学生学号char stuName[NAME_SIZE];//学生姓名double stuScore;//学生成绩 } StuData;typedefstruct { Student*elem;//指向数据元素的基地址intlength;//线性表的当前长度}SqList;/* * 顺序表 * 一个简陋的学生信息管理程序 * Data: 10/13/201720:42 */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <stddef.h>#include <errno.h>#include <inttypes.h>#define STU_NUM_MAX 100#define ID_SIZE 8#define NAME_SIZE 20typedef struct{char stuID[ID_SIZE];//学生学号char stuName[NAME_SIZE];//学生姓名double stuScore;//学生成绩} StuData;typedef StuData* stuPtr;typedef struct{StuData *elem;//指向动态分配的内存的首地址int length;//保存已存储的数据据元素的数目} SqList;void welcome(int *p_choose);/* * 功能:输出欢迎界面,并提示用户执行相应的操作 * 参数:指向choose的指针,通过指针改变变量choose的值,根据其值执行相应的操作 * 返回值:无 */void InitList(SqList *p_seq);/* * 功能:一次性分配所有的储存空间,初始化 * 参数:SqList的指针 * 返回值:无 */void add(SqList *p_seq);/* * 功能: * * */stuPtr info_input(stuPtr info);/* * 功能:对数组赋值,其长度不超过len * 参数:stuID: 指向数组的指针size_t * 返回值:传入的指针 */void NodeDelete(SqList *p_seq, int locate);/* * 功能:删除指定序号的数据元素 * 参数:p_seq: SqList的指针locate: 序号(第几个) * 返回值:无 */StuData *search(stuPtr p, size_t len, char *target);/* * 功能:根据指定的字符串遍历查找是否存在相应的ID or Name * 参数:p: 指向第一个顺序元素len: 已存储的数据元素的长度target: 需要查找的字符 * 返回值:指向查找到的节点,不存在则返回NULL */void print(StuData *elem, size_t len);/* * 功能:打印一定长度的数据元素 * 参数:elem: 指向某个数据元素len: 需要打印多少个数据元素(长度) * 返回值:无 */void save(FILE *stream, stuPtr p, size_t len);/* * 功能:将输入的信息保存到文件中 * 参数:stream: 指定的文件输入流p: 指向第一个数据元素len: 数据元素的长度 * 返回值:无 */int main(void){int choose;char ans = 'y';SqList L;InitList(&L);system("color 2F");while (1){fflush(stdin);ans = 'y';welcome(&choose);switch (choose){case 1:{while (ans == 'y'){if (L.length >= STU_NUM_MAX){printf("antWarning: Memory is full!n");break;}else{//info_input(&info);add(&L);printf("nnAdd succeefully!stu's num %un", L.length);printf("Continue?[y]n");fflush(stdin);ans = getchar( );if (ans == 'n'){ans = 'y';}system("cls");}}break;}case 2:{int locate;while (ans == 'y'){printf("Please enter the node number you want to delete: ");scanf("%d", &locate);NodeDelete(&L, locate);printf("annttDelete Successfullyn");printf("Continue?[y]");fflush(stdin);ans = getchar( );if (ans == 'n'){ans = 'y';}system("cls");}break;}case 3:{StuData *locate;char target[NAME_SIZE];while (ans == 'y'){printf("Please enter the ID/Name of the student you want to find: ");scanf("%s", target);locate = search(L.elem, L.length, target);if (locate == NULL){printf("anttSorry! There is no such person!n");}else{printf("aFind successfully!n");print(locate, 1);}printf("Continu?[y] ");fflush(stdin);ans = getchar( );if (ans == 'n'){ans = 'y';}system("cls");}break;}case 4:{printf("All of the stu's info are:nn");print(L.elem, L.length);getchar( );getchar( );system("cls");break;}case 5:{FILE *stream;if ((stream = fopen("info.dat", "w+")) == NULL){perror("annttSorry: Open fail!n");break;}else{save(stream, L.elem, L.length);getchar( );sleep(3);fclose(stream);system("cls");break;}}case 6:{free(L.elem);L.elem = NULL;printf("annttBye Bye!nn");sleep(2);system("cls");system("color 0F");exit(0);}default :{printf("antSorry! I have not develop the function what you want!n");sleep(2);system("cls");break;}}}return 0;}void welcome(int *p_choose){printf("nnnWELCOMEn");printf("------------------------------------------------------n");printf("--1.增加指定学生信息n");printf("--2.删除指定位置信息n");printf("--3.按学号或姓名查找n");printf("--4.显示所有学生信息n");printf("--5.保存n");printf("--6.退出n");printf("------------------------------------------------------n");printf("请输入那想要执行的操作的序号: ");scanf("%d", p_choose);system("cls");}void InitList(SqList *p_seq){p_seq->elem = (StuData *)malloc(STU_NUM_MAX*sizeof(StuData));if (p_seq->elem == NULL){perror("nnttError: memory may full");//perror??????????????_exit(1);}else{p_seq->length = 0;}}void add(SqList *p_seq){printf("Please enter information:n");while (1){printf("ID: ");scanf("%s", p_seq->elem[p_seq->length].stuID);if (strlen(p_seq->elem[p_seq->length].stuID) >= ID_SIZE){printf("It's too long, enter againn");sleep(1);system("cls");}else{break;}}while (1){printf("Name: ");scanf("%s", p_seq->elem[p_seq->length].stuName);if (strlen(p_seq->elem[p_seq->length].stuName) >= NAME_SIZE){printf("It's too long, enter againn");sleep(1);system("cls");}else{break;}}while (1){printf("Score: ");scanf("%lf", &p_seq->elem[p_seq->length].stuScore);if (p_seq->elem[p_seq->length].stuScore <0 || p_seq->elem[p_seq->length].stuScore > 100){printf("The score is percentage systemn");sleep(1);system("cls");}else{break;}}p_seq->length++;}void NodeDelete(SqList *p_seq, int locate){for (int i=locate; i<=p_seq->length; i++){memccpy((p_seq->elem[i-1]).stuID, (p_seq->elem[i]).stuID, '', ID_SIZE);memccpy((p_seq->elem[i-1]).stuName, (p_seq->elem[i]).stuName, '', NAME_SIZE);(p_seq->elem[i-1]).stuScore = (p_seq->elem[i]).stuScore;}p_seq->length--;}stuPtr search(stuPtr p, size_t len, char *target){for (unsigned i=0; i<len; i++){if (strcmp(p[i].stuID, target) == 0){return (p+i);}else if (strcmp(p[i].stuName, target)== 0){return (p+i);}}return NULL;}void print(StuData *elem, size_t len){printf("IDNameScoren");printf("-----------------------------------------------n");if (len <= 0){printf("anttWarning: NO data exist!n");}else{unsigned count = 0;for (unsigned i=0; i<len; i++){printf("%3d-", ++count);printf(" %stt", elem[i].stuID);printf(" %stt", elem[i].stuName);printf(" %.2lfn", elem[i].stuScore);}printf("num: %lun", len);}printf("-----------------------------------------------n");}void save(FILE *stream, stuPtr p, size_t len){if (len <= 0){printf("annttSorry: No data exist!n");sleep(2);return;}else{unsigned count = 0;fprintf(stream, "IDNameScoren");fprintf(stream, "-------------------------------------------------n");for (unsigned i=0; i<len; i++){fprintf(stream, "%3d-", ++count);fprintf(stream, " %stt", p[i].stuID);fprintf(stream, " %stt", p[i].stuName);fprintf(stream, " %.2lfn", p[i].stuScore);}fprintf(stream,"n-------------------------------------------------n");printf("anntSave successfully!nn");}}以上就是线性表有什么用?的详细内容,更多请关注小潘博客其它相关文章!