Package com.hephec.ds;
public class Linkedlist<t> {
Defines an internal class node node that represents the nodes of a linked list
Class node{
Saving data for a node
Private T data;
Save a reference to the next node
Private Node Next;
Non-parametric construction method
Public Node () {
}
A method of constructing a parameter
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 list and the number of nodes contained in the list
private int size;
Create an empty list
Public LinkedList () {
Empty list, Header,tail null
Header=null;
Tail=null;
}
Creates a linked list with the specified data element, with only one element of the linked list
Public LinkedList (T element) {
Header=new Node (element,null);
Only one node, header and tail, 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;
}
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&¤t!=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 start node.
Node Current=header;
for (int i=0;i<size-1&¤t!=null;i++,current=current.next) {
if (current.data.equals (element)) {
return i;
}
}
return-1;
}
Inserts an element into a chain-linear table at the specified position
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=0, that is, in the chain table header inserted
if (index==0) {
Addatheader (Element);
}
else{
Gets the previous node of the insertion point
Node prev= (node) getnodebyindex (index-1);
Let prev next point to the new node
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 head interpolation method
private 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);
If an empty list was previously inserted
if (tail==null) {
Tail=header;
}
size++;
}
Adding a new node to a linked list using the tail interpolation method
private void Add (T element) {
If the linked list is still an empty list
if (header==null) {
Header=new Node (element,null);
There is only one node where both the header and tail point to the 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++;
}
Remove the element at the specified index of a chain-linear table
Public T Delete (int index) {
if (index<0| | INDEX>SIZE-1) {
throw new Indexoutofboundsexception ("array index out of bounds");
}
Node Del=null;
If the header 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 the next node of the Delete node point to the node being deleted
Prev.next=del.next;
Del.next=null;
}
size--;
return del.data;
}
Remove the last element in the Chain Line table
Public T Remove () {
return Delete (size-1);
}
To determine if a linear table is an empty table
public Boolean isEmpty () {
return size==0;
}
Empty the linear table
public void Clear () {
Header,tail assignment is null
Header=null;
Tail=null;
size=0;
}
Public String toString () {
Determine if a linear table is empty
if (IsEmpty ()) {
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 ();
}
}
}
A chain-linear table structure implemented by Java