Q1 題目說明一

• Please create a C language program to build a singly linked list with the following data. Read the data for the list from the 'students.txt' file and design a main menu with five options:

• 請以C語言程式,建立一個下列資料的單向串列, 串列的資料請讀取 students.txt檔,並且設計主選單的選項如下:

Untitled

輸入資料 /輸出結果

Untitled

Input/Output Result

Untitled

students.txt:

101 85 90 102 92 88 103 78 75 104 88 92 105 95 89

students.txt:

101   85       90
102   92       88
103   78       75
104   88       92
105   95       89

students.txt

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Include the string.h header for strcpy

// Define the student structure
struct Student {
    int id;
    int math;
    int chinese;
    struct Student* next;
};

// Initialize the start pointer
struct Student* start = NULL;

// Function to display all students and their details
void listStudents() {
    struct Student* current = start;
    if (current == NULL) {
        printf("No data found.\\n");
        return;
    }

    printf("Student ID\\tMath\\tChinese\\n");
    while (current != NULL) {
        printf("%d\\t\\t%d\\t%d\\n", current->id, current->math, current->chinese);
        current = current->next;
    }
}

// Function to add a new product
void addStudenth() {
    struct Student* std = (struct Student*)malloc(sizeof(struct Student));
    if (std == NULL) {
        printf("Memory allocation failed.\\n");
        return;
    }

    printf("Enter Student ID: ");
    scanf("%d", &std->id);
    printf("Enter Student Math Score: ");
    scanf("%d", &std->math);
    printf("Enter Student Chinese Score: ");
    scanf("%d", &std->chinese);

    std->next = start;
    start = std;
    printf("Student added successfully.\\n");
}

void invertArray() {
    struct Student* current = start;
    struct Student* previous = NULL;
    struct Student* next = NULL;

    while (current != NULL) {
        next = current->next; // Store the next node
        current->next = previous; // Change the next of the current node

        // Move pointers one position ahead
        previous = current;
        current = next;
    }

    start = previous; // Update the start to the last node
}

void addStudentb() {
    struct Student* std = (struct Student*)malloc(sizeof(struct Student));
    if (std == NULL) {
        printf("Memory allocation failed.\\n");
        return;
    }

    printf("Enter Student ID: ");
    scanf("%d", &std->id);
    printf("Enter Student Math Score: ");
    scanf("%d", &std->math);
    printf("Enter Student Chinese Score: ");
    scanf("%d", &std->chinese);

    std->next = NULL; // Set next to NULL for the last student

    if (start == NULL) {
        start = std; // If the list is empty, set start to the new student
    } else {
        struct Student* current = start;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = std; // Add the new student to the end of the list
    }

    printf("Student added successfully.\\n");
}

void deleteStudent() {
    int id;
    printf("Enter the Student ID to delete: ");
    scanf("%d", &id);

    struct Student* current = start;
    struct Student* prev = NULL;

    while (current != NULL && current->id != id) {
        prev = current;
        current = current->next;
    }

    if (current == NULL) {
        printf("Product not found.\\n");
        return;
    }

    if (prev == NULL) {
        start = current->next;
    } else {
        prev->next = current->next;
    }

    free(current);
    printf("Student with ID %d deleted successfully.\\n", id);
}

int main() {
    FILE *file;
    file = fopen("students.txt", "r");

    if (file == NULL) {
        printf("The file can't open\\n");
        return 1;
    }

    char line[100];  // Buffer to read lines from the file

    while (fgets(line, sizeof(line), file)) {
        struct Student* std = (struct Student*)malloc(sizeof(struct Student));
        if (std == NULL) {
            printf("Memory allocation failed.\\n");
            return 1;
        }

        // Parse the line to extract student data
        if (sscanf(line, "%d %d %d", &std->id, &std->math, &std->chinese) == 3) {
            std->next = start;
            start = std;
        }
    }

    fclose(file);
    invertArray();

    // Main program area
    int choice;
    while (1) {
        printf("========Student Management System========\\n");
        printf("1. Display all students & details \\n");
        printf("2. Add new student head of the array \\n");
        printf("3. Add new student back of the array \\n");
        printf("4. Delete Student based on ID \\n");
        printf("5. Invert the array \\n");
        printf("0. Exit the program \\n");
        printf("Please enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                listStudents();
                break;
            case 2:
                addStudenth();
                break;
            case 3:
                addStudentb();
                break;
            case 4:
            	deleteStudent();
                break;
			case 5:
			    invertArray();
			    printf("Array inverted.\\n");
			    break;
            case 0:
                exit(0);
                break;
            default:
                printf("Wrong Choice.\\n");
                break;
        }
    }
    return 0;
}