单链表实验
实验内容
定义一个包含学生信息(学号,姓名,成绩)的链表,使其具有如下功能:
(1) 根据指定学生个数,逐个输入学生信息;
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和成绩;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录
#include<stdio.h> #include<stdlib.h> #include<string.h> #define Maxsize 100 typedef struct student { char xh[20]; char xm[20]; int cj; struct student* next; }*stu; stu stuhead; void insertstus(int num) { stu c = stuhead->next; stu p=c; for (int i = 0; i < num; i++) { char xm[20]; char xh[20]; int cj; gets(xm); gets(xh); scanf("%d", &cj); stu s = (stu)malloc(sizeof(stu)); s->cj = cj; strcpy(s->xh, xh); strcpy(s->xm, xm); c->next = s; s->next = NULL; p = c; c = c->next; } } void printstu() { stu students = stuhead->next; while (students) { printf("%s,%s,%d", students->xh, students->xm, students->cj); students = students->next; } } stu findname(char* name) { stu students = stuhead->next; while (students) { if (students->xm == name) { return students; } } } stu findstu(int i) { int count = 1; stu students = stuhead->next; while (students) { if (count==i) { return students; } } } void insertstu(stu s, int j) { int count = 0; stu c= stuhead->next; while (c) { if (j==0) { stuhead->next = s; s->next = c; break; } if (count==j-1) { s->next = c->next; c->next = s; break; } c = c->next; } } void delstu(int j) { int count = 1; stu students = stuhead->next; while (students) { if (count=j-1) { students->next = students->next->next; } } } int countstu() { int count = 0; stu students = stuhead->next; while (students) { count = count + 1; } return count; }
好了,我们下回见,peace

浙公网安备 33010602011771号