一、概述:
1、什么時雙向鏈表:
鏈表中的每個節(jié)點(diǎn)即指向前面一個節(jié)點(diǎn),也指向后面一個節(jié)點(diǎn),就像丟手絹游戲一樣,每個人都手拉手
2、從頭部插入
要對鏈表進(jìn)行判斷,如果為空則設(shè)置尾節(jié)點(diǎn)為新添加的節(jié)點(diǎn),如果不為空,還要設(shè)置頭節(jié)點(diǎn)的一個前節(jié)點(diǎn)為新節(jié)點(diǎn)
3、從尾部進(jìn)行插入
如果鏈表為空,則直接設(shè)置頭節(jié)點(diǎn)為新添加的節(jié)點(diǎn),否則設(shè)置尾節(jié)點(diǎn)的后一個節(jié)點(diǎn)為新添加的節(jié)點(diǎn)。同時設(shè)置新添加的節(jié)點(diǎn)的前一個節(jié)點(diǎn)為尾節(jié)點(diǎn)
4、從頭部刪除
判斷節(jié)點(diǎn)是否有下個節(jié)點(diǎn),如果沒有則設(shè)置節(jié)點(diǎn)為null,并且刪除下個節(jié)點(diǎn)指向前節(jié)點(diǎn)的指針
5、刪除尾部節(jié)點(diǎn)
如果頭節(jié)點(diǎn)沒有其它節(jié)點(diǎn),把尾節(jié)點(diǎn)設(shè)置為Null。否則設(shè)置尾節(jié)點(diǎn)前一個節(jié)點(diǎn)的next為Null。設(shè)置尾節(jié)點(diǎn)為前一個節(jié)點(diǎn)
6、刪除方法
此時不需要記錄last的Node
刪除時使用current.previous.next= current.next;
二、實(shí)現(xiàn)
package com.struct.linklist;
public class DoublelinkList {
//頭
private Node first;
//尾
private Node last;
public DoublelinkList(){
first = null;
last = null;
}
public void insertFirst(long value){
Node newNode = new Node(value);
if (first == null) {
last = newNode;
}else {
first.previous = newNode;
//把first節(jié)點(diǎn)往下移動
newNode.next = first;
}
//把插入的節(jié)點(diǎn)作為新的節(jié)點(diǎn)
first = newNode;
}
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;
}
//把最后個節(jié)點(diǎn)設(shè)置為最新的節(jié)點(diǎn)
last = newNode;
}
public boolean isEmpty(){
return first == null;
}
public Node deleteFirst(){
if (first == null) {
throw new RuntimeException("鏈表數(shù)據(jù)不存在");
}
Node temp = first;
if (first.next == null) {
last = null;
}else {
first.next.previous = null;
}
first = temp.next;
return temp;
}
public Node deleteLast(){
if (first == null) {
throw new RuntimeException("鏈表數(shù)據(jù)不存在");
}
Node temp = last;
if (first.next == null) {
last = null;
//把第一個刪除
first = null;
}else {
last.previous.next = null;
}
last = temp.previous;
return temp;
}
public Node deleteByKey(long key){
Node current = first;
while(current.data != key){
if (current.next == null) {
System.out.println("沒找到節(jié)點(diǎn)");
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("鏈表數(shù)據(jù)不存在");
return;
}
Node current = first;
while(current != null){
current.display();
current = current.next;
}
System.out.println("---------------");
}
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;
}
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;
}
public Node findByPosition(int position){
Node current = first;
//為什么是position - 1,因?yàn)橐褂帽闅v,讓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();
}
}