萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> jsp編程 >> java雙向循環鏈表

java雙向循環鏈表

 

代碼如下  

package com.xlst.util;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* 雙向循環鏈表
* 完成時間:2012.9.28
* 版本1.0
* @author xlst
*
*/
public class BothwayLoopLinked {
/**
* 存放鏈表長度的 map
*
* 如果簡單使用 static int 型的 size 基本類型變量,則只能維護一個雙向循環鏈表。
* 同時存在兩個及以上雙向循環鏈表時,數據會錯亂
*/
private static final Map<String, Integer> sizeMap = new HashMap<String, Integer>();
/**
* 雙向鏈表的 id
* 一個雙向一個唯一的 id
* 根據這個id可以從 sizeMap 中取出當前鏈表的長度
*/
private String linkedId = null;

/**
* 節點中的數據
*/
private Object data = null;

/**
* 前一個節點(初始化時是自身)
*/
private BothwayLoopLinked prev = this;
/**
* 後一個節點(初始化時是自身)
*/
private BothwayLoopLinked next = this;

public BothwayLoopLinked(){}

/**
* 在節點之後插入新節點
* @param newLinked 新插入的節點
*/
public void insertAfter(BothwayLoopLinked newLinked){
// 原來的前節點與後節點
BothwayLoopLinked oldBefore = this;
BothwayLoopLinked oldAfter = this.next;

// 設置 newLinked 與原前節點的關系
oldBefore.next = newLinked;
newLinked.prev = oldBefore;

// 設置 newLinked 與原後節點的關系
oldAfter.prev = newLinked;
newLinked.next = oldAfter;

// 鏈表長度加一
changeSize(+1);
// 綁定新節點的 linkedId
newLinked.linkedId = this.linkedId;
}

/**
* 在節點之前插入新節點
* @param newLinked 新插入的節點
*/
public void insertBefore(BothwayLoopLinked newLinked){
// 原來的前節點與後節點
BothwayLoopLinked oldBefore = this.prev;
BothwayLoopLinked oldAfter = this;

// 設置 newLinked 與原前節點的關系
oldBefore.next = newLinked;
newLinked.prev = oldBefore;

// 設置 newLinked 與原後節點的關系
oldAfter.prev = newLinked;
newLinked.next = oldAfter;

// 鏈表長度加一
changeSize(+1);
// 綁定新節點的 linkedId
newLinked.linkedId = this.linkedId;
}

/**
* 從鏈表結構中刪除自身
* @return 被刪除的節點
*/
public BothwayLoopLinked remove(){
return remove(this);
}
/**
* 從鏈表結構中刪除指定節點
* @param linked 要刪除的節點
* @return 被刪除的節點
*/
public BothwayLoopLinked remove(BothwayLoopLinked linked){
linked.prev.next = linked.next;
linked.next.prev = linked.prev;

linked.prev = linked;
linked.next = linked;

// 鏈表長度減一
changeSize(-1);
// 取消該節點的 linkedId
this.linkedId = null;

// 返回被刪除的節點
return linked;
}

/**
* 改變鏈表長度(默認長度加1),私有方法
*/
private void changeSize(){
changeSize(1);
}
/**
* 改變鏈表長度(根據參數),私有方法
* @param value 改變的長度值(可以為負數)
*/
private void changeSize(int value){
if(this.linkedId == null){
this.linkedId = UUID.randomUUID().toString();

sizeMap.put(linkedId, 1 + value); // sizeMap.put(linkedId, 2);
}else{
Integer size = sizeMap.get(this.linkedId);
// Integer 是引用類型,更新值之後不必再 put 回 sizeMap 裡
size += value;
}
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

/**
* 獲取鏈表的長度
* 如果是新生的節點 或 從鏈表中刪除的節點,則作為獨立鏈表,長度是 1
* @return 鏈表的長度
*/
public int getSize() {
return (linkedId == null) ? 1 : sizeMap.get(this.linkedId);
}

public BothwayLoopLinked getPrev() {
return prev;
}

public BothwayLoopLinked getNext() {
return next;
}
}

  例2

代碼如下  

/**
* 雙向循環鏈表測試
* @author GKT
* @param <E>
*/
public class Node<E>
{
private E element; //結點數據
private Node<E> next; //上結點
private Node<E> previous; //下結點
private static int size=0; //鏈表長

//默認關結點next previous都是空,
public Node()
{
this.element=null;
this.next=null;
this.previous=null;
}

private Node(E element,Node<E> next,Node<E> previous)
{
this.element=element;
this.next=next;
this.previous=previous;
}

copyright © 萬盛學電腦網 all rights reserved