Bidirectional linked list to realize the source code

Source: Internet
Author: User

Bidirectional linked list to realize the source code
public class Dulinklist<t>
{
Defines an inner class Node,node instance that represents a node of a linked list
Private Class Node
{
Saving data for a node
Private T data;
Reference to previous node
Private Node prev;
Reference to the next node
Private Node Next;
constructor with no parameters
Public Node ()
{
}
Constructors that initialize all properties
Public node (T data, Node prev, node Next)
{
This.data = data;
This.prev = prev;
This.next = Next;
}
}
Save the head node of the linked list
Private Node header;
Save the tail node of the linked list
Private Node tail;
Save the number of nodes that are already contained in the list
private int size;
Create an empty list
Public Dulinklist ()
{
Empty lists, headers and tail are null
Header = null;
tail = null;
}
Creates a linked list with the specified data element, with only one element of the linked list
Public dulinklist (T Element)
{
Header = new Node (element, NULL, NULL);
Only one node, header, tail all point to that node
tail = header;
size++;
}
Returns the length of the linked list
public int Length ()
{
return size;
}
Gets the element indexed as index in a chain-linear table
Public T get (int index)
{
Return Getnodebyindex (index). data;
}
Gets the node at the specified position according to index
Private Node getnodebyindex (int index)
{
if (Index < 0 | | | index > SIZE-1)
{
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
if (index <= SIZE/2)
{
Start with the header node
Node current = header;
for (int i = 0; I <= size/2 && current! = NULL
; i++, current = Current.next)
{
if (i = = index)
{
return current;
}
}
}
Else
{
Start searching from the tail node
Node current = tail;
for (int i = size-1; i > SIZE/2 && current! = NULL
; i++, current = Current.prev)
{
if (i = = index)
{
return current;
}
}
}
return null;
}
Finding the index of a specified element in a chain-linear table
public int Locate (T element)
{
Start a search from the head node
Node current = header;
for (int i = 0; i < size && current! = NULL
; i++, current = Current.next)
{
if (current.data.equals (Element))
{
return i;
}
}
return-1;
}
Inserts an element into the specified position of a linear chain table
public void Insert (T element, int index)
{
if (Index < 0 | | | index > SIZE)
{
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
If it's an empty list,
if (header = = null)
{
Add (Element);
}
Else
{
When index is 0 o'clock, it is inserted at the end of the list
if (index = = 0)
{
Addatheader (Element);
}
Else
{
Gets the previous node of the insertion point
Node prev = Getnodebyindex (index-1);
Gets the node of the insertion point
Node next = Prev.next;
Let the next reference of the new node point to the next node, the Prev reference to the Prev node
Node NewNode = new node (element, Prev, next);
Let prev next point to the new node
Prev.next = NewNode;
Prev The next node of the Prev point to the new node
Next.prev = NewNode;
size++;
}
}
}
Adding a new node to a linked list using the tail interpolation method
public void Add (T Element)
{
If the linked list is still an empty list
if (header = = null)
{
Header = new Node (element, NULL, NULL);
Only one node, header, tail all point to that node
tail = header;
}
Else
{
Create a new node, the pre of the new node points to the original tail node
Node NewNode = new node (element, tail, NULL);
Let the tail node next point to the new node
Tail.next = NewNode;
New node as the new tail node
tail = NewNode;
}
size++;
}
Adding a new node to a linked list using the head interpolation method
public void Addatheader (T element)
{
Create a new node so that the new node's next point points to the original header
and take the new node as the new header
Header = new Node (element, null, header);
If an empty list was previously inserted
if (tail = = null)
{
tail = header;
}
size++;
}
Delete the element at the specified index in a chain-linear table
Public T Delete (int index)
{
if (Index < 0 | | | index > SIZE-1)
{
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
Node del = null;
If the header node is deleted
if (index = = 0)
{
del = header;
Header = Header.next;
Release the Prev reference for the new header node
Header.prev = null;
}
Else
{
Gets the previous node of the delete point
Node prev = Getnodebyindex (index-1);
Gets the node that will be deleted
del = Prev.next;
Let next of the deleted node point to the next node of the deleted node
Prev.next = Del.next;
Prev of the next node of the deleted node points to the Prev node
if (del.next! = null)
{
Del.next.prev = prev;
}
Prev, next reference to the deleted node is assigned null
Del.prev = null;
Del.next = null;
}
size--;
return del.data;
}
Delete the last element in a chain-linear table
Public T Remove ()
{
return Delete (size-1);
}
Determine if a chain-type linear table is an empty list
public Boolean Empty ()
{
return size = = 0;
}
Empty the linear table
public void Clear ()
{
Assigns all elements of the underlying array to null
Header = null;
tail = null;
size = 0;
}
Public String toString ()
{
When a linked list is an empty list
if (empty ())
{
Return "[]";
}
Else
{
StringBuilder sb = new StringBuilder ("[");
for (Node current = header; Current! = NULL
; Current = Current.next)
{
Sb.append (current.data.toString () + ",");
}
int len = Sb.length ();
Return Sb.delete (Len-2, Len). Append ("]"). ToString ();
}
}
Public String reversetostring ()
{
When a linked list is an empty list
if (empty ())
{
Return "[]";
}
Else
{
StringBuilder sb = new StringBuilder ("[");
for (Node current = tail; current! = NULL
; Current = Current.prev)
{
Sb.append (current.data.toString () + ",");
}
int len = Sb.length ();
Return Sb.delete (Len-2, Len). Append ("]"). ToString ();
}
}
}

Bidirectional linked list to realize the source code

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.