朝陽科技大學資管系 李麗華
第四章 鏈結串列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?
--李麗華 資料結構講義
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); 將記憶體釋回
10i FFF0
FFF0ptr FFF2
address
?p FFF4
1024 832A
取得記憶體
--李麗華 資料結構講義
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.
node
data ptr
--李麗華 資料結構講義
The data structure of a nodeExample of a node declared in C: struct node { int data; <--資料型態可自訂 struct node *next; }
Initialize a linked list, i.e., head = tail = NULL; head -> link = NULL; tail -> link = NULL
head nextNULL NULL
若指標指向 NULL,有時亦可畫成 :
data next
放資料內容 放指標
Create a node: x=(struct node *)malloc(sizeof(struct node)); 名稱自訂 ,例如可叫 head,tail,ptr,x,y,z,…
--李麗華 資料結構講義
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.
Head
DOG CAT BIRD BAT APE
Tail
--李麗華 資料結構講義
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;
Head100 200 300 400
Tail10
x
--李麗華 資料結構講義
Insert a node to the sorted list (2)
Insert to the tail tail->next = x ; tail = x;
Head
100 200 300 400 Tail
500xInsert to the middle cur = head; while(x->data > cur->data) { pre = cur ; cur = cur -> next; } x->next = cur; pre -> next = x;
Head
100 200 300 400
Tail
250
x
curpre
--李麗華 資料結構講義
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 tail2. When found the node, adjust the link and dispose the node.
Dispose a node p: free(p) ;
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;
--李麗華 資料結構講義
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
Head
100 200 300 500
Tail
400
pre cur
--李麗華 資料結構講義
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; }
x
100 200 300 500
y
100 200 300 500
z
--李麗華 資料結構講義
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
100 200 300 500
nex
Head Tail
100 200 300 500
Head Tail
100 200 300 500
Head Tail
100 200 300 500
pre
HeadTail
100 200 300 500
cur
nex
pre
pre
pre
cur
cur
cur
nex
nex
nex
--李麗華 資料結構講義
Circular ListDef: A circular list is a list with its tail points to the head.
Head 100 200 300 500Tail
Insert node x to headWhen list is empty head=x; tail=x; x->next = x;
Insert node x to headWhen list is not empty x->next = head; tail -> next = x; head=x;
head x tail head xtailhead
--李麗華 資料結構講義
Circular List Operation
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 tail p = head; while(p->next != tail) p= p-> next; p -> next = head; free(tail); tail = p;
Delete node p from middle 做法與一般串列相同 請參考前面內容
--李麗華 資料結構講義
Join Two Circular ListsJoin A & B circular listAtail -> next = Bhead;Btail -> next = Ahead;
Ahead100 200 300
Atail Bhead100 200 300
Btail
Ahead100 200 300
Atail Bhead100 200 300
Btail
--李麗華 資料結構講義
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.
pre data next
放左指標 放資料 放右指標
Head Tail10 10 10 10
struct node { int data ; <--資料型態可自訂 struct node *pre ; struct node *next ;}
--李麗華 資料結構講義
Double Linked List Operation(1)
Insert node x to headWhen double linked list is empty head=x; tail=x; x->next = x;
Insert node x to headWhen double linked list is not empty x->next = head; tail -> next = x; head=x;
head x tail head xtailhead
--李麗華 資料結構講義
Double Linked List Operation(2)
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 tail p = head; while(p->next != tail) p= p-> next; p -> next = head; free(tail); tail = 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.
煩惱好像讓你的腦袋有事做 ,
但卻無法幫助你解決事情