對鍊錶的操作比較難的就是反轉,近期會再寫一篇關於鍊錶反轉的。
/**
* 定義乙個鍊錶類
* 實現頭插、尾插、任意位置插入、指定位置查詢
* 指定位置刪除、反轉、列印、取長度操作
*/final class link
/*** 將結點定義為乙個私有內部類
*/private class node
node(int data)
}
/**尾插 */
public void inserttail(int data)
cur.next = n;
}/**頭插 */
public void inserthead(int data)
/**取長度 */
public int getlenth()
return count;
}/**制定位置插入 */
public void insert(int data,int position)
n.next = cur.next;
cur.next = n;
}/**刪除指定位置結點 */
public void del(int position)
cur.next = cur.next.next;
}/**
* 使用頭插來反轉鍊錶
* 將下乙個節點插在前面
* 再將後面的結點刪除
*/public void reverse()
}/**
* 迭代法進行反轉
*/public void reverse2()
head.next = prev;
}/**指定位置查詢 */
public int search(int position)
return cur.data;
}/**列印鍊錶 */
public void display()
}}
Java 鍊錶
鍊錶結點 package com.darren.test.datastructure 鍊錶節點 author darren.zhang public class link public int getvalue public void setvalue int value public link g...
鍊錶(Java)
該 於部落格 先構造 節點物件node 鏈結點,相當於車廂 public class node 顯示方法 public void display 再構造 鍊錶物件linklist,現實方法刪除插入 鍊錶,相當於車頭 public class linklist 插入乙個節點,在頭結點後進行插入 pub...
鍊錶(java)
1 節點 2 鍊錶 3 測試類 1 節點 package com.cwq.ch04 節點,先定義每個資料的格式 author carpoor date 2019年1月29日 public class node public void display 2 鍊錶 package com.cwq.ch04...