fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NUM_SUBJECTS 5 // จำนวนวิชา
  6. #define NUM_STUDENTS 5 // จำนวนนักเรียน
  7.  
  8. struct Student {
  9. char name[50];
  10. char subjects[NUM_SUBJECTS][50];
  11. float grades[NUM_SUBJECTS];
  12. struct Student *next;
  13. };
  14.  
  15. // ฟังก์ชันสร้าง Node ใหม่
  16. struct Student* createNode() {
  17. struct Student* newNode = (struct Student*)malloc(sizeof(struct Student));
  18. printf("ป้อนชื่อนักเรียน: ");
  19. scanf("%s", newNode->name);
  20. for (int i = 0; i < NUM_SUBJECTS; i++) {
  21. printf("ป้อนชื่อวิชาที่ %d: ", i+1);
  22. scanf("%s", newNode->subjects[i]);
  23. printf("ป้อนคะแนน: ");
  24. scanf("%f", &newNode->grades[i]);
  25. }
  26. newNode->next = NULL;
  27. return newNode;
  28. }
  29.  
  30. // ฟังก์ชันเพิ่ม Node เข้าไปใน Linked List
  31. void addNode(struct Student** head, struct Student* newNode) {
  32. newNode->next = *head;
  33. *head = newNode;
  34. }
  35.  
  36. // ฟังก์ชันแสดงข้อมูลนักเรียนทั้งหมด
  37. void printList(struct Student* head) {
  38. struct Student* current = head;
  39. while (current != NULL) {
  40. printf("\nชื่อนักเรียน: %s\n", current->name);
  41. for (int i = 0; i < NUM_SUBJECTS; i++) {
  42. printf(" %s: %.2f\n", current->subjects[i], current->grades[i]);
  43. }
  44. current = current->next;
  45. }
  46. }
  47.  
  48. // ฟังก์ชันปลดปล่อยหน่วยความจำ
  49. void freeList(struct Student* head) {
  50. struct Student* current = head;
  51. while (current != NULL) {
  52. struct Student* temp = current;
  53. current = current->next;
  54. free(temp);
  55. }
  56. }
  57.  
  58. int main() {
  59. struct Student* head = NULL;
  60. struct Student* newNode;
  61.  
  62. // สร้างและเพิ่ม Node ทั้งหมด
  63. for (int i = 0; i < NUM_STUDENTS; i++) {
  64. newNode = createNode();
  65. addNode(&head, newNode);
  66. }
  67.  
  68. // แสดงผลข้อมูลนักเรียนทั้งหมด
  69. printList(head);
  70.  
  71. // ปลดปล่อยหน่วยความจำ
  72. freeList(head);
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
ป้อนชื่อนักเรียน: ป้อนชื่อวิชาที่ 1: ป้อนคะแนน: ป้อนชื่อวิชาที่ 2: ป้อนคะแนน: ป้อนชื่อวิชาที่ 3: ป้อนคะแนน: ป้อนชื่อวิชาที่ 4: ป้อนคะแนน: ป้อนชื่อวิชาที่ 5: ป้อนคะแนน: ป้อนชื่อนักเรียน: ป้อนชื่อวิชาที่ 1: ป้อนคะแนน: ป้อนชื่อวิชาที่ 2: ป้อนคะแนน: ป้อนชื่อวิชาที่ 3: ป้อนคะแนน: ป้อนชื่อวิชาที่ 4: ป้อนคะแนน: ป้อนชื่อวิชาที่ 5: ป้อนคะแนน: ป้อนชื่อนักเรียน: ป้อนชื่อวิชาที่ 1: ป้อนคะแนน: ป้อนชื่อวิชาที่ 2: ป้อนคะแนน: ป้อนชื่อวิชาที่ 3: ป้อนคะแนน: ป้อนชื่อวิชาที่ 4: ป้อนคะแนน: ป้อนชื่อวิชาที่ 5: ป้อนคะแนน: ป้อนชื่อนักเรียน: ป้อนชื่อวิชาที่ 1: ป้อนคะแนน: ป้อนชื่อวิชาที่ 2: ป้อนคะแนน: ป้อนชื่อวิชาที่ 3: ป้อนคะแนน: ป้อนชื่อวิชาที่ 4: ป้อนคะแนน: ป้อนชื่อวิชาที่ 5: ป้อนคะแนน: ป้อนชื่อนักเรียน: ป้อนชื่อวิชาที่ 1: ป้อนคะแนน: ป้อนชื่อวิชาที่ 2: ป้อนคะแนน: ป้อนชื่อวิชาที่ 3: ป้อนคะแนน: ป้อนชื่อวิชาที่ 4: ป้อนคะแนน: ป้อนชื่อวิชาที่ 5: ป้อนคะแนน: 
ชื่อนักเรียน: 
  : 0.00
  : 0.00
  : 0.00
  : 0.00
  : 0.00

ชื่อนักเรียน: 
  : 0.00
  : 0.00
  : 0.00
  : 0.00
  : 0.00

ชื่อนักเรียน: 
  : 0.00
  : 0.00
  : 0.00
  : 0.00
  : 0.00

ชื่อนักเรียน: 
  : 0.00
  : 0.00
  : 0.00
  : 0.00
  : 0.00

ชื่อนักเรียน: 
  : 0.00
  : 0.00
  : 0.00
  : 0.00
  : 0.00