Java bidirectional cyclic chain list implementation code _java

Source: Internet
Author: User
Tags int size prev unique id uuid

Example 1:

Copy Code code as follows:



Package com.xlst.util;

Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.UUID;

/**
* Bidirectional cyclic link list
* Completion time: 2012.9.28
* Version 1.0
* @author Xlst
*
*/
public class Bothwaylooplinked {
/**
* Map containing the length of the linked list
*
* If you simply use the size base type variable of the static int type, you can only maintain a two-way circular list.
* When there are two or more two-way cyclic lists, the data will be disordered
*/
Private static final map<string, integer> sizemap = new hashmap<string, integer> ();
/**
* Two-way linked list ID
* A two-way, a unique ID
* The length of the current list can be removed from the SIZEMAP according to this ID
*/
Private String linkedid = null;

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

/**
* Previous node (self at initialization)
*/
Private bothwaylooplinked prev = this;
/**
* The latter node (itself initialized)
*/
Private bothwaylooplinked next = this;

Public bothwaylooplinked () {}

/**
* Insert a new node after the node
* @param newlinked the newly inserted node
*/
public void InsertAfter (bothwaylooplinked newlinked) {
The original front node and the back node
bothwaylooplinked Oldbefore = this;
bothwaylooplinked oldafter = This.next;

Setting the relationship between Newlinked and the former node
Oldbefore.next = newlinked;
Newlinked.prev = Oldbefore;

Setting the relationship between Newlinked and the original post node
Oldafter.prev = newlinked;
Newlinked.next = Oldafter;

Linked list length plus a
Changesize (+1);
Linkedid to bind a new node
Newlinked.linkedid = This.linkedid;
}

/**
* Insert a new node before the node
* @param newlinked the newly inserted node
*/
public void InsertBefore (bothwaylooplinked newlinked) {
The original front node and the back node
bothwaylooplinked Oldbefore = This.prev;
bothwaylooplinked Oldafter = this;

Setting the relationship between Newlinked and the former node
Oldbefore.next = newlinked;
Newlinked.prev = Oldbefore;

Setting the relationship between Newlinked and the original post node
Oldafter.prev = newlinked;
Newlinked.next = Oldafter;

Linked list length plus a
Changesize (+1);
Linkedid to bind a new node
Newlinked.linkedid = This.linkedid;
}

/**
* Delete itself from the linked list structure
* @return the deleted node
*/
Public bothwaylooplinked Remove () {
Return to remove (this);
}
/**
* Delete the 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;

Chain list length minus one
Changesize (-1);
Cancel the Linkedid of the node
This.linkedid = null;

Returns the deleted node
return linked;
}

/**
* Change the list length (default length plus 1), private method
*/
private void Changesize () {
Changesize (1);
}
/**
* Change list length (according to parameters), private method
* @param value changes in length (can be negative)
*/
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 is a reference type, and you don't have to put it back in Sizemap after you update 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 list, then as a separate list, the length is 1
* @return The length of the linked list
*/
public int GetSize () {
return (Linkedid = null)? 1:sizemap.get (This.linkedid);
}

Public bothwaylooplinked GetPrev () {
return prev;
}

Public bothwaylooplinked GetNext () {
return to Next;
}
}

Example 2:

Copy Code code as follows:



/**


* Bidirectional cyclic link list test


* @author Gkt


* @param &lt;E&gt;


*/


public class Node&lt;e&gt;


{


private E element; Node data


Private node&lt;e&gt; Next; Up node


Private node&lt;e&gt; Previous; Bottom node.


private static int size=0; Chain table length

Default closing node Next previous are 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;
}

/**


* Tail add Element E


* @param E


*/


public void Addafter (e e)


{


Defines a new node,next--&gt;;previous--&gt; head node. Previous (Tail node)


Node&lt;e&gt; newnode=new node&lt;e&gt; (e,this,this.previous==null?this:this.previous);


The header node next is empty, so it points to newnode.


if (this.next==null)


{


This.next=newnode;


}


The head node previous is empty, so it points to newnode.


if (this.previous==null)


{


This.previous=newnode;


}


This.previous.next=newnode;


This.previous=newnode;


size++;


}


/**


* Head add Element E


* @param E


*/


public void Addbefor (e e)


{


Node&lt;e&gt; newnode=new node&lt;e&gt; (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 at index


* @param E


* @param index


*/


public void Add (E e,int index)


{


Index out of Bounds


if (index&gt;=size | | index&lt;0)


{


throw new Indexoutofboundsexception ("Node.get ():" +index);


}


Else


{


INDEX&GT;SIZE/2, backward traversal


if (index&gt;size&gt;&gt;1)


{


Node&lt;e&gt; Temp=this;


for (int i=size;i&gt;index;i--)


{


temp=temp.previous;


}


Node&lt;e&gt; newnode=new node&lt;e&gt; (e,temp,temp.previous);


Temp.previous.next=newnode;


Temp.previous=newnode;


}


Else


{


Node&lt;e&gt; Temp=this;


for (int i=0;i&lt;=index;i++)


{


Temp=temp.next;


}


Node&lt;e&gt; newnode=new node&lt;e&gt; (e,temp,temp.previous);


Temp.previous.next=newnode;


Temp.previous=newnode;


}


size++;


}


}


/**


* Delete Index Element


* @param index


*/


public void Remove (int index)


{


Index out of Bounds


if (index&gt;=size | | index&lt;0)


{


throw new Indexoutofboundsexception ("Node.get ():" +index);


}


Else


{


INDEX&GT;SIZE/2, backward traversal


if (index&gt;size&gt;&gt;1)


{


Node&lt;e&gt; Temp=this;


for (int i=size;i&gt;index;i--)


{


temp=temp.previous;


}


Temp.previous.next=temp.next;


temp.next.previous=temp.previous;


}


Else


{


Node&lt;e&gt; Temp=this;


for (int i=0;i&lt;=index;i++)


{


Temp=temp.next;


}


Temp.previous.next=temp.next;


temp.next.previous=temp.previous;


}


size--;


}


}

/**
* Get index Element
* @param index
* @return
*/
Public E get (int index)
{
Index out of Bounds
if (index>=size | | index<0)
{
throw new Indexoutofboundsexception ("Node.get ():" +index);
}
Else
{
INDEX>SIZE/2, backward 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 lists is whether the head node is a dummy, and the difference between the for loop variable I when the element is fetched (the Get function):

Bidirectional cyclic list (same as Java.util package design): Because the head is dummy, the element takes from the next node in the head

One-way linked list: Head is not a dummy, the first time you must take the heads of the node elements, so the cycle and the two-way circular chain table is different. That is, the first get does not go into the for loop, returns directly to the header node, and starts from the second time into the For loop, where special attention

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.