Node node 1 new Node node 1 data
- Slides: 40
Node node 1 = new Node( ); node 1. data = 1; Node node 2 = new Node( ); node 2. data = 2; node 1. link = node 2; Node node 3 = node 1; //有 new 運算子才會呼叫建構子,分配記憶體 // 將 node 1. link 設為 node 2 的記憶體位址 // 沒有 new 運算, //node 3 與 node 1 指向相同記憶體位址 // 它們目前是一體的 node 3. data = 3; // 改變 node 3 的 data 值, // 其記憶體位置與 node 1. data 相同 System. out. println( "node 1. data = " + node 1. data + " node 2. data = " + node 3. link. data); int i 1 = 1; int i 2 = 2; int i 3 = i 1; //i 3 與 i 1 的記憶體位址不同, //這將 i 1 的值放在 i 3 的記憶體位置,i 3 與 i 1 各自獨立 System. out. println( "i 1=" + i 1 +" i 2=" + i 2 + " i 3=" + i 3); i 3 = 3; System. out. println( "i 1=" + i 1 +" i 2=" + i 2 + " i 3=" + i 3); 4
5
3. 2 單向鏈結串列 串列類別 public class List { private Node front; } link 的初始值設定為 NULL 10
3. 2 單向鏈結串列 判斷給定的單向鏈結 串列是否為一空串列 的方法 boolean isempty( ) { if (front. link == null) return true; else return false; } 建立包含兩節點的單 向鏈結串列 Node front = new Node( ); Node first = new Node( ); Node second = new Node( ); front. link = first; first. data = 25; first. link = second; second. data = 78; 參考:課本 p. 3 -10 11
課本範例程式 ch 3_list. java 因上頁設計改 變造成的程式變動範例 判斷給定的單向鏈結串 列是否為一空串列的方 法 boolean isempty( ) { if (front == null) return true; else return false; } 17 建立包含兩節點的單向鏈結 串列 Node first = new Node( ); Node second = new Node( ); front = first; first. data = 25; first. link = second; second. data = 78;
3. 2 單向鏈結串列 課本範例程式 ch 3_list. java 中 del. Node(int data) 方法的改寫 (ch 3_list_2. java) 練習:將 ch 3_list. java 中 insert. Node(int data) 方法的 for 迴圈改以 while 寫過 18
23
Code: ch 3_concatenate 2 list_1. java // 將front_y串列合併到front_x串列之後,並以front_z為新串列的前端 public void cancatenate(Node front_x, Node front_y, Node front_z){ Node this_node; if(front_x. link == null) // front_x 為空串列 front_z. link = front_y. link; else{ if(front_y. link == null) // front_y 為空串列 front_z. link = front_x. link; else{ // front_x, front_y 均不為空 front_z. link = front_x. link; this_node = front_x. link; while(this_node. link != null) this_node = this_node. link; this_node. link = front_y. link; } } } 24
ch 3_concatenate 2 list_2. java // 將front_y串列合併到front_x串列之後,並以front_z為新串列的前端 public void cancatenate(Node front_x, Node rear_x, Node front_y, Node rear_y, Node front_z, Node rear_z) { Node this_node; if(front_x. link == null){ // front_x 為空串列 front_z. link = front_y. link; rear_z. link = rear_y. link; }else{ if(front_y. link == null){ // front_y 為空串列 front_z. link = front_x. link; rear_z. link = rear_x. link; }else{ // front_x, front_y 均不為空 front_z. link = front_x. link; this_node = rear_x. link; this_node. link = front_y. link; rear_z. link = rear_y. link; } } 25 }
3. 2. 3 將單向鏈結串列反轉 (ch 3_singlelist. java) front rear 22 78 31 83 NULL 反轉後的單向鍊結串列 26 98
27
28
3. 3 雙向鍊結串列 30 (ch 3_doublelist. java)
3. 3 ch 3_doublelist. java 程式範例 public void insert_node(int key){ Node new_node, prev_node, this_node; public void print_front(){ Node this_node; new_node = new Node(); new_node. data = key; new_node. l_link = null; new_node. r_link = null; if( !is_empty() ){ // 若非空串列 this_node = front. r_link; System. out. print(" ==> 串列內容為 : "); if(is_empty()){ front. r_link = new_node; rear. r_link = new_node; new_node. l_link = front; new_node. r_link = front; }else{ this_node = front. r_link; if(key < this_node. data){ front. r_link = new_node; new_node. r_link = this_node; new_node. l_link = front; this_node. l_link = new_node; }else{ 34 ……… while(this_node. r_link != front){ System. out. print(this_node. data+". "); this_node = this_node. r_link; } System. out. print(this_node. data+"n"); }else System. out. println("!!!空串列"); } public void delete_node(int key) 有多處訂正 public void print_rear( ) 由後往前印
public void delete_node(int key){ Node this_node, prev_node, temp_node; prev_node = front; this_node = front. r_link; while(this_node. r_link != front){ // 當不是最後一個節點時 if(key == this_node. data){ // temp_node = this_node; 訂正,這列不需要 prev_node. r_link = this_node. r_link; this_node. r_link. l_link = prev_node; return; } prev_node = this_node; // prev_node 往右前進一個節點 this_node = this_node. r_link; // this_node 往右前進一個節點 } if(key == this_node. data){ // 判斷最後一個節點 // temp_node = this_node; // 訂正,這列不需要 if (front. r_link == this_node) { // 訂正,新增判斷 this 是否為串列僅存的節點 front. r_link = null; rear. r_link = null; } else { prev_node. r_link = front; // 我們將最後一個節點的r_link指向front rear. r_link = prev_node; } } else System. out. println(". . . 找不到資料 "); } 35
- Struct node int data struct node* next
- Reference node and non reference node
- Reference node and non reference node
- Transverse standing waves
- Typedef struct node
- Parallel spin
- Struct node int data
- Examples of inverted commas
- New york pennsylvania new jersey delaware
- Fresh oil, new wine scripture
- Marquee cinemas new hartford
- Weaknesses and strengths of the articles of confederation
- New-old approach to creating new ventures
- What is new marketing realities
- Njbta
- New classical macroeconomics
- Chapter 16 toward a new heaven and a new earth
- Both new hampshire and new york desire more territory
- New classical and new keynesian macroeconomics
- Wilson progressive accomplishments
- Key roles for the new big data ecosystem
- Romi satria wahono
- "data is the new oil"
- Useful vs usable
- Nursing process definition
- Components of gis
- Tentukan simpangan baku dari data 2 3 4 5 6
- Perbedaan data primer dan data sekunder
- Spatial data and attribute data
- Data reliability and validity
- Data-data monitoring lingkungan merupakan gambaran dari
- Snapshot standby
- Categorical data vs numerical data
- Oracle coast guard
- Continuous and discrete data
- Data reduction in data mining
- What is kdd process in data mining
- Contoh data warehouse dan data mart
- Source data component in data warehouse
- What is task abstraction in data visualization
- Data quality and data cleaning an overview