The node node is encapsulated, assuming that node's operation has increased, deleted, searched, and printed several operations. The node is implemented as the inner class of the linked list link, simplifying the code.
Package Chapter5;import java.security.cert.ldapcertstoreparameters;class link{class node{private String data;private Node Next;public node (String data) {this.data = Data;this.next = null;} public void Add (Node newNode) {if (This.next = = = null) {This.next = NewNode;} Else{this.next.add (NewNode);}} public void print () {System.out.print (this.data+ "-to"), if (this.next!=null) {this.next.print ();}} public boolean search (String data) {if (Data.equals (This.data)) {return true;} Else{if (this.next!=null) {return this.next.search (data);} Elsereturn false;}} public void Delete (Node Pre, String data) {if (Data.equals (This.data)) {pre.next = This.next;} Else{if (this.next!=null) {This.next.delete (this, data);}}} Private Node root;public void AddNode (String data) {node NewNode = new Node (data); if (this.root==null) {root = NewNode;} Else{this.root.add (NewNode);}} public void Printnode () {if (this.root!=null) {this.root.print ();} System.out.println ();} Public Boolean contains (String data) {return this.root.search (data);}public void Deletenode (String data) {if (contains (data)) {if (this.root.data.equals (data)) This.root = This.root.next; Else{this.root.delete (root, data);}}} public class LinkDemo01 {public static void main (string[] args) {//TODO auto-generated method Stublink L = new Link (); L.A Ddnode ("A"), L.addnode ("B"), L.addnode ("C"), L.addnode ("D"), L.printnode (), L.deletenode ("B"); L.addnode ("E"); L.printnode (); System.out.println (L.contains ("A"));}}
Implementation of single-link list in Java