更新時間:2019-08-21 13:59:11 來源:動力節(jié)點 瀏覽2239次
一、概述:
1、什么時雙向鏈表:
鏈表中的每個節(jié)點即指向前面一個節(jié)點,也指向后面一個節(jié)點,就像丟手絹游戲一樣,每個人都手拉手
2、從頭部插入
要對鏈表進(jìn)行判斷,如果為空則設(shè)置尾節(jié)點為新添加的節(jié)點,如果不為空,還要設(shè)置頭節(jié)點的一個前節(jié)點為新節(jié)點
3、從尾部進(jìn)行插入
如果鏈表為空,則直接設(shè)置頭節(jié)點為新添加的節(jié)點,否則設(shè)置尾節(jié)點的后一個節(jié)點為新添加的節(jié)點。同時設(shè)置新添加的節(jié)點的前一個節(jié)點為尾節(jié)點
4、從頭部刪除
判斷節(jié)點是否有下個節(jié)點,如果沒有則設(shè)置節(jié)點為null,并且刪除下個節(jié)點指向前節(jié)點的指針
5、刪除尾部節(jié)點
如果頭節(jié)點沒有其它節(jié)點,把尾節(jié)點設(shè)置為Null。否則設(shè)置尾節(jié)點前一個節(jié)點的next為Null。設(shè)置尾節(jié)點為前一個節(jié)點
6、刪除方法
此時不需要記錄last的Node
刪除時使用current.previous.next= current.next;
二、實現(xiàn)
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;
}
/**
* 插入數(shù)據(jù)
* @param value
*/
public void insertFirst(long value){
Node newNode = new Node(value);
if (first == null) {
last = newNode;
}else {
first.previous = newNode;
//把first節(jié)點往下移動
newNode.next = first;
}
//把插入的節(jié)點作為新的節(jié)點
first = newNode;
}
/**
* 插入數(shù)據(jù)
* @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;
}
//把最后個節(jié)點設(shè)置為最新的節(jié)點
last = newNode;
}
public boolean isEmpty(){
return first == null;
}
/**
* 刪除頭節(jié)點時要去除兩個指針,一個指向下個的next ,一個是next的previous指向前面的
*
* @param value
* @return
*/
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;
}
/**
* 刪除頭節(jié)點時要去除兩個指針,一個指向下個的next ,一個是next的previous指向前面的
*
* @param value
* @return
*/
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;
}
/**
* 刪除
* @param key
* @return
*/
public Node deleteByKey(long key){
Node current = first;
while(current.data != key){
if (current.next == null) {
System.out.println("沒找到節(jié)點");
return null;
}
current = current.next;
}
if (current == first) {
//return deleteFirst();
//指向下個就表示刪除第一個
first = first.next;
}else {
current.previous.next = current.next;
}
return current;
}
/**
* 顯示所有的數(shù)據(jù)
*/
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("---------------");
}
/**
* 查找節(jié)點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;
}
/**
* 查找節(jié)點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;
}
/**
* 根據(jù)索引查找對應(yīng)的值
* @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();
}
}
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743