Linked List CSIM PU C Language 1 malloc

  • Slides: 31
Download presentation
鏈結串列 (Linked List) CSIM, PU C Language 1

鏈結串列 (Linked List) CSIM, PU C Language 1

動態記憶體配置---malloc函數 q 範例: #include<stdio. h> int main() { float *fp; fp=(float *)malloc(sizeof(float)); if(fp!=NULL) {

動態記憶體配置---malloc函數 q 範例: #include<stdio. h> int main() { float *fp; fp=(float *)malloc(sizeof(float)); if(fp!=NULL) { *fp=3. 14; printf(“數字=%fn”, *fp); } else printf(“記憶體配置失敗!n”); } CSIM, PU C Language 4

動態記憶體配置---free函數 q free函數可將已配置的記憶體空間歸還。 q 用法: free(fp); q 範例: int main() { float *fp; fp=(float

動態記憶體配置---free函數 q free函數可將已配置的記憶體空間歸還。 q 用法: free(fp); q 範例: int main() { float *fp; fp=(float *)malloc(sizeof(float)); if(fp==NULL) printf(“記憶體配置失敗n”); free(fp); } CSIM, PU C Language 5

單向鏈結串列 q 可使用自我參考結構(self-referential structure)來定義節點 結構,如下: typedef struct node { int data; struct node *link;

單向鏈結串列 q 可使用自我參考結構(self-referential structure)來定義節點 結構,如下: typedef struct node { int data; struct node *link; } nodes; ptr為node結構的指標 nodes *ptr; 定義節點結構 q 可使用malloc函數來建立新節點,如下: ptr=(nodes *)malloc(sizeof(nodes)); 配置一塊記憶體空間 為指標,指向另ㄧ個node結構 ptr data CSIM, PU link C Language 7

練習 #include<stdio. h> int main() { typedef struct node { int data; struct node

練習 #include<stdio. h> int main() { typedef struct node { int data; struct node *link; }nodes; ptr 10 null data link nodes *ptr; ptr =(nodes *)malloc(sizeof(nodes)); ptr->data=10; ptr->link=NULL; printf("第一個數= %dn", ptr->data); } CSIM, PU C Language 8

練習 2 #include<stdio. h> int main() { typedef struct node { int data; struct

練習 2 #include<stdio. h> int main() { typedef struct node { int data; struct node *link; }nodes; nodes *ptr, *first; ptr =(nodes *)malloc(sizeof(nodes)); first =(nodes *)malloc(sizeof(nodes)); ptr 10 null data link 20 null data link first ptr->data=10; ptr->link=NULL; first->data=20; first->link=NULL; printf("第一個數= %dn", ptr->data); printf(“第二個數= %dn", first->data); } CSIM, PU C Language 9

單向鏈結串列 q 程式範例: #include<stdio. h> int main() { ptr typedef struct node { int

單向鏈結串列 q 程式範例: #include<stdio. h> int main() { ptr typedef struct node { int data; struct node *link; }nodes; nodes *ptr, *first; ptr =(nodes *)malloc(sizeof(nodes)); first = (nodes *)malloc(sizeof(nodes)); ptr->data=10; first->data=20; ptr->link=first; printf("第一個數= %dn", ptr->data); printf("第二個數= %dn", ptr->link->data); } CSIM, PU first 20 10 data link data C Language link 10

單向鏈結串列-加入串列前端 a. 加入於串列的前端 ptr NULL x typedef struct node { int data; struct node

單向鏈結串列-加入串列前端 a. 加入於串列的前端 ptr NULL x typedef struct node { int data; struct node *link; }nodes; nodes *x; 步驟如下: (1)x=(nodes *) malloc (sizeof(nodes)); (2)x→link=ptr; (3)ptr=x; CSIM, PU /*配置記憶體空間*/ /*將x節點的point指到剛head的地方*/ /*head換指到x節點(前端變成x節點)*/ C Language 11

練習 3 -加入節點於串列前端 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 first ptr (1) 20 10 data link data first

練習 3 -加入節點於串列前端 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 first ptr (1) 20 10 data link data first ptr (2) CSIM, PU data 20 10 5 x link data link C Language 12

單向鏈結串列-加入串列尾端 b. 加入於串列的尾端 head NULL next x null 步驟如下: (1)x=(nodes *) malloc (sizeof(nodes)); x->link=NULL;

單向鏈結串列-加入串列尾端 b. 加入於串列的尾端 head NULL next x null 步驟如下: (1)x=(nodes *) malloc (sizeof(nodes)); x->link=NULL; (2)next=head while(next->link!=NULL) next=next->link; next->link=x; CSIM, PU C Language 13

練習 4 -加入節點於串列尾端 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 ptr (1) data 20 10 5 data link y

練習 4 -加入節點於串列尾端 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 ptr (1) data 20 10 5 data link y ptr (2) data CSIM, PU 20 10 5 link data 30 link data C Language link 14

單向鏈結串列-加入某一節點之後 c. 加入在串列某一特定節點(add節點)的後面 add ptr NULL x 步驟如下: (1)x=(nodes *) malloc (sizeof(nodes)); (2)x→link=add→link; (3)add→link=x;

單向鏈結串列-加入某一節點之後 c. 加入在串列某一特定節點(add節點)的後面 add ptr NULL x 步驟如下: (1)x=(nodes *) malloc (sizeof(nodes)); (2)x→link=add→link; (3)add→link=x; CSIM, PU C Language 15

單向鏈結串列-刪除串列前端 a. 刪除串列前端的節點 ptr NULL temp 步驟如下: (1)temp=ptr; (2)ptr=ptr->link; (3)free(temp); CSIM, PU C Language

單向鏈結串列-刪除串列前端 a. 刪除串列前端的節點 ptr NULL temp 步驟如下: (1)temp=ptr; (2)ptr=ptr->link; (3)free(temp); CSIM, PU C Language 16

練習 5 -刪除前端節點 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 ptr (1) data 20 10 5 data link data

練習 5 -刪除前端節點 q 請將下方(1)鏈結串列,改成(2)鏈結串列。 ptr (1) data 20 10 5 data link data 30 link data link ptr (2) data CSIM, PU 20 10 link data 30 link data link C Language 17

單向鏈結串列-刪除串列最後節點 b. 刪除串列的最後節點 prev ptr NULL 步驟如下: NULL temp (1) temp=ptr; while(temp->link!=NULL) { prev=temp;

單向鏈結串列-刪除串列最後節點 b. 刪除串列的最後節點 prev ptr NULL 步驟如下: NULL temp (1) temp=ptr; while(temp->link!=NULL) { prev=temp; temp=temp->link; } (2) prev->link=NULL; (3) free(temp); CSIM, PU C Language 18

計算單向鏈結串列之長度 串列長度指的是此串列共有多少個節點,只要指標指到的 節點非NULL,則利用一變數做累加,直到指標到NULL為止。 int length (struct node *ptr ) { struct node *this; this=ptr;

計算單向鏈結串列之長度 串列長度指的是此串列共有多少個節點,只要指標指到的 節點非NULL,則利用一變數做累加,直到指標到NULL為止。 int length (struct node *ptr ) { struct node *this; this=ptr; int leng=0; while (this != NULL) { leng ++; this=this->link; } return leng ; } CSIM, PU C Language 20

多項式的相加 q 假設兩個多項式 與 相加 q 以單向鏈結串列方式呈現,C語言片段程式如下: void poly_add(struct poly *eql, struct poly *eq

多項式的相加 q 假設兩個多項式 與 相加 q 以單向鏈結串列方式呈現,C語言片段程式如下: void poly_add(struct poly *eql, struct poly *eq 2, struct poly *ans_h, struct poly *ptr) { struct poly *thisl, *this 2, *prev; this 1 = eq 1; this 2 = eq 2; prev = NULL; while(this 1 != NULL || this 2 != NULL) { ptr = (struct poly*) malloc(sizeof(struct poly)); ptr ->link = NULL; CSIM, PU C Language 25

if (this 1 != NULL && (this 2 = = NULL | | this

if (this 1 != NULL && (this 2 = = NULL | | this 1 ->exp > this 2 ->exp)) { ptr->coef = this 1 ->coef; 第ㄧ個多項式指數大於第二個多項式 ptr->exp = this 1 ->exp; this 1 = this 1 ->next; } else if (this 1 == NULL || this 1 ->exp < this 2 ->exp) { ptr->coef = this 2 ->coef; ptr->exp = this 2 ->exp; 第ㄧ個多項式指數小於第二個多項式 this 2 = this 2 ->link; } else { ptr->coef = this 1 ->coef + this 2 ->coef; ptr->exp = this 1 ->exp; if (this 1 != NULL) 兩個多項式指數相等,進行相加 this 1 = this 1 ->link; if (this 1 != NULL) this 2 = this 2 ->link; } CSIM, PU C Language 26

} CSIM, PU } if (ptr->coef != 0) { if (ans_h = = NULL)

} CSIM, PU } if (ptr->coef != 0) { if (ans_h = = NULL) ans_h = ptr; else prev -> link = ptr; prev = ptr; } else free (ptr); C Language 27

刪除堆疊頂端的頂點:其運作類似刪除串列的前端節點。 刪除資料的演算法 void pop(int num , nodes *top) { nodes *clear; if(top==NULL) { printf(“stack

刪除堆疊頂端的頂點:其運作類似刪除串列的前端節點。 刪除資料的演算法 void pop(int num , nodes *top) { nodes *clear; if(top==NULL) { printf(“stack is empty”); return; } clear=top; num=top->data; top=top->link; free(clear); } CSIM, PU top clear top C Language 29

六、使用鏈結串列製作佇列 佇列的加入 void Add. Q(int num , nodes *front , nodes *rear) { nodes

六、使用鏈結串列製作佇列 佇列的加入 void Add. Q(int num , nodes *front , nodes *rear) { nodes *ptr; ptr = (nodes *)malloc(sizeof(nodes)); front ptr->data=num; ptr->next=NULL; if(rear==NULL) front=rear=ptr; else { rear->link=ptr; rear=ptr; rear } } CSIM, PU ptr null num null rear C Language 30

六、使用鏈結串列製作佇列 佇列的刪除 void Delete. Q(int num , nodes *front) { clear nodes *clear; if

六、使用鏈結串列製作佇列 佇列的刪除 void Delete. Q(int num , nodes *front) { clear nodes *clear; if (front==NULL) { front printf(“queue empty”); return; } num=front->data; clear=front; front=front->link; rear null free(clear); } CSIM, PU C Language 31