一、实验目的
用线性表对十个同学的信息进行存储,并设计对应的程序完成插入数据、删除数据、查找数据这些操作。
二、实验要求
1.设计线性表对数据进行存储
2.完成查询的程序
3.完成插入的程序
4.完成删除的程序
三、数据
小白 女 2018210214033 18 15372091138 aa
小刘 女 2018210214010 19 15378325138 bb
小张 女 2018210214089 19 15189030138 cc
小邵 女 2018210214045 17 15372078456 dd
小马 女 2018210214012 19 15372091138 ee
小蔡 女 2018210214011 18 15372091112 ff
小邵 女 2018210214010 19 15323081156 gg
小王 女 2018210214034 19 15372091193 hh
小赵 女 2018210214036 20 15378091147 ii
小明 女 2018210214037 19 15377900026 jj
四、代码实现
#pragma warning(disable : 4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Students{
char name[20];
char sex[5];
char id[20];
int age;
char phoneNumber[20];
char en[5];
};
struct S {
struct Students data[100] = {NULL};
int length = 0;
};
typedef struct Students* stu;
typedef struct S* Stu;//让指针Stu与S平级
Stu CreatPtrl(Stu S,FILE *fp) {
S->length = 10;
for (int i = 0;i < S->length;i++) {
fscanf(fp, "%s", S->data[i].name);
fscanf(fp, "%s", S->data[i].sex);
fscanf(fp, "%s", S->data[i].id);
fscanf(fp, "%d", &S->data[i].age);
fscanf(fp, "%s", S->data[i].phoneNumber);
fscanf(fp, "%s", S->data[i].en);
}
return S;
}
stu Research(Stu S,char* strName) {
for (int i = 0;i < S->length;i++) {
if (strcmp(strName, S->data[i].name) == 0) {
return &(S->data[i]);
}
}
return NULL;
}
void Insert(Stu S, stu tS) {
(S->length)++;
strcpy(S->data[(S->length) - 1].name, tS->name);
strcpy(S->data[(S->length) - 1].sex, tS->sex);
strcpy(S->data[(S->length) - 1].id, tS->id);
S->data[(S->length) - 1].age = tS->age;
strcpy(S->data[(S->length) - 1].phoneNumber, tS->phoneNumber);
strcpy(S->data[(S->length) - 1].en, tS->en);
}
void Del(Stu S,char* tName) {
int index = -1;
for (int i = 0;i < S->length;i++) {
if (strcmp(S->data[i].name, tName) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("人名不存在\n");
return;
}
for (int i = index;i < (S->length)-1;i++) {
S->data[i] = S->data[i + 1];
}
(S->length)--;
}
void show(Stu S) {
for (int i = 0;i < S->length;i++) {
printf("%s ", S->data[i].name);
printf("%s ", S->data[i].sex);
printf("%s ", S->data[i].id);
printf("%d ", S->data[i].age);
printf("%s ", S->data[i].phoneNumber);
printf("%s\n", S->data[i].en);
}
}
int main() {
Stu Ptrl;
Ptrl = (Stu)malloc(sizeof(struct S));
FILE* fp = fopen("C:\\Users\\ZYH\\Desktop\\data1.txt", "r");
Ptrl = CreatPtrl(Ptrl, fp);
printf("若查询请输入1\n");
printf("若插入请输入2\n");
printf("若删除请输入3\n");
printf("若退出请输入4\n");
while (1) {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("亲输入查找到的人名:\n");
char reName[20];
scanf("%s", reName);
if (Research(Ptrl, reName) == NULL) {
printf("信息不存在\n");
}
else {
stu tStu;
tStu = (stu)malloc(sizeof(struct Students));
tStu = Research(Ptrl, reName);
printf("%s的信息如下: \n", reName);
printf("性别为 %s\n", tStu->sex);
printf("学号为 %s\n", tStu->id);
printf("年龄为 %d\n", tStu->age);
printf("手机号为 %s\n", tStu->phoneNumber);
printf("额外信息为 %s\n", tStu->en);
free(tStu);
}
break;
case 2:
stu tStu;
tStu = (stu)malloc(sizeof(struct Students));
printf("请输入插入的名字:");
scanf("%s", tStu->name);
printf("请输入其性别:");
scanf("%s", tStu->sex);
printf("请输入其学号:");
scanf("%s", tStu->id);
printf("请输入其年龄:");
scanf("%d", &tStu->age);
printf("请输入其手机号:");
scanf("%s", tStu->phoneNumber);
printf("请输入其他消息:");
scanf("%s", tStu->en);
Insert(Ptrl, tStu);
printf("当前全员信息为:\n");
show(Ptrl);
break;
case 3:
printf("请输入要删除的人名:\n");
char tName[20];
scanf("%s", tName);
Del(Ptrl, tName);
printf("当前全员信息为:\n");
show(Ptrl);
break;
case 4:
return -1;
}
}
}
五、实现效果