Since there is no struct in Java, a class is used to define the linked list, and the code is as follows
Includes a data, and a pointer to the next node
Rewrite the ToString function to return the data you want
Define the class of the linked list:
Package Linknode;
public class Linknode {
public String data;
Public Linknode Next;
Public String GetData () {
return data;
}
public void SetData (String data) {
This.data=data;
}
Public Linknode GetNext () {
return next;
}
public void Setnext (Linknode next) {
This.next=next;
}
Public Linknode (String Data,linknode next) {
Super ();
This.data=data;
This.next=next;
}
Public Linknode () {
Super ();
}
@Override
Public String toString () {
Return "Data:" +data+ "next->" +next;
}
}
1. Initialize the linked list:
public static void Initlinknode (Linknode L) {
L.setdata ("#");
L.setnext (NULL);
}
2. Traverse the list to return the length of the linked list
public static int traverse (Linknode L) {
Linknode p = L;
int count = 1;
while (P.next!=null) {
p = p.next;
count++;
}
return count;
}
3. Convert the data of the linked list L to StringBuffer output, the output is a string
public static StringBuffer Outputlinknode (Linknode L) {
StringBuffer str = new StringBuffer ("");
Linknode p = L;
For (@SuppressWarnings ("unused")
int i=0;;) {
Str.append (P.data);
if (p.next!=null) {
p = p.next;
}
else{
Break
}
}
return str;
}
4. Insert a node at the end of the list L with a value of data
public static void Insertlinknode (Linknode l,string data) {
Linknode p = L;
Linknode q = new Linknode ();
For (@SuppressWarnings ("unused")
int i=0;;) {
if (p.next==null) {
Q.setdata (data);
Q.setnext (NULL);
P.setnext (q);
System.out.println ("Insert" +data+ "success.");
Break
}
else{
p = p.next;
}
}
}
5. Delete nth node (starting from 0)
public static void Deletelinknode (Linknode l,int N) {
int count = 1;
Linknode p = L;
For (@SuppressWarnings ("unused")
int i;;) {
if (count = = N) {
P.setnext (P.next.next);
Break
}
else{
count++;
p = p.next;
}
}
}
6. start traversal from Index=n, or return True if STR is later returned false
public static int LastIndex (Linknode l,string str) {
Linknode p = L;
int flag = 0;
for (int i=0;i<traverse (L); i++) {
if (P.DATA==STR) {
System.out.println (i);
flag = i;
}
p = p.next;
}
return flag;
}
Test procedure:
Package Linknode;
public class Quarrel extends method{
public static void Main (string[] args) {
Linknode L = new Linknode ();
SYSTEM.OUT.PRINTLN ("Initialize:");
Initlinknode (L);
System.out.println (L.tostring ());
System.out.println ("Insert node:");
Insertlinknode (L, "R");
Insertlinknode (L, "R");
Insertlinknode (L, "L");
Insertlinknode (L, "L");
Insertlinknode (L, "R");
Insertlinknode (L, "L");
System.out.println (L.tostring ());
int count = Traverse (L);
System.out.println ("Number of nodes:" +count);
StringBuffer str = outputlinknode (L);
System.out.println (str);
The position of the last L
int lastindex = lastindex (L, "L");
System.out.println ("The last position of L:" +lastindex);
System.out.println ("Remove a Node");
Deletelinknode (l,2);
Count = Traverse (L);
System.out.println ("Number of nodes:" +count);
str = Outputlinknode (L);
System.out.println (str);
System.out.println (L.tostring ());
}
}
The results are as follows:
Simple operation of the Java linked list (Linknode): initialization, traversal, insertion, deletion, etc.