代码中有详细的解释
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef struct Student {
int *s_id;
char *s_sex;
char *s_name;
};
typedef struct SqList {
Student *elem;
int length;
int listSize;
};
int main() {
int InitList(SqList &L);
int GetStudentNumber(SqList L, char *name);
int *GetStudentNumberByName(SqList L, char *name);
int GetStudentNumberById(SqList L, int *id);
int ListInsert(SqList &L, int i, Student s);
int ListDelete(SqList &L, int i, Student &s);
SqList list;
InitList(list);
int s1_id[10], s2_id[10], s3_id[10], s4_id[10], s5_id[10], s6_id[10];
for(int i = 0; i < 9; i++) {
s1_id[i] = i + 1;
s2_id[i] = i + 1;
s3_id[i] = i + 1;
s4_id[i] = i + 1;
s5_id[i] = i + 1;
s6_id[i] = i + 1;
}
s1_id[9] = 1;
s2_id[9] = 2;
s3_id[9] = 3;
s4_id[9] = 4;
s5_id[9] = 5;
s6_id[9] = 6;
char *s1_name, *s2_name, *s3_name, *s4_name, *s5_name, *s6_name;
s1_name = (char *)malloc(sizeof(char) * 12);
s2_name = (char *)malloc(sizeof(char) * 12);
s3_name = (char *)malloc(sizeof(char) * 12);
s4_name = (char *)malloc(sizeof(char) * 12);
s5_name = (char *)malloc(sizeof(char) * 12);
s6_name = (char *)malloc(sizeof(char) * 12);
strcpy(s1_name, "小刚");
strcpy(s2_name, "小晓");
strcpy(s3_name, "小晓");
strcpy(s4_name, "小红");
strcpy(s5_name, "xiao");
strcpy(s6_name, "xiao");
char *s1_sex, *s2_sex, *s3_sex, *s4_sex, *s5_sex, *s6_sex;
s1_sex = (char *)malloc(sizeof(char) * 4);
s2_sex = (char *)malloc(sizeof(char) * 4);
s3_sex = (char *)malloc(sizeof(char) * 4);
s4_sex = (char *)malloc(sizeof(char) * 4);
s5_sex = (char *)malloc(sizeof(char) * 4);
s6_sex = (char *)malloc(sizeof(char) * 4);
strcpy(s1_sex, "男");
strcpy(s2_sex, "男");
strcpy(s3_sex, "女");
strcpy(s4_sex, "女");
strcpy(s5_sex, "女");
strcpy(s6_sex, "女");
Student s1 = {s1_id, s1_sex, s1_name};
Student s2 = {s2_id, s2_sex, s2_name};
Student s3 = {s3_id, s3_sex, s3_name};
Student s4 = {s4_id, s4_sex, s4_name};
Student s5 = {s5_id, s5_sex, s5_name};
Student s6 = {s6_id, s6_sex, s6_name};
ListInsert(list, 1, s1);
ListInsert(list, 2, s2);
ListInsert(list, 3, s3);
ListInsert(list, 4, s4);
ListInsert(list, 5, s5);
ListInsert(list, 6, s6);
printf("\n输出线性表:\n");
for(int i = 0; i < list.length; i++) {
printf("学号:");
for(int j = 0; j < 10; j++) {
printf("%d", *(list.elem[i].s_id + j));
}
printf(", 姓名:%s, 性别:%s\n", list.elem[i].s_name, list.elem[i].s_sex);
}
printf("请选择操作(i: 添加, d: 删除, g: 查看, 其它键子退出):");
char op;
scanf("%c", &op);
if(op == 'd') {
int delete_m;
printf("请输入要删除 Student 的位置:");
scanf("%d", &delete_m);
Student delete_s;
int delect_bool = ListDelete(list, delete_m, delete_s);
if(delect_bool == 0) {
printf("填写错误,请重新运行!\n");
return 1;
}
printf("删除的 Student:学号:");
for(int i = 0; i < 10; i++) {
printf("%d", *(delete_s.s_id + i));
}
printf(", 姓名:%s, 性别:%s\n", delete_s.s_name, delete_s.s_sex);
printf("\n查看删除后的线性表:\n");
for(int i = 0; i < list.length; i++) {
printf("学号:");
for(int j = 0; j < 10; j++) {
printf("%d", *(list.elem[i].s_id + j));
}
printf(", 姓名:%s, 性别:%s\n", list.elem[i].s_name, list.elem[i].s_sex);
}
system("pause");
} else if(op == 'i') {
Student insert_s;
int insert_m;
printf("请输入要添加 Student 的位置:");
scanf("%d", &insert_m);
printf("\n请输入要添加 Student 的学号:");
int insert_id[10];
scanf("%d", insert_id);
insert_s.s_id = insert_id;
printf("\n请输入要添加 Student 的名字:");
char insert_name[12];
scanf("%s", insert_name);
insert_s.s_name = insert_name;
printf("\n请输入要添加 Student 的性别(男 或者 女):");
char insert_sex[12];
scanf("%s", insert_sex);
insert_s.s_sex = insert_sex;
int insert_bool = ListInsert(list, insert_m, insert_s);
if(insert_bool == 0) {
printf("填写错误,请重新运行!\n");
return 1;
}
printf("\n查看添加的线性表:\n");
for(int i = 0; i < list.length; i++) {
printf("学号:");
for(int j = 0; j < 10; j++) {
printf("%d", *(list.elem[i].s_id + j));
}
printf(", 姓名:%s, 性别:%s\n", list.elem[i].s_name, list.elem[i].s_sex);
}
system("pause");
} else if(op == 'g') {
printf("请选择操作(gn: 通过名字查看, gi: 通过学号查看, 其它键子退出):");
char g_op[1];
scanf("%s", g_op);
if(strcmp(g_op, "gn") == 0) {
printf("\n请输入要查找 Student 的名字:");
char get_name[12];
scanf("%s", get_name);
printf("%s 的位置为:", get_name);
for(int i = 0; i < GetStudentNumber(list, get_name); i++) {
printf("%d, ", GetStudentNumberByName(list, get_name)[i]);
}
printf("\n");
system("pause");
} else if(strcmp(g_op, "gi") == 0) {
printf("\n请输入要查找 Student 的学号:");
int get_id[10];
for(int i = 0; i < 10; i++) {
scanf("%d", &get_id[i]);
}
printf("位置为:%d\n", GetStudentNumberById(list, get_id));
system("pause");
}
}
}
int InitList(SqList &L) {
L.elem = (Student *)malloc(sizeof(Student) * LIST_INIT_SIZE);
if(!L.elem) {
return 0;
}
L.length = 0;
L.listSize = LIST_INIT_SIZE;
return 1;
}
int GetStudentNumber(SqList L, char *name) {
int j = 0;
for(int i = 0; i < L.length; i++) {
if(strcmp(L.elem[i].s_name, name) == 0) {
j++;
}
}
return j;
}
int *GetStudentNumberByName(SqList L, char *name) {
int getStuNumber[GetStudentNumber(L, name)];
int j = 0;
for(int i = 0; i < L.length; i++) {
if(strcmp(L.elem[i].s_name, name) == 0) {
getStuNumber[j] = i + 1;
j++;
}
}
return getStuNumber;
}
int GetStudentNumberById(SqList L, int *id) {
for(int i = 0; i < L.length; i++) {
int z = 0;
for(int j = 0; j < 10; j++) {
if(L.elem[i].s_id[j] == id[j]) {
z++;
}
}
if(z == 10) {
return i + 1;
}
}
return 0;
}
int ListInsert(SqList &L, int i, Student s) {
if(i < 1 || i > L.length + 1) {
return 0;
}
if (L.length >= L.listSize) {
Student *newElem = (Student *)realloc(L.elem, sizeof(Student) * (L.listSize + LIST_INCREMENT));
if(!newElem) {
return 0;
}
L.elem = newElem;
L.listSize += LIST_INCREMENT;
}
Student *q, *p;
p = &(L.elem[L.length - 1]);
q = &(L.elem[i-1]);
for(p; p >= q; p--) {
*(p + 1) = *p;
}
*q = s;
L.length++;
return 1;
}
int ListDelete(SqList &L, int i, Student &s) {
if (i < 1 || i > L.length) {
return 0;
}
Student *q, *p;
p = &(L.elem[i - 1]);
s = *p;
q = &(L.elem[L.length - 1]);
for(p; p <= q; p++) {
*p = *(p + 1);
}
L.length--;
return 1;
}
结果
添加
删除
通过名字查找
通过学号查找