用c语言有3个学生,包括学号、姓名、分数,定义结构体类型数组存学生信息,使用链表输入输出学生信息。
时间: 2024-06-13 10:09:51 浏览: 158
以下是C语言的代码示例,实现了定义学生信息的结构体类型,包括:学号、姓名、专业、班级、3门成绩,并使用链表输入输出学生信息。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3 // 定义符号常量N为3
// 定义学生信息的结构体类型
struct student {
char id[10]; // 学号
char name[20]; // 姓名
char major[20]; // 专业
char class[10]; // 班级
float score[3]; // 3门成绩
};
// 定义链表节点的结构体类型
struct node {
struct student data; // 学生信息
struct node *next; // 指向下一个节点的指针
};
// 创建链表节点
struct node *create_node(struct student data) {
struct node *p = (struct node *)malloc(sizeof(struct node));
if (p == NULL) {
printf("Error: memory allocation failed!n");
exit(1);
}
p->data = data;
p->next = NULL;
return p;
}
// 插入节点到链表尾部
void insert_node(struct node **head, struct node *p) {
if (*head == NULL) {
*head = p;
} else {
struct node *q = *head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
// 从链表中删除指定学号的节点
void delete_node(struct node **head, char *id) {
struct node *p = *head;
struct node *q = NULL;
while (p != NULL && strcmp(p->data.id, id) ! 0) {
q = p;
p = p->next;
}
if (p == NULL) {
printf("Error: student with id %s not found!\n", id);
return;
}
if (q == NULL) {
*head = p->next;
} else {
q->next = p->next;
}
free(p);
}
// 修改链表中指定学号的学生信息
void modify_node(struct node *head, char *id) {
struct node *p = head;
while (p != NULL && strcmp(p->data.id, id) != 0) {
p = p->next;
}
if (p == NULL) {
printf("Error: student with id %s not found!\n", id);
return;
}
printf("Please enter the new information of the student:\n");
printf("id: ");
scanf("%s", p->data.id);
printf("name: ");
scanf("%s", p->data.name);
printf("major: ");
scanf("%s", p->data.major);
printf("class: ");
scanf("%s", p->data.class);
printf("score1: ");
scanf("%f", &p->data.score[0]);
printf("score2: ");
scanf("%f", &p->data.score[1]);
printf("score3: ");
scanf("%f", &p->data.score[2]);
}
// 输出链表中所有学生信息
void print_list(struct node *head) {
printf("id\tname\tmajor\tclass\tscore1\tscore2\tscore3\n");
struct node *p = head;
while (p != NULL) {
printf("%s\t%s\t%s\t%s\t%.1f\t%.1f\t%.1f\n", p->data.id, p->data.name, p->data.major, p->data.class, p->data.score[0], p->data.score[1], p->data.score[2]);
p = p->next;
}
}
int main() {
struct node *head = NULL; // 定义链表头指针
int choice;
while (1) {
printf("1. Insert a student\n");
printf("2. Delete a student\n");
printf("3. Modify a student\n");
printf("4. Print all students\n");
printf("5. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
struct student s;
printf("Please enter the information of the student:\n");
printf("id: ");
scanf("%s", s.id);
printf("name: ");
scanf("%s", s.name);
printf("major: ");
scanf("%s", s.major);
printf("class: ");
scanf("%s", s.class);
printf("score1: ");
scanf("%f", &s.score[0]);
printf("score2: ");
scanf("%f", &s.score[1]);
printf("score3: ");
scanf("%f", &s.score[2]);
struct node *p = create_node(s);
insert_node(&head, p);
break;
}
case 2: {
char id[10];
printf("Please enter the id of the student to be deleted: ");
scanf("%s", id);
delete_node(&head, id);
break;
}
case 3: {
char id[10];
printf("Please enter the id of the student to be modified: ");
scanf("%s", id);
modify_node(head, id);
break;
}
case 4: {
print_list(head);
break;
}
case 5: {
exit(0);
}
default: {
printf("Error: invalid choice!\n");
break;
}
}
}
return 0;
}
```
阅读全文