Simple one-way linked list
Class node {private string data; // stores the content of the current node. Private node next = NULL; // stores the public node (string data) {This. setdate (data);} public void setdate (string data) {This. data = data;} public void setnext (node next) {This. next = next;} Public String getdate () {return this. data;} public node getnext () {return this. next ;}} public class linkdemo01 {public static void main (string ARGs []) {node n1 = new node ("node-a"); node N2 = New node ("node-B"); node N3 = new node ("node-c"); node N4 = new node ("node-d "); n1.setnext (N2); n2.setnext (N3); n3.setnext (n4); printnode (N1);} public static void printnode (node) {system. out. println (node. getdate (); If (node. getnext ()! = NULL) {printnode (node. getnext ());}}}
One-way linked list integration internal class
Class link {class node {private stringdata; private nodenext = NULL; Public node (string data) {This. setdata (data);} public void setdata (string data) {This. data = data;} public void setnext (node next) {This. next = next;} Public String getdata () {return this. data;} public nodegetnext () {return this. next;} public void add (node) {If (this. next = NULL) {This. next = node;} else {This. next. add (node) ;}} public void print (){ If (this. next = NULL) {system. out. println (this. getdata ();} else {system. out. println (this. getdata (); this. next. print () ;}} public Boolean search (string data) {// internal search method if (data. equals (this. data) {return true;} else {// continue to judge if (this. next! = NULL) {return this. next. search (data) ;}else {return false ;}} public void Delete (node previous, string data) {If (data. equals (this. data) {previous. next = This. next; // empty current node} else {If (this. next! = NULL) {This. next. delete (this, data); // continue searching }}} private node root; // The root node public void addnode (string data) {node newnode = new node (data ); // create a new node if (this. root = NULL) {This. root = newnode;} else {This. root. add (newnode) ;}} public void printnode () {If (this. root! = NULL) {This. root. print (); // call the output operation} public Boolean contains (string name) {return this. root. search (name); // call the search method of the node class} public void deletenode (string data) {If (this. contains (data) {// determine whether a node exists if (this. root. getdata (). equals (data) {This. root = This. root. next; // modify the root node} else {This. root. next. delete (root, data); // pass in the previous node and data of the next node }}} public class linkdemo02 {public static void main (string ARGs []) {link L = new link (); L. addnode ("node-a"); L. addnode ("node-B"); L. addnode ("node-c"); L. addnode ("node-d"); // added content L. printnode (); // determines whether the node system is included. out. println (L. contains ("node-X"); L. deletenode ("node-B"); // The deleted content L. printnode ();}}
Summary:
1. Different classes have different functions. linkdemo01 is the basic linked list class, while linkdemo02's internal class is the basic linked list class, and the external class is the Linked List Operation class.
2. There is a this. Next in the basic linked list class pointing to the next linked list object.