#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_SUBJECTS 5 // จำนวนวิชา
#define NUM_STUDENTS 5 // จำนวนนักเรียน
struct Student {
char name[50];
char subjects[NUM_SUBJECTS][50];
float grades[NUM_SUBJECTS];
struct Student *next;
};
// ฟังก์ชันสร้าง Node ใหม่
struct Student* createNode() {
struct Student
* newNode
= (struct Student
*)malloc(sizeof(struct Student
)); scanf("%s", newNode
->name
); for (int i = 0; i < NUM_SUBJECTS; i++) {
printf("ป้อนชื่อวิชาที่ %d: ", i
+1); scanf("%s", newNode
->subjects
[i
]); scanf("%f", &newNode
->grades
[i
]); }
newNode->next = NULL;
return newNode;
}
// ฟังก์ชันเพิ่ม Node เข้าไปใน Linked List
void addNode(struct Student** head, struct Student* newNode) {
newNode->next = *head;
*head = newNode;
}
// ฟังก์ชันแสดงข้อมูลนักเรียนทั้งหมด
void printList(struct Student* head) {
struct Student* current = head;
while (current != NULL) {
printf("\nชื่อนักเรียน: %s\n", current
->name
); for (int i = 0; i < NUM_SUBJECTS; i++) {
printf(" %s: %.2f\n", current
->subjects
[i
], current
->grades
[i
]); }
current = current->next;
}
}
// ฟังก์ชันปลดปล่อยหน่วยความจำ
void freeList(struct Student* head) {
struct Student* current = head;
while (current != NULL) {
struct Student* temp = current;
current = current->next;
}
}
int main() {
struct Student* head = NULL;
struct Student* newNode;
// สร้างและเพิ่ม Node ทั้งหมด
for (int i = 0; i < NUM_STUDENTS; i++) {
newNode = createNode();
addNode(&head, newNode);
}
// แสดงผลข้อมูลนักเรียนทั้งหมด
printList(head);
// ปลดปล่อยหน่วยความจำ
freeList(head);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBOVU1fU1VCSkVDVFMgNSAvLyDguIjguLPguJnguKfguJnguKfguLTguIrguLIKI2RlZmluZSBOVU1fU1RVREVOVFMgNSAvLyDguIjguLPgu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nZvaWQgYWRkTm9kZShzdHJ1Y3QgU3R1ZGVudCoqIGhlYWQsIHN0cnVjdCBTdHVkZW50KiBuZXdOb2RlKSB7CiAgICBuZXdOb2RlLT5uZXh0ID0gKmhlYWQ7CiAgICAqaGVhZCA9IG5ld05vZGU7Cn0KCi8vIOC4n+C4seC4h+C4geC5jOC4iuC4seC4meC5geC4quC4lOC4h+C4guC5ieC4reC4oeC4ueC4peC4meC4seC4geC5gOC4o+C4teC4ouC4meC4l+C4seC5ieC4h+C4q+C4oeC4lAp2b2lkIHByaW50TGlzdChzdHJ1Y3QgU3R1ZGVudCogaGVhZCkgewogICAgc3RydWN0IFN0dWRlbnQqIGN1cnJlbnQgPSBoZWFkOwogICAgd2hpbGUgKGN1cnJlbnQgIT0gTlVMTCkgewogICAgICAgIHByaW50ZigiXG7guIrguLfguYjguK3guJnguLHguIHguYDguKPguLXguKLguJk6ICVzXG4iLCBjdXJyZW50LT5uYW1lKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IE5VTV9TVUJKRUNUUzsgaSsrKSB7CiAgICAgICAgICAgIHByaW50ZigiICAlczogJS4yZlxuIiwgY3VycmVudC0+c3ViamVjdHNbaV0sIGN1cnJlbnQtPmdyYWRlc1tpXSk7CiAgICAgICAgfQogICAgICAgIGN1cnJlbnQgPSBjdXJyZW50LT5uZXh0OwogICAgfQp9CgovLyDguJ/guLHguIfguIHguYzguIrguLHguJnguJvguKXguJTguJvguKXguYjguK3guKLguKvguJnguYjguKfguKLguITguKfguLLguKHguIjguLMKdm9pZCBmcmVlTGlzdChzdHJ1Y3QgU3R1ZGVudCogaGVhZCkgewogICAgc3RydWN0IFN0dWRlbnQqIGN1cnJlbnQgPSBoZWFkOwogICAgd2hpbGUgKGN1cnJlbnQgIT0gTlVMTCkgewogICAgICAgIHN0cnVjdCBTdHVkZW50KiB0ZW1wID0gY3VycmVudDsKICAgICAgICBjdXJyZW50ID0gY3VycmVudC0+bmV4dDsKICAgICAgICBmcmVlKHR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