一、概述:
1、什麼時雙端鏈表:
鏈表中保持這對最後一個連點引用的鏈表
2、從頭部插入
要對鏈表進行判斷,如果為空白則設定尾節點為新添加的節點
3、從尾部進行插入
如果鏈表為空白,則直接設定前端節點為新添加的節點,否則設定尾節點的後一個節點為新添加的節點
4、從頭部刪除
判斷節點是否有下個節點,如果沒有則設定節點為null 二、具體實現
/** * @描述 頭尾相接的鏈表 * @項目名稱 Java_DataStruct * @包名 com.struct.linklist * @類名 LinkList * @author chenlin * @date 2010年6月26日 上午8:00:28 * @version 1.0 */public class FirstLastLinkList { //頭 private Node first; //尾 private Node last; public FirstLastLinkList(){ first = null; last = null; } /** * 插入資料 * @param value */ public void insertFirst(long value){ Node newNode = new Node(value); if (first == null) { last = newNode; }else { //把first節點往下移動 newNode.next = first; } //把插入的節點作為新的節點 first = newNode; } /** * 插入資料 * @param value */ public void insertLast(long value){ Node newNode = new Node(value); if (first == null) { first = newNode; }else { last.next = newNode; } //把最後個節點設定為最新的節點 last = newNode; } public boolean isEmpty(){ return first == null; } /** * 刪除前端節點 * @param value * @return */ public Node deleteFirst(){ if (first == null) { throw new RuntimeException("鏈表資料不存在"); } if (first.next == null) { last = null; } Node temp = first; first = temp.next; return temp; } public Node deleteByKey(long key){ Node current = first; Node last = first; while(current.data != key){ if (current.next == null) { System.out.println("沒找到節點"); return null; } last = current; current = current.next; } if (current == first) { //return deleteFirst(); //指向下個就表示刪除第一個 first = first.next; }else { last.next = current.next; } return current; } /** * 顯示所有的資料 */ public void display(){ if (first == null) { //throw new RuntimeException("鏈表資料不存在"); return; } Node current = first; while(current != null){ current.display(); current = current.next; } System.out.println("---------------"); } /** * 尋找節點1 * @param value * @return */ public Node findByValue(long value){ Node current = first; while(current != null){ if (current.data != value) { current = current.next; }else { break; } } if (current == null) { System.out.println("沒找到"); return null; } return current; } /** * 尋找節點2 * * @param key * @return */ public Node findByKey(long key) { Node current = first; while (current.data != key) { if (current.next == null) { System.out.println("沒找到"); return null; } current = current.next; } return current; } /** * 根據索引尋找對應的值 * @param position * @return */ public Node findByPosition(int position){ Node current = first; //為什麼是position - 1,因為要使用遍曆,讓current指向下一個, 所以position - 1的下個node就是要找的值 for (int i = 0; i < position - 1 ; i++) { current = current.next; } return current; } public static void main(String[] args) { FirstLastLinkList linkList = new FirstLastLinkList(); linkList.insertFirst(21); linkList.insertFirst(22); linkList.insertFirst(23); linkList.insertLast(24); linkList.insertLast(25); linkList.insertLast(26); linkList.insertLast(27); linkList.display(); System.out.println("---尋找-------------------------------------"); linkList.findByKey(25).display(); System.out.println("--刪除first-------------------------------------"); //linkList.deleteFirst().display(); ///linkList.deleteFirst().display(); //linkList.deleteFirst().display(); //linkList.deleteFirst().display(); System.out.println("-刪除指定值---------------------------------------"); linkList.deleteByKey(27).display(); linkList.deleteByKey(21).display(); System.out.println("----------------------------------------"); linkList.display(); }}