You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
pvk3wty2e 9fc3f499a9
Update README.md
2 years ago
README.md Update README.md 2 years ago

README.md

#include <stdio.h> #include <stdlib.h> #include <string.h>

// 结构体定义 typedef struct student { char *name; char *clas; char *num; double score1; double score2; double score3; double totscore; struct student *prev; // 指向前驱结点 struct student *next; // 指向后继结点 } student;

// 头结点和尾结点 struct student *head = NULL; struct student *tail = NULL;

// 函数声明 void addStudent(char *name, char *clas, char *num, double score1, double score2, double score3); void printStudents(); void freeMemory();

int main() { // 添加学生信息 addStudent("zhang", "Class 11", "1001", 99.5, 88.5, 89.5); addStudent("li", "Class 22", "1002", 77.9, 56.5, 87.5); addStudent("wang", "Class 11", "1003", 92.5, 99.0, 60.5);

// 打印学生信息
printStudents();

// 释放内存
freeMemory();

return 0;

}

// 添加学生信息到链表 void addStudent(char *name, char *clas, char *num, double score1, double score2, double score3) { // 分配内存并初始化学生结点 struct student *newStudent = (struct student *)malloc(sizeof(struct student)); newStudent->name = strdup(name); newStudent->clas = strdup(clas); newStudent->num = strdup(num); newStudent->score1 = score1; newStudent->score2 = score2; newStudent->score3 = score3; newStudent->totscore = score1 + score2 + score3; newStudent->prev = NULL; newStudent->next = NULL;

// 如果链表为空,设置头结点和尾结点
if (head == NULL) {
    head = newStudent;
    tail = newStudent;
} else {
    // 否则,将新结点添加到链表末尾
    tail->next = newStudent;
    newStudent->prev = tail;
    tail = newStudent;
}

}

// 打印学生信息 void printStudents() { struct student *current = head;

printf("学生信息:\n");
while (current != NULL) {
    printf("姓名: %s, 班级: %s, 学号: %s, 总分: %.2f\n", current->name, current->clas, current->num, current->totscore);
    current = current->next;
}
printf("\n");

}

// 释放内存 void freeMemory() { struct student *current = head; struct student *next;

while (current != NULL) {
    next = current->next;
    free(current->name);
    free(current->clas);
    free(current->num);
    free(current);
    current = next;
}

}