資料結構
DESCRIPTION
資料結構. 老師:李崇明 助教:楊斯竣. 大綱. 認識 建構元與建構元的多載 認識「類別變數」與「類別函數」 認識類別型態的變數 學習利用陣列來儲存物件 認識內部 類別. 建構元的基本認識. 建構元( constructor )是幫助新建立的物件設定初值 建構元的名稱必須與其所屬之類別的類別名稱相同 建構元可視為一種特殊的函數,其語法如下:. 建構元的定義 格式. 修飾子 類別名稱 ( 型態 1 引數 1, 型態 2 引數 2,...) { 程式敘述 ; .... }. 可以是 public 或 private. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/1.jpg)
Providence University
1/24Dept. of Computer Science and Information Engineering
資訊工程學系
資料結構
老師:李崇明助教:楊斯竣
![Page 2: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/2.jpg)
Providence University
2/24Dept. of Computer Science and Information Engineering
資訊工程學系
學習目標• 認識鏈結串列的基本觀念與結構。• 了解鏈結串列的基本運算。• 知道如何以適當的方式來表示鏈結串列。• 認識鏈結串列的應用與相關的演算法。
![Page 3: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/3.jpg)
Providence University
3/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.1 鏈結串列 (linked list) 的基本結構與特性
•載運旅客的火車把好幾個車廂一個接一個地連起來,靠火車頭的動力與車廂間的連結力來行駛,這種透過鏈結把相似個體連接起來的結構,就是一種鏈結串列。
•火車的車廂一定都按照車廂號碼的順序排列,所以不管你從月台的哪個入口進站,只要知道目前所在的車廂位置,就一定能循序往前或往後找到自己要上的車廂。
•列車長查票的時候也要從最前頭的車廂開始,依序到每一個車廂查票,才不會漏掉。
![Page 4: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/4.jpg)
Providence University
4/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.1.1 鏈結串列的基本結構• [ 鏈結串列的形成 ]• [ 鏈結串列的節點結構 ]• [ 單向鏈結串列 (singly linked list) 的
定義 ]
![Page 5: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/5.jpg)
Providence University
5/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 鏈結串列的形成 ]
• 鏈結串列的元素透過鏈結連接在一起,除了頭尾兩個元素以外,每個元素都有之前與之後的兩個相鄰的元素。
• 帶頭的元素之前沒有元素,之後則連接一個元素,最尾端的元素之前連接了一個元素,之後則沒有元素。
![Page 6: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/6.jpg)
Providence University
6/24Dept. of Computer Science and Information Engineering
資訊工程學系
鏈結串列形成的實例
![Page 7: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/7.jpg)
Providence University
7/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 鏈結串列的節點結構 ]
• 鏈結串列中的元素可以用一個節點來表示,節點包括兩個主要的部分 : 資料(data) 與鏈結 (link) ,鏈結儲存的是下一個節點的位址,下一個節點的鏈結則儲存下下一個節點的位址。
![Page 8: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/8.jpg)
Providence University
8/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 單向鏈結串列 (singly linked list)的定義 ]
• 頭節點在結構上看起來跟其他的節點沒有什麼差異,但是前面沒有其他的節點。
• 尾節點的鏈結是空值 (null) ,因為沒有下一個節點。
![Page 9: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/9.jpg)
Providence University
9/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.1.2 鏈結串列的基本特性• 鏈結串列透過鏈結把元素串起來,元素越
多則串列越龐大。• 雖然串列中的元素有一定的順序,但是我
們並沒有像陣列那樣用編號來加以區別。• 串列中元素的增加與移除也不必按照特定
的順序。
![Page 10: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/10.jpg)
Providence University
10/24Dept. of Computer Science and Information Engineering
資訊工程學系
鏈結串列的特性 (I)
1. 假如鏈結串列中沒有任何元素,則鏈結串列就是空的(empty)。由於在定義上並沒有限制鏈結串列的成長,所以除非特別規定,否則沒有鏈結串列滿的(full)情況。
2. 下面的圖顯示的是一個含有單一節點的鏈結串列,從鏈結串列的定義來看,這個節點有部分頭節點的特徵,也有部分尾節點的特徵。鏈結串列中的節點都會有鏈結,這樣才可以連結其他的節點。
![Page 11: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/11.jpg)
Providence University
11/24Dept. of Computer Science and Information Engineering
資訊工程學系
鏈結串列的特性 (II)
1. 當我們把元素加入到只有單一節點的鏈結串列時,有兩種方式,一種是從前面加入,另外一種是從後面加入。
2. 當鏈結串列繼續擴充時,還有第 3種加入節點的方式,就是從中間的任何兩個節點之間加入新節點。
![Page 12: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/12.jpg)
Providence University
12/24Dept. of Computer Science and Information Engineering
資訊工程學系
鏈結串列的特性 (III)
1. 由於鏈結串列中的節點沒有編號,無法像陣列元素那樣直接以索引來隨機存取,所以要從頭節點開始,透過節點之間的鏈結來走訪(traversing),找到需要存取的元素。當走訪碰到鏈結為空值的時候,就代表已經到達尾節點了。
2. 一旦透過走訪找到元素以後,就可以進行刪除、修改、讀取等操作。串列元素的刪除沒有特定的順序,可以從頭、從尾,或是從中間刪除節點。
![Page 13: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/13.jpg)
Providence University
13/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.2 鏈結串列的基本操作• 鏈結串列也可以看成是一種串列,只是節
點中多了記載其他節點位址的鏈結,多了鏈結的好處是可以透過鏈結來引用其他節點的資料,或是走訪整個串列。
• 一般的串列不用鏈結,必須靠陣列索引來存取節點的資料,鏈結串列有鏈結可用,所以有些操作就可以運用鏈結來進行。
![Page 14: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/14.jpg)
Providence University
14/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.2.1 頭節點的問題• 鏈結串列中頭節點 (header node) 的使用
具有十分特殊的意義,我們在概念上可以把頭節點看成跟其他的節點相同,但是在鏈結串列的實務上,頭節點的處理會跟串列中其他的節點不同,目的在於維持一般節點操作程序的一致性、提高效率。
![Page 15: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/15.jpg)
Providence University
15/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 解決的辦法 ]
• 把頭節點當成特殊的節點來處理,不像其他的節點那樣專門用來儲存同類型的資料,頭節點裡可以記載一些與串列相關的資訊,例如串列中節點的數目。
![Page 16: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/16.jpg)
Providence University
16/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 對於操作的影響 ]
• 在鏈結串列建立的時候馬上加入一個頭節點,格式與其他的串列節點一樣,但是不用來儲存資料,只是為了讓鏈結串列在節點的處理操作上有一致性、提高執行時的效能。
![Page 17: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/17.jpg)
Providence University
17/24Dept. of Computer Science and Information Engineering
資訊工程學系
串列節點的插入 (insert)
• 節點可以插入到頭節點之後、尾節點之後,或是一般節點之後,但是不能插入到頭節點之前,這樣才能維持操作的一致性。下圖顯示如何將節點 P 插入到 Pi 之後。
![Page 18: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/18.jpg)
Providence University
18/24Dept. of Computer Science and Information Engineering
資訊工程學系
串列節點的刪除 (delete)
• 規定串列的頭節點不能刪除,其他的節點都可以刪除。下圖顯示如何將節點 P 從串列中刪除。
![Page 19: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/19.jpg)
Providence University
19/24Dept. of Computer Science and Information Engineering
資訊工程學系
3.2.2 基本操作• [ 鏈結串列的產生 ]• [ 檢查鏈結串列是否為空的 (isEmpty)]• [ 插入節點 (insert)]• [ 刪除節點 (delete)]• [ 鏈結串列的走訪 (traversal)]
![Page 20: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/20.jpg)
Providence University
20/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 鏈結串列的產生 ]
• 鏈結串列是由節點所形成的,必須先產生節點,下面以 ListNode 類別來定義一個鏈結串列的節點,一個節點包含兩個主要的資料成員,即所儲存的資料 (底下的 data屬性 ) 、以及指向下一個節點的鏈結 (底下的 link屬性 ) 。
![Page 21: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/21.jpg)
Providence University
21/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 檢查鏈結串列是否為空的(isEmpty)]
• 簡單地說,只要鏈結串列中沒有任何節點就表示鏈結串列是空的,一個非空的鏈結串列至少要有一個節點。
• 串列的元素可以新增或刪除,所以有可能碰到串列沒有元素的情況,這時候存在的就是一個空串列。
![Page 22: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/22.jpg)
Providence University
22/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 插入節點 (insert)]
![Page 23: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/23.jpg)
Providence University
23/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 刪除節點 (delete)]
![Page 24: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/24.jpg)
Providence University
24/24Dept. of Computer Science and Information Engineering
資訊工程學系
[ 鏈結串列的走訪 (traversal)]
• 鏈結串列在使用時可能常會碰到走訪(traversal) 的情況,也就是一個接著一個地拜訪鏈結串列中的節點。
• 走訪有很多種目的,我們可能會走訪所有的節點,得到串列中節點的數目,代表串列的長度 (length);假如想知道串列是否有某個數值,也是要透過走訪,檢視串列元素中儲存的資料,這種操作可以稱為找尋 (find) 。
![Page 25: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/25.jpg)
Providence University
25/24Dept. of Computer Science and Information Engineering
資訊工程學系
實作 Linked List
• 使用 Java Collections Framework 中的Linked List 類別實作。
• 不用從頭寫一個全新的 Linked List功能程式。
• Linked List 在 Java API架構中的位置:– import java.util.*;
• 宣告 Linked List物件– LinkedList < 資料型態 >物件名 = new
LinkedList < 資料型態 >();• Linked List 可以直接輸出
– System.out.println(物件名 );
25
![Page 26: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/26.jpg)
Providence University
26/24Dept. of Computer Science and Information Engineering
資訊工程學系
插入尾端節點 (addLast)
• 使用 addLast 方法可直接將元素插入原Linked List 尾端– 1. 產生新節點 (data 為元素值, link指向 null)– 2. 將原 linked list 最尾端 link指向新節點。
• 程式碼:–物件名 .add( 元素 );– 例如: obj.addLast(“Joe”);
26
John null
Joe null
![Page 27: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/27.jpg)
Providence University
27/24Dept. of Computer Science and Information Engineering
資訊工程學系
插入前端節點 (addFirst)
• 執行步驟– 1. 產生新節點 (data 為元素值, link指向 null)– 2. 將新節點的 link指向原 linked list 最前端節
點。• 程式碼:
–物件名 .add( 元素 );– 例如: obj.addFirst(“Allen”);
27
![Page 28: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/28.jpg)
Providence University
28/24Dept. of Computer Science and Information Engineering
資訊工程學系
插入節點 (add)
• 若無索引值,可將元素插入原 Linked List 尾端 (功能與 addLast 相同 )– 1. 產生新節點 (data 為元素值, link指向 null)– 2. 將原 linked list 最尾端 link指向新節點。
• 若有索引值,則可在索引值位置上插入節點– 1. 產生新節點 (data 為元素值, link指向 null)– 2. 新節點的 link指向索引值位置的節點。– 3. 索引值的前節點 link指向新節點。
• 程式碼:– obj.add(“Martin”);– obj.add(1, “Richard”);
28
![Page 29: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/29.jpg)
Providence University
29/24Dept. of Computer Science and Information Engineering
資訊工程學系
刪除尾端節點 (removeLast)
• 刪除 linked list 中的最後節點– 將倒數第 2 個節點的 link改為 null 。– 刪除最後節點 (回收空間 ) 。
• 程式碼:– obj.removeLast();
29
![Page 30: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/30.jpg)
Providence University
30/24Dept. of Computer Science and Information Engineering
資訊工程學系
刪除前端節點 (removeFirst)
• 刪除 linked list 中的最前端節點– 將開頭改為第 2 個節點。– 刪除第 1 個節點 (回收空間 ) 。
• 程式碼:– obj.removeFirst();
30
![Page 31: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/31.jpg)
Providence University
31/24Dept. of Computer Science and Information Engineering
資訊工程學系
刪除節點 (remove)
• 給予索引值或元素,刪除該索引值或元素之節點。– 將欲刪除節點的前節點,指向下個節點。– 刪除節點。 (回收空間 )
• 程式碼:– obj.remove(“Joe”);– obj.remove(1);
31
![Page 32: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/32.jpg)
Providence University
32/24Dept. of Computer Science and Information Engineering
資訊工程學系
取得目前長度 (size)
• 使用 size 方法取得目前鏈結串列長度– obj.size();
• 其他 Linked List 相關方法:• http://download.oracle.com/javase/6/docs/
api/java/util/LinkedList.html
32
![Page 33: 資料結構](https://reader036.vdocuments.pub/reader036/viewer/2022081519/5681313a550346895d97af64/html5/thumbnails/33.jpg)
Providence University
33/24Dept. of Computer Science and Information Engineering
資訊工程學系33