#include <stdio.h>
#include <stdlib.h>
typedef struct {
int num;
char name[80];
char sex[2];
int age;
int score;
} Student;
typedef struct {
Student students[7];
int length;
} StudentList;
void InsertStudent(StudentList *studentList, Student student, int i) {
if (studentList->length == 7) {
printf("there is no more capacity!\n");
return;
}
if (i < 1 || i > studentList->length + 1) {
printf("invalid insert position!\n");
return;
}
int j = 0;
for (j = studentList->length; j >= i; j--) {
studentList->students[j] = studentList->students[j - 1];
}
studentList->students[i - 1] = student;
studentList->length++;
}
void DeleteStudent(StudentList *studentList, int i) {
if (i < 1 || i > studentList->length) {
printf("invalid insert position!\n");
return;
}
int j = 0;
for (j = i; j <= studentList->length; j++) {
studentList->students[j - 1] = studentList->students[j];
}
studentList->length--;
}
int LocateStudents(StudentList *studentList, int num) {
int i = 0;
while (i < studentList->length && studentList->students[i].num != num) {
i++;
}
if (i < studentList->length)
return i + 1;
else
return 0;
}
void display(StudentList *studentList) {
int i = 0;
for (i = 0; i < studentList->length; i++) {
printf("num=%d,name=%s,age=%d,score=%d,sex=%s\n",
studentList->students[i].num, studentList->students[i].name,
studentList->students[i].age, studentList->students[i].score,
studentList->students[i].sex);
}
}
int main(void) {
StudentList studentList;
studentList.length = 0; //给变量赋值时才会分配内存
Student student1 = { 1, "zhangrenyang1", "m", 1, 1 };
Student student2 = { 2, "zhangrenyang2", "m", 2, 2 };
Student student3 = { 3, "zhangrenyang3", "m", 3, 3 };
Student student4 = { 4, "zhangrenyang4", "m", 4, 4 };
Student student5 = { 5, "zhangrenyang5", "m", 5, 5 };
Student student6 = { 6, "zhangrenyang6", "m", 6, 6 };
Student student7 = { 7, "zhangrenyang7", "m", 7, 7 };
Student student8 = { 8, "zhangrenyang8", "m", 8, 8 };
InsertStudent(&studentList, student1, 1);
InsertStudent(&studentList, student2, 2);
InsertStudent(&studentList, student3, 3);
InsertStudent(&studentList, student4, 4);
InsertStudent(&studentList, student5, 5);
InsertStudent(&studentList, student6, 6);
InsertStudent(&studentList, student7, 7);
InsertStudent(&studentList, student8, 8);
display(&studentList);
printf("=============================================\n");
DeleteStudent(&studentList, 3);
display(&studentList);
printf("=============================================\n");
printf("the index of 5 is %d\n",LocateStudents(&studentList, 5));
return 0;
}
结果
there is no more capacity!
num=1,name=zhangrenyang1,age=1,score=1,sex=m
num=2,name=zhangrenyang2,age=2,score=2,sex=m
num=3,name=zhangrenyang3,age=3,score=3,sex=m
num=4,name=zhangrenyang4,age=4,score=4,sex=m
num=5,name=zhangrenyang5,age=5,score=5,sex=m
num=6,name=zhangrenyang6,age=6,score=6,sex=m
num=7,name=zhangrenyang7,age=7,score=7,sex=m
=============================================
num=1,name=zhangrenyang1,age=1,score=1,sex=m
num=2,name=zhangrenyang2,age=2,score=2,sex=m
num=4,name=zhangrenyang4,age=4,score=4,sex=m
num=5,name=zhangrenyang5,age=5,score=5,sex=m
num=6,name=zhangrenyang6,age=6,score=6,sex=m
num=7,name=zhangrenyang7,age=7,score=7,sex=m
=============================================
the index of 5 is 4