220 likes | 424 Views
第四章 鏈結串列 Linked List. Fools look to tomorrow, wise men use tonight. 練功前的沉思. Q1: Remember the pointer in C? Q2: What is linked list? Q3: What is single linked list? Q4: What is circular linked list? Q4: How to implement linked list using C ?
E N D
第四章 鏈結串列Linked List Fools look to tomorrow, wise men use tonight.
練功前的沉思 Q1: Remember the pointer in C? Q2: What is linked list? Q3: What is single linked list? Q4: What is circular linked list? Q4: How to implement linked list using C? Q5: What is the difference between linked list and array? (優缺點) Q6: When do I apply linked list to solve problems? Q7: Do you aware any problem for link list? 李麗華--資料結構講義
address i 10 FFF0 ptr FFF0 FFF2 FFF4 p ? Review of Pointer in C • The pointer data type in C means the data stores the address that this address points to other data with the same type). EX:int i, *ptr, *p ; i=10 ; ptr = &i ; p= (int *) malloc(sizeof(int)) ; *p = 1024 ; printr(“i=%d, *ptr=%d, *p=%d ”, i, *ptr, *p) free(p); 將記憶體釋回 取得記憶體 832A 1024 李麗華--資料結構講義
data ptr node What is linked list? Def: A link list is a dynamic allocating list in which data is stored in a node and pointer(s) is used to indicate the position of the list. Characteristics: 1. The list is a dynamically allocated structure. 2. The size of the storage do not need to be declared in advance. 3.Dynamically create/dispose the node when a node is inserted/deleted. 4.Insertion and deletion can be done without any order. 李麗華--資料結構講義
data next 放資料內容 放指標 head next NULL NULL The data structure of a node Example of a node declared in C: struct node { int data; <--資料型態可自訂 struct node *next; } Create a node: x=(struct node *)malloc(sizeof(struct node)); 名稱自訂,例如可叫 head,tail,ptr,x,y,z,… Initialize a linked list, i.e., head = tail = NULL; head -> link = NULL; tail -> link = NULL 若指標指向NULL,有時亦可畫成: 李麗華--資料結構講義
Tail Head APE DOG CAT BIRD BAT Single Linked List • A single linked list is a dynamic allocated list where nodes are linked by one way pointer. • Insertion and deletion of a node can be done in any position of a list. 李麗華--資料結構講義
10 x 100 200 300 Insert a node to the sorted list (1) 1. Get the memory for the new node x=(struct node *)malloc(sizeof(struct node)); x = num; (設 x 內存放某一數值num) x->next = NULL ; 2. To insert a node in a sorted list, we search the list from “head” and find the right position. Three possible positions are considered. (1) insert to the head (2) insert to the middle (3) insert to the tail Insert to the head x->next = head ; head = x; Tail Head 400 李麗華--資料結構講義
Head 400 Tail x 500 Tail Head 400 200 pre cur 250 x 200 100 300 100 300 Insert a node to the sorted list (2) Insert to the tail tail->next = x ; tail = x; Insert to the middle cur = head; while(x->data > cur->data) { pre = cur ; cur = cur -> next; } x->next = cur; pre -> next = x; 李麗華--資料結構講義
Delete a node from the sorted list (1) 1. Find the node in the list. Deletion should be taken care in three different positions. (1) Delete from head (2) Delete from middle (3) Delete from tail 2. When found the node, adjust the link and dispose the node. Delete from head: p = head ; head = head->next; free(p); Delete from tail: p = head ; while(p->next != tail) p = p ->next; p ->next = NULL; free(tail) ; tail = p; Dispose a node p: free(p) ; 李麗華--資料結構講義
Tail Head pre cur 500 200 100 300 400 Delete a node from the sorted list (2) Delete a node in the middle cur = head; while(cur->next != NULL) { pre = cur ; cur = cur -> next; if (x->data == cur->data) { pre->next = cur->next; free(cur); } } 令欲刪除x->data=300 李麗華--資料結構講義
x z 500 y 500 200 200 100 100 300 300 Join Two Lists Join x & y list into Z: struct node *c; if (x==NULL) z = y ; 當x的list為空else if (y==NULL) z = x ; 當y的list為空 else { z = x ; c = x ; while(c->next!=NULL) c = c -> next; c -> next = y; } 李麗華--資料結構講義
Head Tail nex 500 200 200 100 300 300 300 pre pre pre pre cur cur cur cur nex nex nex nex Invert a List Invert a list: struct node *cur,*pre,*nex; nex = head; cur = NULL; while(nex != NULL) { pre = cur; cur = nex; nex = nex -> next cur-> next = pre; } tail = head; head = cur; Head Tail 500 100 Head Tail 200 500 100 Head Tail 200 300 500 100 Tail Head 500 200 300 100 李麗華--資料結構講義
500 Head Tail x head head 200 100 300 tail tail x head Circular List Def: A circular list is a list with its tail points to the head. Insert node x to head When list is not empty x->next = head; tail -> next = x; head=x; Insert node x to head When list is empty head=x; tail=x; x->next = x; 李麗華--資料結構講義
Circular List Operation Delete node p from tail p = head; while(p->next != tail) p= p-> next; p -> next = head; free(tail); tail = p; Insert node x to tail tail -> next = x; x->next = head; tail = x; Delete node p from head p = head; head = head -> next; tail -> next = head; free(p); Delete node p from middle 做法與一般串列相同 請參考前面內容 李麗華--資料結構講義
300 300 Ahead Bhead Btail Atail 300 300 Bhead Ahead Btail Atail 200 200 200 200 100 100 100 100 Join Two Circular Lists Join A & B circular list Atail -> next = Bhead; Btail -> next = Ahead; 李麗華--資料結構講義
pre data next 放左指標 放資料 放右指標 Head Tail 10 10 10 10 Double Linked List • The problem of single linked list is that the search can only be done through one way pointer. • The double linked list is a type of circular list in which each node contains two way pointers. struct node { int data ; <--資料型態可自訂 struct node *pre ; struct node *next ; } 李麗華--資料結構講義
x head head tail tail x head Double Linked List Operation(1) Insert node x to head When double linked list is empty head=x; tail=x; x->next = x; Insert node x to head When double linked list is not empty x->next = head; tail -> next = x; head=x; 李麗華--資料結構講義
Double Linked List Operation(2) Delete node p from tail p = head; while(p->next != tail) p= p-> next; p -> next = head; free(tail); tail = p; Insert node x to tail tail -> next = x; x->next = head; tail = x; Delete node p from head p = head; head = head -> next; tail -> next = head; free(p); Delete node p from middle 做法與一般串列相同 請參考前面內容 李麗華--資料結構講義
Application—Stack & Queue 李麗華--資料結構講義
Polynomial Addition 李麗華--資料結構講義
大師—該動動腦囉!! • 第1題,第3題 • 請寫出double linked list及circular linked list的反轉串列之演算法. 回家動動腦 上課來發表 你知道Linked List的問題嗎? -- 如果不小心沒有將pointer處理好, 會產生哪些問題?? -- 如果忘了free(ptr), 又會有什麼問題?? -- 如果指標指錯了, 又可能產生哪些問題?? 李麗華--資料結構講義
Worry is like a rocking chair--it will give you something to do but it won’t get you anywhere. 煩惱好像讓你的腦袋有事做, 但卻無法幫助你解決事情 李麗華--資料結構講義