273 lines
6.5 KiB
C
273 lines
6.5 KiB
C
|
/* 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);
|
|||
|
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 ;
|
|||
|
|
|||
|
//<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>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;
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><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);
|
|||
|
}
|
|||
|
}
|
|||
|
fclose(fptr);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɮ<EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
}
|
|||
|
|
|||
|
// <20>s<EFBFBD><73>
|
|||
|
void saveToFile(const char* filename ) {
|
|||
|
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;
|
|||
|
}
|
|||
|
Student* current = head->rlink;
|
|||
|
while (current != tail ) {
|
|||
|
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");
|
|||
|
}
|
|||
|
|
|||
|
int main(){
|
|||
|
init_f();
|
|||
|
readFromFile("list.txt");
|
|||
|
printStudentList();
|
|||
|
/* <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>\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:
|
|||
|
printf("\n<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
saveToFile("list.txt");
|
|||
|
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 != 5);
|
|||
|
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>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);
|
|||
|
|
|||
|
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");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|