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 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

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: info-contact@alibabacloud.com 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.