1、概述:java
一、什麼時雙向鏈表:
鏈表中的每一個節點即指向前面一個節點,也指向後面一個節點,就像丟手絹遊戲同樣,每一個人都手拉手
node
二、從頭部插入
要對鏈表進行判斷,若是爲空則設置尾節點爲新添加的節點,若是不爲空,還要設置頭節點的一個前節點爲新節點
linux
三、從尾部進行插入
若是鏈表爲空,則直接設置頭節點爲新添加的節點,不然設置尾節點的後一個節點爲新添加的節點。同時設置新添加的節點的前一個節點爲尾節點
web
四、從頭部刪除
判斷節點是否有下個節點,若是沒有則設置節點爲null,而且刪除下個節點指向前節點的指針設計模式
五、刪除尾部節點
若是頭節點沒有其它節點,把尾節點設置爲Null。不然設置尾節點前一個節點的next爲Null。設置尾節點爲前一個節點
數據結構
六、刪除方法
此時不須要記錄last的Node
刪除時使用current.previous.next= current.next;架構
package com.struct.linklist;
/** * @描述 雙向鏈表 * @項目名稱 Java_DataStruct * @包名 com.struct.linklist * @類名 LinkList * @author chenlin * @date 2010年6月26日 上午8:00:28 * @version 1.0 */
public class DoubleLinkList {
//頭
private Node first;
//尾
private Node last;
public DoubleLinkList(){
first = null;
last = null;
}
/** * 插入數據 * @param value */
public void insertFirst(long value){
Node newNode = new Node(value);
if (first == null) {
last = newNode;
}else {
first.previous = newNode;
//把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;
//first.previous = newNode;
newNode.previous = last;
}
//把最後個節點設置爲最新的節點
last = newNode;
}
public boolean isEmpty(){
return first == null;
}
/** * 刪除頭節點時要去除兩個指針,一個指向下個的next ,一個是next的previous指向前面的 * * @param value * @return */
public Node deleteFirst(){
if (first == null) {
throw new RuntimeException("鏈表數據不存在");
}
Node temp = first;
if (first.next == null) {
last = null;
}else {
first.next.previous = null;
}
first = temp.next;
return temp;
}
/** * 刪除頭節點時要去除兩個指針,一個指向下個的next ,一個是next的previous指向前面的 * * @param value * @return */
public Node deleteLast(){
if (first == null) {
throw new RuntimeException("鏈表數據不存在");
}
Node temp = last;
if (first.next == null) {
last = null;
//把第一個刪除
first = null;
}else {
last.previous.next = null;
}
last = temp.previous;
return temp;
}
/** * 刪除 * @param key * @return */
public Node deleteByKey(long key){
Node current = first;
while(current.data != key){
if (current.next == null) {
System.out.println("沒找到節點");
return null;
}
current = current.next;
}
if (current == first) {
//return deleteFirst();
//指向下個就表示刪除第一個
first = first.next;
}else {
current.previous.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) {
DoubleLinkList linkList = new DoubleLinkList();
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();
}
}
—————————————————–
(java 架構師全套教程,共760G, 讓你從零到架構師,每個月輕鬆拿3萬)
請先拍 購買地址, 下載請用百度盤
目錄以下:
01.高級架構師四十二個階段高
02.Java高級系統培訓架構課程148課時
03.Java高級互聯網架構師課程
04.Java互聯網架構Netty、Nio、Mina等-視頻教程
05.Java高級架構設計2016整理-視頻教程
06.架構師基礎、高級片
07.Java架構師必修linux運維繫列課程
08.Java高級系統培訓架構課程116課時
(送:hadoop系列教程,java設計模式與數據結構, Spring Cloud微服務, SpringBoot入門)運維
01高級架構師四十二個階段高內容:
—————————————————–svg