Package com.session.link;
/**
* One-way linked list
*/
public class linkedlist<t> {
Private NodeHeadReference variable pointing to the node of the linked header
Private NodeTailReference variable pointing to the tail node of the chain
IntSizeCurrent total node count in linked list
Private class Node {
PrivateTDataSave data
Private NodeNextNext node
Public Node (T data, Node next) {
This.data = data;
This.Next = next;
}
}
Generating a linked list object is an empty table
Public LinkedList () {
Head =Null
Tail =Null
}
A header node (with a parametric constructor) when generating a linked list object
Public LinkedList (T data) {
Head =New Node (data,NULL);Specifies the data field value of a head node that does not point to another node
Tail =Head
size++;
}
Returns the length of the linked list
public int Length () {
ReturnSize
}
Gets the element at the specified position
PublicT GetElement (int index) {
Return Findnodebyindex (Index).Data
}
Finds the node at the specified index location
Public Node Findnodebyindex (int index) {
if (Index <0 | | Index >Size-1) {
throw New Indexoutofboundsexception ("Linear table index out of Bounds");
}
Node current =HeadMove the traverse from the beginning of the node
for (int i =0; I <Size & Current.Next! =Null i++, current = current.Next) {
if (i = = index) {
return current;
}
}
return null;
}
Finds the location of the specified element (Lookup data field holds the node location of element)
public int Findindexbyelement (T Element) {
Node current =HeadFind comparison data starting from the first node
for (int i =0; I <Size & Current.Next! =Null i++, current = current.Next) {
if (current.Data.equals (Element))
return i;
}
Return-1;
}
Insert inserts an element at a specified position
public void Insert (int index,T Element) {
if (Index <0 | | Index >Size) {
throw New Indexoutofboundsexception ("Linear table index out of Bounds");
}
if (Head = =NullIf the list is empty, call the Add method directly
{
Add (Element);
}ElseWhen a linked list is not empty
{
if (index = =0)Insert in the list header
{
Addathead (Element);
}else {
Node prev = findnodebyindex (Index-1);Locate the previous node where you want to insert the location
Prev.Next =New Node (element, prev.Next);After inserting Prev, next points to the new node, and next to the new node points to the next node of the original Prev
size++;
}
}
}
Insert tail interpolation to add a new node at the end of each link
public void Add (T Element) {
if (Head = =NULL) {
Head =New Node (element,NULL);
Tail =Head
}else {
Node NewNode =New Node (element,NULL);
Tail.Next = NewNode;
tail = NewNode;
}
size++;
}
Inserting head interpolation to join a new node in the head of a linked list
public void Addathead (T Element) {
Inserting a new node in the head is to have the new node point to the original head, and the new node as the head node of the linked list
Head =New Node (element,Head);
Newnode.next = head;
head = NewNode;
If an empty list was previously inserted
if (Tail = =NULL) {
Tail =Head
}
}
Deletes a node at a specified location returns the value of an element in a deleted node
PublicT Delete (int index) {
Node Deletenode =Null
if (Index <0 | | Index >Size-1) {
throw New Indexoutofboundsexception ("Linear table index out of Bounds");
}
if (index = =0)Delete Head node
{
Deletenode =Head
Head =Head.Next
}else {Node Prev = Findnodebyindex (index-1);//Gets the previous node of the node to be deleted Deletenode = prev.next;//the node to be deleted is the node prev next to Prev.next = deletenode.next;//Delete after prev next points to the deleted node before the next deletenode.next = null; } return deletenode.data; }//Delete the last element in the list public T Removelast () {return Delete (size-1);}//Clears all elements in the list public void clear () {head = null; tail = Null size = 0; }//Determine if the list is empty public boolean isEmpty () {return size = = 0;} public String ToString ()//output of the linked list overrides the ToString method {if (IsEmpty ()) { Return "[]"; } else {StringBuilder sb = new StringBuilder ("[");//Use StringBuilder class for (Node current = head;current! = NULL; Current = Current.next)//Traverse {Sb.append (current.data.toString () + ",") from head, and//splice the data of the node} int len = Sb.length (); Return Sb.delete (Len-1,len). Append ("]"). toString ();//Remove the last element, then add]}} public static void Main (string[] args) { linkedlist<string> list = new linkedlist<string> (); List.add ("AA"); List.add ("BB"); List.add ("CC"); SYSTEM.OUT.PRINTLN (list); List.addathead ("11"); LiSt.insert (1, "22"); SYSTEM.OUT.PRINTLN (list); List.delete (2); SYSTEM.OUT.PRINTLN (list); List.clear (); SYSTEM.OUT.PRINTLN (list); New Java.util.linkedlist<> (). Remove ()}}
JAVA One-way list