Data_Structure/作業/unit4/slist_test_file.c

189 lines
4.5 KiB
C
Raw Normal View History

2025-01-20 21:30:53 +08:00
#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("<EFBFBD>O<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>t<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\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("<EFBFBD>Ǹ<EFBFBD>: %ld, <20>m<EFBFBD>W: %s, <20><><EFBFBD><EFBFBD>: %.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("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>}<7D>ɮ<EFBFBD> %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("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>}<7D>ɮ<EFBFBD> %s <20>i<EFBFBD><69><EFBFBD>g<EFBFBD>J\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("<EFBFBD><EFBFBD><EFBFBD>Ƥw<EFBFBD><EFBFBD><EFBFBD>\<EFBFBD>x<EFBFBD>s<EFBFBD><EFBFBD> %s\n", filename);
}
void inputFromKeyboard(struct LinkedList* list) {
long int id;
char name[10];
double score;
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>Ǹ<EFBFBD>: ");
scanf("%ld", &id);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>m<EFBFBD>W (<28>̦h9<68>Ӧr<D3A6><72>): ");
scanf("%9s", name);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%lf", &score);
insertNode(list, id, name, score);
printf("<EFBFBD><EFBFBD><EFBFBD>Ƥw<EFBFBD><EFBFBD><EFBFBD>\<EFBFBD>K<EFBFBD>[<5B><><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43>\n");
}
void deleteNode(struct LinkedList* list, long int id) {
struct Node* current = list->head;
struct Node* prev = NULL;
// <20>p<EFBFBD>G<EFBFBD>n<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>Y<EFBFBD>`<60>I
if (current != NULL && current->id == id) {
list->head = current->next;
free(current);
if (list->head == NULL) {
list->tail = NULL; // <20>p<EFBFBD>G<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>šA<C5A1><41><EFBFBD>s tail
}
printf("<EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD> %ld <20><><EFBFBD><EFBFBD><EFBFBD>Ƥw<C6A4>R<EFBFBD><52>\n", id);
return;
}
// <20>j<EFBFBD>M<EFBFBD>n<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I
while (current != NULL && current->id != id) {
prev = current;
current = current->next;
}
// <20>p<EFBFBD>G<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>n<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I
if (current == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD> %ld <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n", id);
return;
}
// <20>R<EFBFBD><52><EFBFBD>`<60>I
prev->next = current->next;
if (current == list->tail) {
list->tail = prev; // <20>p<EFBFBD>G<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD>`<60>I<EFBFBD>A<EFBFBD><41><EFBFBD>s tail
}
free(current);
printf("<EFBFBD>Ǹ<EFBFBD><EFBFBD><EFBFBD> %ld <20><><EFBFBD><EFBFBD><EFBFBD>Ƥw<C6A4>R<EFBFBD><52>\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. <20><><EFBFBD>ܩҦ<DCA9><D2A6><EFBFBD><EFBFBD><EFBFBD>\n");
printf("2. <20>q<EFBFBD><71><EFBFBD>L<EFBFBD><4C><EFBFBD>J<EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD>\n");
printf("3. <20>R<EFBFBD><52><EFBFBD>S<EFBFBD>w<EFBFBD><77><EFBFBD><EFBFBD>\n");
printf("4. <20>x<EFBFBD>s<EFBFBD>ðh<C3B0>X\n");
printf("<EFBFBD>п<EFBFBD><EFBFBD>ܾާ@ (1-4): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\n<EFBFBD>Ҧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
printList(&list);
break;
case 2:
inputFromKeyboard(&list);
break;
case 3:
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>n<EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>: ");
scanf("%ld", &id_to_delete);
deleteNode(&list, id_to_delete);
break;
case 4:
saveToFile(&list, filename);
break;
default:
printf("<EFBFBD>L<EFBFBD>Ī<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܡA<EFBFBD>Э<EFBFBD><EFBFBD>s<EFBFBD><EFBFBD><EFBFBD>J\n");
}
} while (choice != 4);
freeList(&list);
return 0;
}