Java single-Link list code implementation

Source: Internet
Author: User

Used to C++,java write up really not very good ... No nonsense, on the code ...

Package Javainnerclassdemo;class link{class node{private string name;private node next;public node (string name) { This.name=name;} public void SetName (String name) {this. name = name;} Public String GetName () {return this.name;} public void AddNode (Node newnode) {if (this.next==null) This.next=newnode;elsethis.next.addnode (newnode);} public void Printnode () {if (this.next!=null) {System.out.print (this.name); System.out.print ("--");} ElseSystem.out.println (this.name); if (this.next!=null) This.next.printnode ();} public boolean Searchnode (String name) {if (this.name.equals (name)) {return true;} Else{if (this.next!=null) {return this.next.searchnode (name);} Else{return false;}}} public void Deletenode (Node prenode,string name) {if (this.name.equals (name)) {prenode.next = This.next;} Else{this.next.deletenode (This,name);}}} Private Node root;public void Add (String name) {Node NewNode = new Node (name); if (this.root==null) This.root=newnode; Elsethis.root.addnode (newnode);}      public void print () {if (this.root!=null) {       The reason for the external judgment, because printnode need to iterate This.root.printnode ();} ElseSystem.out.println ("The linked list is empty, cannot print!") ");} public boolean search (String name) {if (This.root.searchnode (name) ==true) return True;elsereturn false;} public void Delete (String name) {if (This.search (name)) {///Determine if this node exists if (This.root.name.equals (name)) {if ( This.root.next!=null) {this.root = This.root.next;//change root node}else{this.root = null;//Cancel}}else{if (This.root.next!=null) {This.root.next.deleteNode (root,name);}}} ELSESYSTEM.OUT.PRINTLN ("The node you want to delete does not exist!") ");}} public class Linklistdemo {public static void main (string[] args) {link L = new Link (); L.add ("Walkthehorizon"); L.add ("Already") , L.add ("No"), L.add ("able to block"), L.add ("the"), L.add ("" "); System.out.println ("Print List"); L.print (); System.out.println ("Find linked list"); System.out.println (L.search ("Walkthehorizon")); System.out.println (L.search ("The Blade of Exile"); System.out.println ("Delete node"); L.delete (""); L.print ();}}
The core of Java's single-linked list implementation is the multi-level iteration.

Java single-Link list code implementation

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.