Data_Structure/作業/unit4/sList-v3/sList-v3.c

450 lines
11 KiB
C
Raw Normal View History

2025-01-20 21:30:53 +08:00
/* sList.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* <20>w<EFBFBD>q<EFBFBD>@<40><><EFBFBD>V FILE <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
FILE *fptr;
/* <20><EFBFBD><E7A6A1><EFBFBD><EFBFBD>ŧi */
void init_f(void);
void insertStudent(int id, char* name, double score);
void readFromFile(const char* filename);
void saveToFile(const char* filename, int flag);
void insert(void);
void del(void);
void modify(void);
void display(void);
// <20>w<EFBFBD>q<EFBFBD>ǥ͸`<60>I<EFBFBD><49><EFBFBD>c
typedef struct Student {
long int id;
char name[10];
double score;
struct Student *rlink;
struct Student *llink;
} Student;
Student *head, *pNode ,*tail, *current, *prev, *temp , *originalHead ,*originalTail;
//<2F>]<5D>@<40><> head<61>A<EFBFBD>N<EFBFBD><4E><EFBFBD>k<EFBFBD><EFBFBD>ҫ<EFBFBD><D2AB>V<EFBFBD><56><EFBFBD><EFBFBD>
void init_f() {
head = (Student *) malloc(sizeof(Student));
tail = (Student *) malloc(sizeof(Student));
// <20><><EFBFBD>l<EFBFBD><6C> head <20>M tail
head->llink = NULL;
head->rlink = tail;
tail->llink = head;
tail->rlink = NULL;
// <20><><EFBFBD>l<EFBFBD>ƭ<EFBFBD><C6AD>l<EFBFBD><6C><EFBFBD>Ǧ<EFBFBD><C7A6>C
originalHead = (Student *) malloc(sizeof(Student));
originalTail = (Student *) malloc(sizeof(Student));
originalHead->llink = NULL;
originalHead->rlink = originalTail;
originalTail->llink = originalHead;
originalTail->rlink = NULL;
}
// <20>b<EFBFBD><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>s<EFBFBD>ǥ͸`<60>I
void insertStudent(int id, char* name, double score) {
Student* newNode = (Student*)malloc(sizeof(Student));
newNode->id = id;
strcpy(newNode->name, name);
newNode->name[9] = '\0';
newNode->score = score;
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e
newNode->rlink = tail;
newNode->llink = tail->llink;
tail->llink->rlink = newNode;
tail->llink = newNode;
}
/* <20>s<EFBFBD>W<EFBFBD><57><EFBFBD>ơG<C6A1><47><EFBFBD>J<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><6C><EFBFBD>Ǧ<EFBFBD><C7A6>C */
void insertOriginalList(int id, char* name, double score) {
Student* newNode = (Student*)malloc(sizeof(Student));
newNode->id = id;
strncpy(newNode->name, name, 9);
newNode->name[9] = '\0';
newNode->score = score;
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><6C><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e
newNode->rlink = originalTail;
newNode->llink = originalTail->llink;
originalTail->llink->rlink = newNode;
originalTail->llink = newNode;
}
// Ū<><C5AA>
void readFromFile(const char* filename) {
char line[100];
FILE* fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>}<7D>ɮ<EFBFBD> %s\n", filename);
return;
}
long int id;
char name[10];
double score;
while(fgets(line ,sizeof(line) ,fptr)!=NULL){
if(sscanf(line,"%ld\t%9s\t%lf", &id, name, &score) == 3){
insertStudent(id, name, score);
insertOriginalList(id, name, score); // <20><><EFBFBD>J<EFBFBD><4A><EFBFBD>l<EFBFBD><6C><EFBFBD>Ǧ<EFBFBD><C7A6>C
}
}
fclose(fptr);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɮ׸<EFBFBD><EFBFBD><EFBFBD>\n");
}
// <20>s<EFBFBD><73>
void saveToFile(const char* filename , int flag) {
FILE* fptr = fopen(filename, "w");
if (fptr == NULL) {
printf("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>}<7D>ɮ<EFBFBD> %s <20>i<EFBFBD><69><EFBFBD>g<EFBFBD>J\n", filename);
return;
}
if(flag == 2){
Student* current = head->rlink;
while (current != tail ) {
fprintf(fptr, "%ld\t%s\t%.lf\n", current->id, current->name, current->score);
current = current->rlink;
}
}else if(flag == 1){
Student* current = originalHead->rlink;
while (current != originalTail ) {
fprintf(fptr, "%ld\t%s\t%.lf\n", current->id, current->name, current->score);
current = current->rlink;
}
}
fclose(fptr);
printf("<EFBFBD><EFBFBD><EFBFBD>Ƥw<EFBFBD><EFBFBD><EFBFBD>\\<EFBFBD>x<EFBFBD>s<EFBFBD><EFBFBD> %s\n", filename);
}
//<2F><><EFBFBD>ܦ<EFBFBD><DCA6>C<EFBFBD><43><EFBFBD><EFBFBD>
void printStudentList() {
Student* current = head->rlink;
printf("\n*******************************");
if (current != NULL) {
printf("\n%6s %10s %8s\n", "ID", "Name", "Score");
printf("-------------------------------\n");
while (current != tail) {
printf("%6ld %10s %8.1f\n", current->id,current->name, current->score);
/* <20>N<EFBFBD><4E><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>U<EFBFBD>@<40>Ӹ`<60>I */
current = current->rlink;
}
}
/* <20>Y<EFBFBD>O<EFBFBD>Ū<EFBFBD><C5AA>A<EFBFBD>h<EFBFBD><68><EFBFBD>X<EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD> */
else {
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>L<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
printf("*******************************\n\n");
}
/* <20>s<EFBFBD>W<EFBFBD><57><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD>Ǹ<EFBFBD><C7B8>ƪ<EFBFBD><C6AA><EFBFBD><EFBFBD><EFBFBD> */
void printOriginalList() {
Student* current = originalHead->rlink;
printf("\n*******************************");
if (current != originalTail) {
printf("\n%6s %10s %8s\n", "ID", "Name", "Score");
printf("-------------------------------\n");
while (current != originalTail) {
printf("%6ld %10s %8.1f\n", current->id, current->name, current->score);
current = current->rlink;
}
} else {
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>L<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
printf("*******************************\n\n");
}
void sort_id(){
Student* temp ;
Student* current;
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (head->rlink == tail || head->rlink->rlink == tail) {
return;
}
int flag = 1;
while(flag){
flag = 0;
current = head->rlink;
while(current->rlink != tail){
temp = current->rlink;
if(current->id > temp->id){
// <09>u<EFBFBD>ϥΨ<CFA5><CEA8>Ӹ`<60>I<EFBFBD>i<EFBFBD><69><EFBFBD>`<60>I<EFBFBD>
// <09>n<EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>Ч<EFBFBD><D0A7>ܫ᪺<DCAB><E1AABA><EFBFBD><EFBFBD>
// <09>Y<EFBFBD>U<EFBFBD>C<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD>ǥ洫<C7A5><E6B4AB><EFBFBD>Ы<EFBFBD><D0AB>V<EFBFBD><56><EFBFBD><EFBFBD><EFBFBD>}<7D>|<7C>X<EFBFBD><58>
// <09>]<5D>i<EFBFBD>H<EFBFBD>h<EFBFBD>ŧi<C5A7><69><EFBFBD>Ӹ`<60>I <20><><EFBFBD><EFBFBD>current->llink<6E><6B>temp->rlink
// <09>o<EFBFBD>˴N<CBB4>i<EFBFBD>H<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD>޶<EFBFBD><DEB6><EFBFBD>
temp->llink = current->llink;
current->rlink = temp->rlink;
temp->rlink->llink = current;
temp->rlink = current;
current->llink->rlink = temp;
current->llink = temp;
flag = 1;
}
else{
current = current->rlink;
}
}
}
}
void sort_score(){
Student* temp;
Student* current;
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (head->rlink == tail || head->rlink->rlink == tail) {
return;
}
int flag = 1;
while(flag){
flag = 0;
current = head->rlink;
while(current->rlink != tail){
temp = current->rlink;
if(current->score < temp->score){
// <09>u<EFBFBD>ϥΨ<CFA5><CEA8>Ӹ`<60>I<EFBFBD>i<EFBFBD><69><EFBFBD>`<60>I<EFBFBD>
// <09>n<EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>Ч<EFBFBD><D0A7>ܫ᪺<DCAB><E1AABA><EFBFBD><EFBFBD>
// <09>Y<EFBFBD>U<EFBFBD>C<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD>ǥ洫<C7A5><E6B4AB><EFBFBD>Ы<EFBFBD><D0AB>V<EFBFBD><56><EFBFBD><EFBFBD><EFBFBD>}<7D>|<7C>X<EFBFBD><58>
// <09>]<5D>i<EFBFBD>H<EFBFBD>h<EFBFBD>ŧi<C5A7><69><EFBFBD>Ӹ`<60>I <20><><EFBFBD><EFBFBD>current->llink<6E><6B>temp->rlink
// <09>o<EFBFBD>˴N<CBB4>i<EFBFBD>H<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD>޶<EFBFBD><DEB6><EFBFBD>
temp->llink = current->llink;
current->rlink = temp->rlink;
temp->rlink->llink = current;
temp->rlink = current;
current->llink->rlink = temp;
current->llink = temp;
flag = 1;
}
else{
current = current->rlink;
}
}
}
}
int main(){
init_f();
readFromFile("list.txt");
printStudentList();
int flag = 0;
/* <20>Q<EFBFBD>Τ@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϥο<CFA5><CEBF>ܥ\<5C><EFBFBD><E0B6B5> */
int choice;
do {
printf("************************\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>@<40><><EFBFBD><EFBFBD>\n");
printf("1. <20>[<5B>J<EFBFBD>@<40>`<60>I\n");
printf("2. <20>R<EFBFBD><52><EFBFBD>@<40>`<60>I\n");
printf("3. <20>ק<EFBFBD><D7A7>@<40>`<60>I\n");
printf("4. <20><><EFBFBD>ܩҦ<DCA9><D2A6>`<60>I\n");
printf("5. <20><><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><ECAABA><EFBFBD><EFBFBD>\n");
printf("6. <20><><EFBFBD>ܦ<EFBFBD><DCA6>Z<EFBFBD>Ѱ<EFBFBD><D1B0>ܧC<DCA7>ƦC\n");
printf("7. <20><><EFBFBD><EFBFBD>ID<49>Ѥp<D1A4><70><EFBFBD>j<EFBFBD>ƦC\n");
printf("8. <20><><EFBFBD><EFBFBD>\n");
printf("<EFBFBD>п<EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
modify();
break;
case 4:
printStudentList();
break;
case 5:
printOriginalList();
flag = 1;
break;
case 6:
sort_score();
printStudentList();
flag = 2;
break;
case 7:
sort_id();
printStudentList();
flag = 2;
break;
case 8:
printf("\n<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
saveToFile("list.txt",flag);
exit(0);
default:
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>X<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T<EFBFBD>A<EFBFBD>Э<EFBFBD><EFBFBD>s<EFBFBD><EFBFBD><EFBFBD>J\n");
}
printf("\n");
} while(choice != 8);
getchar();
return 0;
}
/* <20><><EFBFBD>Ӥ<EFBFBD><D3A4>ƥѤj<D1A4>ܤp<DCA4>[<5B>J<EFBFBD>@<40>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C */
void insert()
{
/* <20>Q<EFBFBD><51> malloc() <20><EFBFBD>t<EFBFBD>m<EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>*/
Student* newNode = (Student*)malloc(sizeof(Student));
printf("\n<EFBFBD>п<EFBFBD><EFBFBD>JID: ");
scanf("%ld", &newNode->id);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>m<EFBFBD>W: ");
scanf("%s", newNode->name);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%lf", &newNode->score);
/* <20>[<5B>J<EFBFBD>@<40>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C */
current = head->rlink;
prev = head;
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e
newNode->rlink = tail;
newNode->llink = tail->llink;
tail->llink->rlink = newNode;
tail->llink = newNode;
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD>l<EFBFBD><6C><EFBFBD>Ǧ<EFBFBD><C7A6>C
insertOriginalList(newNode->id, newNode->name, newNode->score);
}
void deleteFromOriginalList(long int deleteID) {
Student* current = originalHead->rlink;
Student* prev = originalHead;
while (current != originalTail) {
if (current->id == deleteID) {
current->rlink->llink = prev;
prev->rlink = current->rlink;
free(current);
break;
}
prev = current;
current = current->rlink;
}
}
/* <20>R<EFBFBD><52><EFBFBD>Y<EFBFBD>@<40>`<60>I*/
void del()
{
long int deleteID;
/* <20>N current <20><><EFBFBD>Ы<EFBFBD><D0AB>V head <20><><EFBFBD>U<EFBFBD>@<40>Ӹ`<60>I */
current = head->rlink;
prev = head;
/* <20><><EFBFBD>P<EFBFBD>_<EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD> */
if (current != NULL) {
/* <20>Y<EFBFBD><59><EFBFBD>O<EFBFBD>Ū<EFBFBD><C5AA>A<EFBFBD>h<EFBFBD><68><EFBFBD>M<EFBFBD><4D><EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I */
printf("\n<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ID: ");
scanf("%ld", &deleteID);
/* <20><><EFBFBD>M<EFBFBD><4D><EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I */
while ((current != tail) && (current->id != deleteID)) {
prev = current;
current = current->rlink;
}
/* <20>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>h<EFBFBD>N<EFBFBD><4E><EFBFBD>R<EFBFBD><52> */
if (current != tail) {
current->rlink->llink = prev;
prev->rlink = current->rlink;
printf("ID: %ld <20>w<EFBFBD>R<EFBFBD><52>\n", current->id);
deleteFromOriginalList(deleteID);
free(current);
}
/* <20>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>h<EFBFBD><68><EFBFBD>X<EFBFBD><EFBFBD><E4A4A3><EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><52><EFBFBD>`<60>I<EFBFBD><49><EFBFBD>T<EFBFBD><54>*/
else {
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I\n");
}
}
/* <20>Y<EFBFBD>O<EFBFBD>Ū<EFBFBD><C5AA>A<EFBFBD>h<EFBFBD><68><EFBFBD>X<EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD>O<EFBFBD>Ū<EFBFBD><C5AA>T<EFBFBD><54> */
else {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>O<EFBFBD>Ū<EFBFBD>\n");
}
}
/* <20>ק<EFBFBD><D7A7>Y<EFBFBD>@<40>`<60>I */
void modify()
{
Student *temp;
long int modifyID;
double modifyScore;
int flag = 0;
printf("\n<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>ק<EFBFBD><EFBFBD>`<60>I<EFBFBD><49> ID: ");
scanf("%ld", &modifyID);
current = head->rlink;
prev = head;
/* <20><><EFBFBD>M<EFBFBD><4D><EFBFBD>ק諸<D7A7>`<60>I */
while (current != tail) {
if (current->id == modifyID) {
printf("<EFBFBD>ثe<EFBFBD><EFBFBD><EFBFBD>ק<EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>Ʀp<C6A6>U:\n");
printf("%6ld %10s %8.1f\n\n", current->id,
current->name, current->score);
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>ק諸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf("%lf", &modifyScore);
current->score = modifyScore;
flag = 1;
break;
}
else {
prev = current;
current = current->rlink;
}
}
/* <20>P<EFBFBD>_<EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ק諸<D7A7>`<60>I */
if (flag != 0) {
/* <20>N current <20><><EFBFBD>`<60>I<EFBFBD><49><EFBFBD>w<EFBFBD><77> temp */
temp = current;
prev->rlink = current->rlink;
/* <20>N temp <20>`<60>I<EFBFBD>[<5B>J<EFBFBD><4A><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C */
current = head->rlink;
prev = head;
while ((current != tail) && (temp->score < current->score)) {
prev = current;
current = current->rlink;
}
prev->rlink = temp;
temp->rlink = current;
}
else {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ק諸<EFBFBD>`<60>I\n");
}
}