Data_Structure/作業/unit4/slist_test.c

78 lines
1.6 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 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 Node** head) {
struct Node* newNode = createNode();
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>Ǹ<EFBFBD>: ");
scanf("%ld", &(newNode->id));
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>m<EFBFBD>W (<28>̦h9<68>Ӧr<D3A6><72>): ");
scanf("%9s", newNode->name);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%lf", &(newNode->score));
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
printf("<EFBFBD><EFBFBD><EFBFBD>ƲK<EFBFBD>[<5B><><EFBFBD>\\n");
}
void printList(struct Node* head) {
struct Node* current = 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;
}
}
int main() {
struct Node* head = NULL;
char choice;
do {
insertNode(&head);
printf("<EFBFBD>O<EFBFBD>_<EFBFBD>~<7E><><EFBFBD><EFBFBD><EFBFBD>J? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
printf("\n<EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
printList(head);
// <20><><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD>]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Τ<EFBFBD><CEA4><EFBFBD><EFBFBD>ӧ<EFBFBD><D3A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{<7B>^
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}