Data_Structure/作業/unit4/slist_test_file.c
2025-01-20 21:30:53 +08:00

189 lines
4.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
long int id;
char name[10];
double score;
struct Node *next;
};
struct LinkedList {
struct Node *head;
struct Node *tail;
};
struct Node* createNode() {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("記憶體分配失敗\n");
exit(1);
}
newNode->next = NULL;
return newNode;
}
void insertNode(struct LinkedList* list, long int id, const char* name, double score) {
struct Node* newNode = createNode();
newNode->id = id;
strncpy(newNode->name, name, 9);
newNode->name[9] = '\0';
newNode->score = score;
if (list->head == NULL) {
list->head = newNode;
list->tail = newNode;
} else {
list->tail->next = newNode;
list->tail = newNode;
}
}
void printList(struct LinkedList* list) {
struct Node* current = list->head;
while (current != NULL) {
printf("學號: %ld, 姓名: %s, 分數: %.2f\n", current->id, current->name, current->score);
current = current->next;
}
}
void freeList(struct LinkedList* list) {
struct Node* current = list->head;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
list->head = NULL;
list->tail = NULL;
}
void readFromFile(struct LinkedList* list, const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("無法打開檔案 %s\n", filename);
return;
}
long int id;
char name[10];
double score;
while (fscanf(file, "%ld %9s %lf", &id, name, &score) == 3) {
insertNode(list, id, name, score);
}
fclose(file);
}
void saveToFile(struct LinkedList* list, const char* filename) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("無法打開檔案 %s 進行寫入\n", filename);
return;
}
struct Node* current = list->head;
while (current != NULL) {
fprintf(file, "%ld %s %.2f\n", current->id, current->name, current->score);
current = current->next;
}
fclose(file);
printf("資料已成功儲存到 %s\n", filename);
}
void inputFromKeyboard(struct LinkedList* list) {
long int id;
char name[10];
double score;
printf("請輸入學號: ");
scanf("%ld", &id);
printf("請輸入姓名 (最多9個字符): ");
scanf("%9s", name);
printf("請輸入分數: ");
scanf("%lf", &score);
insertNode(list, id, name, score);
printf("資料已成功添加到串列中\n");
}
void deleteNode(struct LinkedList* list, long int id) {
struct Node* current = list->head;
struct Node* prev = NULL;
// 如果要刪除的是頭節點
if (current != NULL && current->id == id) {
list->head = current->next;
free(current);
if (list->head == NULL) {
list->tail = NULL; // 如果刪除後列表為空,更新 tail
}
printf("學號為 %ld 的資料已刪除\n", id);
return;
}
// 搜尋要刪除的節點
while (current != NULL && current->id != id) {
prev = current;
current = current->next;
}
// 如果沒找到要刪除的節點
if (current == NULL) {
printf("找不到學號為 %ld 的資料\n", id);
return;
}
// 刪除節點
prev->next = current->next;
if (current == list->tail) {
list->tail = prev; // 如果刪除的是尾節點,更新 tail
}
free(current);
printf("學號為 %ld 的資料已刪除\n", id);
}
int main() {
struct LinkedList list = {NULL, NULL};
int choice;
long int id_to_delete;
const char* filename = "list.txt";
readFromFile(&list, filename);
do {
printf("\n1. 顯示所有資料\n");
printf("2. 從鍵盤輸入新資料\n");
printf("3. 刪除特定資料\n");
printf("4. 儲存並退出\n");
printf("請選擇操作 (1-4): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\n所有資料:\n");
printList(&list);
break;
case 2:
inputFromKeyboard(&list);
break;
case 3:
printf("請輸入要刪除的學號: ");
scanf("%ld", &id_to_delete);
deleteNode(&list, id_to_delete);
break;
case 4:
saveToFile(&list, filename);
break;
default:
printf("無效的選擇,請重新輸入\n");
}
} while (choice != 4);
freeList(&list);
return 0;
}