Java bidirectional cyclic linked list

Source: Internet
Author: User

 

The Code is as follows:  

Package com.xls t. util;

Import java. util. HashMap;
Import java. util. Map;
Import java. util. UUID;

/**
* Bidirectional cyclic linked list
* Completion time: 2012.9.28
* Release 1.0
* @ Author xlst
*
*/
Public class BothwayLoopLinked {
/**
* Map storing the length of the linked list
*
* If a variable of the Basic size Type of the static int type is used, only one bidirectional cyclic linked list can be maintained.
* If two or more two-way circular linked lists exist at the same time, the data will be disordered.
*/
Private static final Map <String, Integer> sizeMap = new HashMap <String, Integer> ();
/**
* Id of a two-way linked list
* A two-way unique id
* Based on this id, the length of the current linked list can be retrieved from sizeMap.
*/
Private String Region ID = null;

/**
* Data in a node
*/
Private Object data = null;

/**
* Previous node (itself during initialization)
*/
Private BothwayLoopLinked prev = this;
/**
* The next node (itself during initialization)
*/
Private BothwayLoopLinked next = this;

Public BothwayLoopLinked (){}

/**
* Insert a new node after the node
* @ Param newLinked the newly inserted Node
*/
Public void insertAfter (BothwayLoopLinked newLinked ){
// Original front and back nodes
BothwayLoopLinked oldBefore = this;
BothwayLoopLinked oldAfter = this. next;

// Set the relationship between newLinked and the original Node
OldBefore. next = newLinked;
NewLinked. prev = oldBefore;

// Set the relationship between newLinked and the original Node
OldAfter. prev = newLinked;
NewLinked. next = oldAfter;

// Add one to the length of the linked list
ChangeSize (+ 1 );
// Bind the consumer ID of the new node
NewLinked. Consumer id = this. Consumer ID;
}

/**
* Insert a new node before the node
* @ Param newLinked the newly inserted Node
*/
Public void insertBefore (BothwayLoopLinked newLinked ){
// Original front and back nodes
BothwayLoopLinked oldBefore = this. prev;
BothwayLoopLinked oldAfter = this;

// Set the relationship between newLinked and the original Node
OldBefore. next = newLinked;
NewLinked. prev = oldBefore;

// Set the relationship between newLinked and the original Node
OldAfter. prev = newLinked;
NewLinked. next = oldAfter;

// Add one to the length of the linked list
ChangeSize (+ 1 );
// Bind the consumer ID of the new node
NewLinked. Consumer id = this. Consumer ID;
}

/**
* Delete itself from the linked list Structure
* @ Return the deleted Node
*/
Public BothwayLoopLinked remove (){
Return remove (this );
}
/**
* Delete a specified node from the linked list Structure
* @ Param linked the node to be deleted
* @ Return the deleted Node
*/
Public BothwayLoopLinked remove (BothwayLoopLinked linked ){
Linked. prev. next = linked. next;
Linked. next. prev = linked. prev;

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

// Reduce the length of the linked list by one
ChangeSize (-1 );
// Cancel the Region ID of the node
This. Upload id = null;

// Return the deleted Node
Return linked;
}

/**
* Change the length of the linked list (the default length is increased by 1). Private Method
*/
Private void changeSize (){
ChangeSize (1 );
}
/**
* Changing the length of a linked list (based on parameters), private Method
* @ Param value the length value changed (it can be a negative number)
*/
Private void changeSize (int value ){
If (this. Upload id = null ){
This. Upload id = UUID. randomUUID (). toString ();

SizeMap. put (sequence ID, 1 + value); // sizeMap. put (sequence ID, 2 );
} Else {
Integer size = sizeMap. get (this. Upload ID );
// Integer is a reference type. You do not need to put it back to sizeMap after updating the value.
Size + = value;
}
}

Public Object getData (){
Return data;
}

Public void setData (Object data ){
This. data = data;
}

/**
* Get the length of the linked list
* If it is a new node or a node deleted from the linked list, it will be used as an independent linked list with a length of 1
* @ Return the length of the linked list
*/
Public int getSize (){
Return (Response id = null )? 1: sizeMap. get (this. Your ID );
}

Public BothwayLoopLinked getPrev (){
Return prev;
}

Public BothwayLoopLinked getNext (){
Return next;
}
}

Example 2

The Code is as follows:  

/**
* Bidirectional cyclic Linked List Test
* @ Author GKT
* @ Param <E>
*/
Public class Node <E>
{
Private E element; // node data
Private Node <E> next; // The upper Node
Private Node <E> previous; // lower Node
Private static int size = 0; // linked list Length

// By default, the next previous parameter is empty,
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;
}

/**
* Add Element e at the end
* @ Param e
*/
Public void addAfter (E e)
{
// Define the new node, next --> header node; previous --> header node. previous (tail node)
Node <E> newNode = new Node <E> (e, this, this. previous = null? This: this. previous );
// If the header node next is null, point it to newNode.
If (this. next = null)
{
This. next = newNode;
}
// If the header node previous is null, point it to newNode.
If (this. previous = null)
{
This. previous = newNode;
}
This. previous. next = newNode;
This. previous = newNode;
Size ++;
}
/**
* Add Element e to the header
* @ Param e
*/
Public void addBefor (E e)
{
Node <E> newNode = new Node <E> (e, this. next = null? This: this. next, this );
If (this. next = null)
{
This. next = newNode;
}
If (this. previous = null)
{
This. previous = newNode;
}
This. next. previous = newNode;
This. next = newNode;
Size ++;
}
/**
* Add element e to index
* @ Param e
* @ Param index
*/
Public void add (E e, int index)
{
// The index is out of bounds.
If (index> = size | index <0)
{
Throw new IndexOutOfBoundsException ("Node. get ():" + index );
}
Else
{
// Index> size/2, reverse Traversal
If (index> size> 1)
{
Node <E> temp = this;
For (int I = size; I> index; I --)
{
Temp = temp. previous;
}
Node <E> newNode = new Node <E> (e, temp, temp. previous );
Temp. previous. next = newNode;
Temp. previous = newNode;
}
Else
{
Node <E> temp = this;
For (int I = 0; I <= index; I ++)
{
Temp = temp. next;
}
Node <E> newNode = new Node <E> (e, temp, temp. previous );
Temp. previous. next = newNode;
Temp. previous = newNode;
}
Size ++;
}
}
/**
* Delete the index Element
* @ Param index
*/
Public void remove (int index)
{
// The index is out of bounds.
If (index> = size | index <0)
{
Throw new IndexOutOfBoundsException ("Node. get ():" + index );
}
Else
{
// Index> size/2, reverse Traversal
If (index> size> 1)
{
Node <E> temp = this;
For (int I = size; I> index; I --)
{
Temp = temp. previous;
}
Temp. previous. next = temp. next;
Temp. next. previous = temp. previous;
}
Else
{
Node <E> temp = this;
For (int I = 0; I <= index; I ++)
{
Temp = temp. next;
}
Temp. previous. next = temp. next;
Temp. next. previous = temp. previous;
}
Size --;
}
}

/**
* Obtain the index element.
* @ Param index
* @ Return
*/
Public E get (int index)
{
// The index is out of bounds.
If (index> = size | index <0)
{
Throw new IndexOutOfBoundsException ("Node. get ():" + index );
}
Else
{
// Index> size/2, reverse Traversal
If (index> size> 1)
{
Node <E> temp = this;
For (int I = size; I> index; I --)
{
Temp = temp. previous;
}
Return temp. element;
}
Else
{
Node <E> temp = this;
For (int I = 0; I <= index; I ++)
{
Temp = temp. next;
}
Return temp. element;
}
}
}
Public int size ()
{
Return size;
}

Public static void main (String a [])
{
Node node = new Node ();
Node. addAfter ("1 ");
Node. addAfter ("2 ");
Node. addAfter ("3 ");
Node. addBefor ("0 ");
Node. add ("7", 0 );
System. out. println (node. get (0 ));
System. out. println (node. get (1 ));
System. out. println (node. get (2 ));
System. out. println (node. get (3 ));
System. out. println (node. get (4 ));

}
}

The biggest difference between the two linked lists is whether the header node is a dummy element and the for loop variable I when the element (get function) is retrieved:

Two-way cyclic linked list (same as the java. util package design): Because the head is a dummy element, the element is retrieved from the next node of the head.

One-way linked list: the head is not a dummy element. For the first time, the element of the head header node must be taken. Therefore, the loop is different from the bidirectional cyclic linked list. That is to say, the first get does not enter the for loop, and the header node is directly returned. The for loop is started only after the second get.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.