Use of Java linked lists
Talking about the understanding after learning the linked list:
A linked list is an important data structure with a single linked list and a double linked list.
Single linked list (one-way list): consists of two parts of the data domain and node domain (node), a single linked list is like a rope with a lot of knots, each knot is equivalent to a knot, and there is a rope connection between each node, so the principle is realized through the head pointer heads of node nodes. Each node has a pointer, each of which points to the next nodal point of its own, the head of the last node is pointed to null, so that it is connected to the same chain as the string above, the operation of a single list can only start from one end, if you need to find a node in the list, You need to iterate through it from the beginning.
Single-linked list operations:
Add: The above diagram can see one-way linked list only one point, the original head for P,p point S, add nodes only need to point P to Q,q point s on it, that is: p--->q; Q--->s; this realizes the addition of one-way linked list;
Delete: the principle and add opposite, if at this time the list is P---> Q--->s; If you delete the Q node only need to change the point of p--->s, so it is deleted;
Lookup: The search operation needs to traverse the entire but linked list until the lookup condition is met;
Modification: This operation is generally based on the search, to find the point after the value of the change;
First build a node class, set a data area and node area
Use the construction method to ease the use of
public class node{The public node data//Area public node next;
Pointer Area public node (node Data,node next) {this.data = data;
This.next = Next; The public node () {} is public void SetData (Node data) {this.data = data.} public node GetData () {return data.} public Voi
D Setnext (Node next) {this.next=next;
Public Node GetNext () {return next; The public class linklist{the public node head;//Header node public int count;
The length of the record Node public linklist () {//constructor is used to initialize head = NULL;
Count = 0;
The Add public void AddNode (node data) {Nodes node = new node (data,null) for the.
Node temp = null;
If (head!= null) {temp = head;
while (Temp.getnext ()!=null) {temp = Temp.getnext ();
} temp.setnext (node);
}else{head = node;
temp = node;
} count++; }//Node delete public void Delnode (node data) {node front = NULL;//define an empty node to receive and determine if there is a node in front of the deleted node while (head!= null
{If (head.equals (data)) {break;
} Front=head;
Head = Head.getnext (); } If (HeaD!=null) {If (front==null) {head = Head.getnext ();
}else{Front.setnext (Head.getnext ());
} count--;
}else{count--; }//delete node public void delnode_count (int index) {if index<0| |
index>count-1) {System.out.println ("list index out of bounds"); Node temp = head;
Function ditto//Find the previous node for delete node for (int i=0;i<index-1;i++) {temp=temp.getnext (); ///Locate the node after the former node and the node after the node to connect//Let the node to delete the previous node, point to the deleted node after the node, that is, point to delete the node after a node Temp.setnext (Temp.getnext (). GetNext ());
The node to be removed from the past to connect, that is, to achieve the deletion of the node's operation//deletion of the list after the length of the 1-bit count--; ///To give the index lookup node public nodes findnode (int index) {if (index<0| |
index>count-1) {System.out.println ("list index out of bounds");
Node temp = head;
for (int i=0;i<index-1;i++) {temp=temp.getnext ();///Find the position of index in the linked list, indicating that the value of index node in the list is temp.getdata; return to temp;
The found data object can be returned as needed, or not returned, so it is recommended to return, so that the linked list can be encapsulated}//To find nodes public node Findnode (nodedata data) {Node temp = head; while (temp!=null) {if (temp.equals (data)) {return temp;
} temp.setnext (Temp.getnext ());
return null;
//Modify public void Updatenode (Nodedata data) {Node temp = findnode (data);
if (temp!=null) {temp.setdata (data);
}//Print public void print () {Node temp=head;
while (Temp!=null) {temp.print ();
Temp.print (Temp.getdata (). toString ());
Temp=temp.getnext (); }
}
}
Double linked list (bi-directional list): Double linked list and single linked list, with a pointing tail pointer (tail), each node of the double linked list has a head and tail pointer tail, the double linked list is easier to operate than the single linked list, and the head of the double linked table node is pointed to null. Tail refers to the tail of the next node, and the head of the tail node points to the head,tail of the previous nodes as NULL, which is a two-way relationship;
Double-linked list operation:
Package soft1;
public class Linklist {Node head;
Node tail;
int count;
Public linklist (Node head,node tail,int count) {this.head=null;
This.tail=null;
this.count=0;
Public linklist () {}///Endnote Add node public void Addheadnode (Nodedata data) {node node = new node (data,null,null);
if (head==null&&tail==null) {head=node;
Head.setfront (NULL);
Tail=node;
Tail.setnext (NULL);
}else{Head.setfront (node);
Node.setnext (head);
Head=node;
Head.setfront (NULL);
} count++;
}//Header add node public void Addtailnode (Nodedata data) {node node = new node (data,null,null);
if (head==null&&tail==null) {head=node;
Head.setfront (NULL);
Tail=node;
Tail.setnext (NULL);
}else{tail.setnext (node);
Node.setfront (tail);
Tail=node;
Tail.setnext (NULL);
} count++;
//Lookup node public nodes Findnode (nodedata data) {node temp=head;
if (Head!=null) {while (temp!=null) {if (Temp.data.compare (data)) {return temp; }
Temp=temp.getnext ();
} return null;
//delete node public void Delnode (Nodedata data) {node Temp=findnode (data);
if (temp!=null) {if (Temp.getfront () ==null) {head=temp.getnext ();
Head.setfront (NULL);
}else if (Temp.getnext () ==null) {Tail=tail.getfront ();
Tail.setnext (NULL);
}else{Temp.getfront (). Setnext (Temp.getnext ());
Temp.getnext (). Setfront (Temp.getfront ());
} count--;
}//Modify update public void Updnode (Nodedata data) {Node temp=findnode (data);
if (temp!=null) {temp.setnodedata (data);
}///Print List public void Printnode () {Node temp = head;
while (Temp!=null) {temp.print ();
Temp=temp.getnext (); }
}
}Need to write their own code summary after all put on the internet, but is a little slow, nasty ah haha