The following is a small series for everyone to share a use of Java write single linked list, there are questions welcome to my message oh.
First define a node class
public class Node {
protected node next;//pointer field public
int data;//data domain public
Node (int data) {This
. data = data;
}
Displays this node public
void Display () {
System. Out.print (Data + "");
}
Next, define a single linked list and implement the relevant methods:
public class Linklist {public Node first;//define a head node private int pos = 0;//node position public linklist () {this.first = null; //Insert a header node public void Addfirstnode (int data) {node node = new node (data); node.next = first; first = node;}//Delete a header node, and returns the head node public node Deletefirstnode () {node Tempnode = first; a = Tempnode.next; return tempnode;}//Insert nodes at any point in I
Ndex the following inserts public void Add (int index, int data) {node node = new node (data);
Node current = i;
Node previous = i;
while (POS!= index) {previous = current; current = Current.next; pos++;} node.next = current;
Previous.next = node;
pos = 0;
//delete nodes in any location public node Deletebypos (int index) {node current = i;
Node previous = i; while (POS!= index) {pos++-previous = current, current = Current.next;} if (current = =-
else {pos = 0; previous.next = current.next;} return current;
///Per node data Delete node (delete first only) public node deletebydata (int data) {node: Node Previous= A; Remember the previous node while (Current.data!= data) {if (Current.next = = null) {return null;} previous = current;
. Next;
} if (current = = i) {i = First.next} else {previous.next = Current.next;} //Show all node information public void Displayallnodes () {Node current = A-I (current!= null) {Current.display ();
= Current.next;
} System.out.println (); ///According to location lookup node information public node Findbypos (int index) {Node current = i if (pos!= index) {current = Current.next; pos++
;
return to current; /////Based on data lookup node information public node findbydata (int data) {node current = Current.data!= data) {if (Current.next =
= null) return null;
current = Current.next;
return to current; }
}
Finally, we can test the test class to do the relevant testing:
public class Testlinklist {public
static void Main (string[] args) {
linklist linklist = new linklist ();
Linklist.addfirstnode (a);
Linklist.addfirstnode ();
Linklist.addfirstnode (a);
print19,21,20
Linklist.add (1);//print19,22,21,20
Linklist.add (2);//print19,22,23,21,20
Linklist.add (3, 99); print19,22,23,99,21,20
//Call this method will print 19,22,23,99,21,20
linklist.displayallnodes ();
}
So far, the single linked list of operations on the note here.
The above is a small set to introduce the Java single linked list of the implementation code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!