Implementation of single-link list in Java

Source: Internet
Author: User

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;class link{class node{private String data;private Node Next;public node (String data) { = Data; = null;} public void Add (Node newNode) {if ( = = = null) { = NewNode;} Else{ (NewNode);}} public void print () {System.out.print ( "-to"), if (!=null) { ();}} public boolean search (String data) {if (Data.equals ( {return true;} Else{if (!=null) {return (data);} Elsereturn false;}} public void Delete (Node Pre, String data) {if (Data.equals ( { =;} Else{if (!=null) { (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 (data);}public void Deletenode (String data) {if (contains (data)) {if ( (data)) This.root =; 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.