One-way linked list to realize source code

Source: Internet
Author: User

One-way linked list to realize source code
public class Linklist<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 the next node
Private Node Next;
constructor with no parameters
Public Node ()
{
}
Constructors that initialize all properties
Public node (T data, node next)
{
This.data = data;
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 linklist ()
{
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 linklist (T Element)
{
Header = new Node (element, 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");
}
Start with the header node
Node current = header;
for (int i = 0; i < size && current! = NULL
; i++, current = Current.next)
{
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);
Let prev next point to the new node and let the next reference of the new node point to the next node of the original Prev
Prev.next = new Node (element, prev.next);
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);
Only one node, header, tail all point to that node
tail = header;
}
Else
{
Create a new node
Node NewNode = new node (element, 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, 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;
}
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;
The next reference to the deleted node is assigned 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 table
public Boolean Empty ()
{
return size = = 0;
}
Empty the linear table
public void Clear ()
{
Assign header, tail 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 ();
}
}
}

One-way linked list to realize 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.