Doubly Linked Lists C struct node int number
雙向鏈結串列 (Doubly Linked Lists)
C語言程式的結構語法 struct node { int number; char name[40]; struct node *back; struct node *next; }
u x → rlink =head ; u x → llink =tail ;
u tail - >rlink = x; u head -> link = x;
u tail = x;
• head →rlink = x; • x → llink = tail ;
• head → llink = x ; • tail = x ;
刪除動作 u 若假設要刪除this所指之節點,其步驟如下: • this → llink → rlink = this →rlink; • this → rlink → llink = this →llink; • free (this);
加入動作 u 產生串列首 void_init_head(struct node *ptr, struect node *head, struct node *tail) { ptr = ( struct *) malloc(sizeof ( struct student)) ; ptr -> key = NULL; ptr -> prev = ptr ; ptr ->next = ptr; head = ptr; tail = ptr; }
u 雙向鏈結串列的加入 void_insert_head(struct node *ptr, struect node *head, struct node *tail) { struct node *this ; this = head -> next; while (this != head) { if ( ptr->key < this->key) { ptr ->next = this ; ptr -> prev = this->prev ; this->prev->next = ptr ; this->prev = ptr; break; }
this = this ->next; } if (head->next = = head || this = = head) { ptr ->next = head ; ptr -> prev = head->prev ; head->prev->next = ptr ; head->prev = ptr; tail = ptr ; } }
刪除動作 void delete_node (int del_key, struect node *head, struct node *tail) { struct node *this, *clear; this = head ->next ; while (this != head) { if ( del_key = = this->key) { clear = this ; this->prev->next = this->next ; this->next->prev = this ->prev ; if ( this = = tail ) tail = this->prev; break; } this = this ->next; } free(clear); }
宣告函數程序 #include<stdio. h> void read_f(void); void write_f(void); void insert_f(void); void sort_f(void); void delete_f(void); void display_f(void); void modify_f(void); void anykey_f(void); //讀入第一筆學生資料 //寫入學生資料 //新增一筆學生資料 //利用成績做排序 //刪除一筆學生資料 //印出目前輸入的學生資料 //更改學生資料 //程式暫停執行
利用單向鏈結串列實作 單向鏈結節點結構宣告 //學生成績資料節點結構的宣告 typedef struct student { char name[20]; int score; struct student *next; } student; student *ptr, *head, *tail, *this_n, *prev;
u 主程式部分 void main(vokd) { int choice; read_f(); //檢查有無學生成績資料檔(dfile. dat) 的存在 while(1) { printf(“################ ##n”); printf(“ 1. insertn”); printf(“ 2. deleten”); printf(“ 3. displayn”); printf(“ 4. modifyn”); printf(“ 5. quitn”); printf(“################ ##n”); printf(“Please enter your choice(1 -5). . ”); scanf(“%d”, &choice); switch(choice)
• • • • • { case 1: insert_f(); break; case 2: delete_f() break; case 3: display_f() break; case 4: modify_f() break; case 5: write_f() exit(0); } } }
u 讀取學生資料 void read_f(void) { FILE *fptr; /*判斷檔案是否存在*/ if ((fptr=fopen(“dfile. dat”, ”r”)) ==NULL) { printf(“file doesn’t exitn”); printf(“press any key to continue”); getch(); insert_f(); } else /*若檔案存在,則讀入該檔資料*/ { ptr=(student*) malloc(sizeof(student)); while(fscanf(fptr, ”%s %d”, ptr->name, &ptr->score)!=EOF) { sort_f(); /*將讀入的學生成績排序*/ ptr=(student*)malloc(sizeof(student)); } fclose(fptr); } }
u 儲存學生資料 //離開程式時,將學生資料存入檔案 void write_f(void) { FILE *fptr; fptr=fopen(“dfile. dat”, ”w”); this_n=head; while(this_n !=NULL) { fprintf(fptr, “%s %dn”, this_n->name, this_n>score); this_n = this_n->next; } fclose(fptr); • }
u 新增學生資料 //新增一筆學生成績資料 void insert_f(void) { char s_temp[4]; ptr=(student*) malloc(sizeof(student)); printf(“Student name: ”); scanf(“%s”, ptr->name); printf(“student score: ”); scanf(“%s”, &s_temp); ptr->score = atoi(s_temp); sort_f();
成績排序 //依學生成績由高至低排序 void sort_f(void)) { if(head==NULL) { ptr->next = NULL; head = ptr; ; tail = ptr; ; } else { this_n = head; if(ptr->score > this_n->score) if(ptr->score > this_n { ptr->next = this_n; ; ptr->next = this_n head = ptr; ; } else { while(this_n->next !=NULL) { prev = this_n; ; prev = this_n->next; this_n = this_n if(ptr->score> this_n->score) if(ptr->score>this_n { ptr->next= this_n; ; ptr->next= this_n prev->next = ptr; ; prev->next =ptr break; } } if(ptr->score <= this_n->score) if(ptr->score <= this_n { ptr->next=NULL; this_n->next = ptr; ; this_n->next =ptr tail =ptr; ; } }
u 刪除學生資料 • void delete_f(void) • { • char del_name[20]; • int count=0; • struct student *clear; • printf( “ Delete student name: “); • gets(del_name); • this_n = head;
• while(strcmp(del_name, head->name) = = 0 /*刪除資料於前端*/ • { • count ++; • clear = this_n ; • if (head->next = = NULL) /*資料僅存一筆*/ • tail = NULL; • head = head ->next; • this_n = head; • free (clear); • }
• • • • • • • while(this_n ->next != NULL) */ while(this_n ->next != NULL) /*刪除資料於中間* { prev = this_n; ; prev = this_n->next; this_n = this_n while(strcmp(del_name, this_n->name) = = 0) while(strcmp(del_name, this_n { count ++; prev->next = this_n ->next; prev->next = this_n ->next; this_n = this_n free(clear); } } if (strcmp(del_name , this_n->name) = = 0) /*刪除資料於尾端 */ if (strcmp(del_name, /*刪除資料於尾端* { count ++; clear = this_n; ; prev ->next =NULL: tail = prev; ; free(clear); } if (count > 0) printf( “ %d student record(s) deletedn”, count); printf( “ %d student record(s else printf( “ Student %s not foundn”, del_name); anykey_f();
u 修改學生資料 • void modify_f(void)) • { • char n_temp[20], s_temp[4]; • int count=0; • printf(“ Modify student name: “); • gets(n_temp); • this_n =head; • while(this_n != NULL) • { • if(strcmp(n_temp, this_n->name) = = 0 if(strcmp(n_temp, this_n • { • printf(“ ******************************n”); • printf(“ Student name: % sn”, this_n->name); printf(“ Student name: %sn • printf(“ Student score: %dn dn”, this_n->score); printf • printf(“ ******************************n”); • printf(“ Please enter new score: “); • gets(s_temp); • this_n ->score = atoi(s_temp); ); this_n ->score = atoi(s_temp • count ++; • } • this_n = this_n->next; this_n = this_n • } • if(count > 0) • printf(“ %d student record(s) modifiedn”, count); printf(“ %d student record(s • else • printf(“ Student %s not foundn”, n_temp); • anykey_f(); • }
u 顯示學生資料 • void display_f(void)) • { • int count=0; • if(head ==NULL) printf(“no student recordn”); if(head ==NULL) printf(“no • else • { • printf(“NAME SCOREn”); • printf(“----------n”); printf(“----------n • this_n = head; • while(this_n !=NULL) • { • printf(“ %-20 s %3 dn”, this_n->name, this_n->score); printf(“ %-20 s %3 dn”, this_n • count ++; • this_n = this_n->next; this_n =this_n • if(count %20 ==0) getch(); if(count %20 ==0) getch • } • printf(“--------------------n”); • printf(“total %d record found n”, count); ); printf(“total %d record foundn”, count • } • anykey_f(); • } • • • void anykey_f(void)) { printf(“press any key to continue…”); getch(); printf(“n”); }
利用環狀鏈結串列實作 u 環狀鏈結節點結構宣告 • //學生成績資料節點結構的宣告 • typedef struct student • { • char name[20]; • int score; • struct student *next; • } • student; • student *ptr, *head, *tail, *this_n, *prev;
u 主程式部分 • void main(void)) • { • int choice; • read_f(); //檢查有無學生成績資料檔((dfile. dat)的存在 • while(1) • { • printf(“#################n”) ; printf(“#################n”); • printf(“ 1. insertn”) ; printf(“ 1. insertn”); • printf(“ 2. deleten”) ; printf(“ 2. deleten”); • printf(“ 3. displayn”) ; printf(“ 3. displayn”); • printf(“ 4. modifyn”) ; printf(“ 4. modifyn”); • printf(“ 5. quitn”) ; printf(“ 5. quitn”); • printf(“#################n”) ; printf(“#################n”); • printf(“Please enter your choice(1 -5). . ”) ; printf(“Please enter your choice(1 -5). . ”); • scanf(“%d”, &choice); • switch(choice) • { • case 1: • insert_f(); • break; • case 2: • delete_f() • break; • case 3: • display_f() • break; • case 4: • modify_f() • break; • case 5: • write_f() • exit(0); • } • }
u 讀取學生資料 • void read_f(void)) • { • FILE *fptr; ; • /*判斷檔案是否存在 */ /*判斷檔案是否存在* • if ((fptr =fopen(“dfile. dat”, ”r”)) ==NULL) if ((fptr= fopen(“dfile. dat”, ”r”)) ==NULL) • { • printf(“file doesn’t exitn”); • printf(“press any key to continue”); • getch(); • insert_f(); • } • else • { • ptr=( struct student*) malloc(sizeof( ( struct student)); ptr=(struct • while(fscanf(fptr, ”%s %d”, ptr-> ->name, &ptr->score)!=EOF) while(fscanf(fptr, ”%s %d”, ptr • { • if(strcmp(ptr->name, “”) != 0) • { • sort_f(); /*將讀入的學生成績排序 */ sort_f(); /*將讀入的學生成績排序* • ptr=(student*) malloc(sizeof(student )); ptr=(student*) malloc(sizeof(student • } • else • free(ptr); • } • fclose(fptr); • } /*若檔案存在,則讀入該檔資料 */ /*若檔案存在,則讀入該檔資料*
u 儲存學生資料 • //離開程式時,將學生資料存入檔案 • void write_f(void) • { • FILE *fptr; • fptr=fopen(“dfile. dat”, ”w”); • this_n=head; • while(this_n !=NULL) • { • fprintf(fptr, “%s %dn”, this_n->name, this_n->score); • this_n = this_n->next; • } • fclose(fptr); • }
u 新增學生資料 • //新增一筆學生成績資料 • void insert_f(void) • { • char s_temp[4]; • ptr=(student*) malloc(sizeof(student)); • printf(“Student name: ”); • scanf(“%s”, ptr->name); • printf(“student score: ”); • scanf(“%s”, &s_temp); • ptr->score = atoi(s_temp); • sort_f(); • }
u 成績排序 • //依學生成績由高至低排序 • void sort_f(void)) • { • if(head==NULL) • { /*插入資料為第一筆 */ /*插入資料為第一筆* • ptr->next = NULL; • head = ptr; ; • tail = ptr; ; • } • else • { • this_n = head; • if(strcmp(ptr->name, > this_n->name)< 0) if(strcmp(ptr->name, > this_n • { /*插入位置為前端 */ /*插入位置為前端* • ptr->next = this_n; ; ptr->next = this_n • head = ptr; ; head = • tail ->next = head; • } • else • { • while(this_n ->next !=head) • { • prev = this_n; ; prev = this_n • this_n = this_n->next; this_n = this_n • if(strcmp(ptr ->name, > this_n->name)< 0)) if(strcmp(ptr->name, > • { /*插入位置為中間 */ /*插入位置為中間* • ptr->next= this_n; ; ptr->next= this_n • prev->next = ptr; ; prev->next =ptr • break; • } • if(strcmp(ptr->name, > this_n->name)>= 0) if(strcmp(ptr->name, > this_n • { /*插入位置為尾端* * / /* • ptr->next=head; • this_n->next = ptr; ; this_n->next =ptr • tail =ptr; ; • } • }
u 顯示學生資料 • void display_f(void)) • { • int count=0; • if(head ==NULL) printf(“no student recordn”); if(head ==NULL) printf(“no • else • { • printf(“NAME SCOREn”); • printf(“--------------------n”); • this_n = head; • while(this_n !=NULL) • { • printf(“ %-20 s %3 dn”, this_n->name, this_n->score); printf(“ %-20 s %3 dn”, this_n • count ++; • this_n = this_n->next; this_n =this_n • if(count %20 ==0) getch(); if(count • } • printf(“--------------------n”); • printf(“total %d record found n”, count); ); printf(“total %d record foundn”, count • } • anykey_f(); • } • • • void anykey_f(void)) { printf(“press any key to continue…”); getch(); printf(“n”); }
雙向鏈結串列實作 u 雙向鏈結節點結構宣告 • //學生成績資料節點結構的宣告 • typedef struct student • { • char name[20]; • int score; • struct student *prev; /*節點左鏈結*/ • struct student *next; /*節點右鏈結*/ • } • student; • student *ptr, *head, *tail, *this_n, ;
u 主程式部分 • void main(vokd)) • { • int choice; • read_f(); //檢查有無學生成績資料檔((dfile. dat)的存在 • while(1) • { • printf(“#################n”) ; printf(“#################n”); • printf(“ 1. insertn”) ; printf(“ 1. insertn”); • printf(“ 2. deleten”) ; printf(“ 2. deleten”); • printf(“ 3. displayn”) ; printf(“ 3. displayn”); • printf(“ 4. modifyn”) ; printf(“ 4. modifyn”); • printf(“ 5. quitn”) ; printf(“ 5. quitn”); • printf(“#################n”) ; printf(“#################n”); • printf(“Please enter your choice(1 -5). . ”) ; printf(“Please enter your choice(1 -5). . ”); • scanf(“%d”, &choice); • switch(choice) • { • case 1: • insert_f(); • break; • case 2: • delete_f() • break; • case 3: • display_f() • break; • case 4: • modify_f() • break; • case 5: • write_f() • exit(0); • } • }
u 讀取學生資料 • void init_f(void HEAD, 將左右鏈結皆指向本身* */ void init_f(void)) /*設一HEAD, • { • ptr = ( struct student *) malloc(sizeof(struct student)); ptr = ( struct • strcpy(ptr->name, “ 0”); • ptr -> prev = = ptr; ; ptr ->prev • ptr ->next = ptr; ; ptr ->next = ptr • head = ptr; ; • tail = ptr; ; • } • void read_f(void)) • { • FILE *fptr; ; • /*判斷檔案是否存在 */ /*判斷檔案是否存在* • if ((fptr =fopen(“dfile. dat”, ”r”)) ==NULL) if ((fptr= fopen(“dfile. dat”, ”r”)) ==NULL) • { • printf(“file doesn’t exitn”); • printf(“press any key to continue…”); • getch(); • insert_f(); • } • else • { • ptr=(student*) malloc(sizeof(student )); ptr=(student*) malloc(sizeof(student • while(fscanf(fptr, ”%s % d”, ptr-> ->name, &ptr->score)!=EOF) while(fscanf(fptr, ”%s %d”, ptr • { if(strcmp(ptr ->name, “”) != 0) • { • sort_f(); • ptr=(student*) malloc(sizeof(student )); ptr=(student*) malloc(sizeof(student • } • else • free(ptr); • } • fclose(fptr); • }
u 儲存學生資料 • //離開程式時,將學生資料存入檔案 • void write_f(void) • { • FILE *fptr; • fptr=fopen(“dfile. dat”, ”w”); • this_n=head; • while(this_n !=NULL) • { • fprintf(fptr, “%s %dn”, this_n->name, this_n->score); • this_n = this_n->next; • } • fclose(fptr); • }
u 新增學生資料 • • • //新增一筆學生成績資料 void insert_f(void) { char s_temp[4]; ptr=(student*) malloc(sizeof(student)); printf(“Student name: ”); scanf(“%s”, ptr->name); printf(“student score: ”); scanf(“%s”, &s_temp); ptr->score = atoi(s_temp); sort_f(); }
成績排序 //依學生成績由高至低排序 void sort_f(void) { this_n = head->next; while(this_n != head) { if(strcmp(ptr->name, > this_n->name)< 0) { ptr->next = this_n; ptr->prev = this_n->prev; this_n ->prev ->next = ptr; this_n ->prev = ptr; break; } this_n = this_n ->next; } if (head->next == head | | this_n == head) { ptr ->next =head; ptr ->prev = head -> prev; head ->prev->next = ptr; tail = ptr; } }
- Slides: 47