Object-oriented basics: Improvement of linked lists

Source: Internet
Author: User

Use internal classes to complete linked list operations 1. Add nodes and add the [java] class Link {// linked list completion class Node {// save each Node at the end of the linked list, in this example, private String data is defined as an internal class for convenience. // you can save the content of a Node, private Node next. // you can save the next public Node (String data) {this. data = data; // set the Node content through the constructor} public void add (Node newNode) {// add the Node to the appropriate location if (this. next = null) {// if the next node is empty, set the new node to the next position this. next = newNode;} else {// if it is not empty, you need to continue searching for next this. next. add (newNode) ;}} p Ublic void print () {System. out. print (this. data + "\ t"); // output the node content if (this. next! = Null) {// there is another element. You need to output this. next. print (); // The next Node continues to call print }}; private Node root; // a root Node public void addNode (String data) must exist in the linked list) {// Add Node newNode = new Node (data); // define a new Node if (this. root = null) {// No root node this. root = newNode; // set the first node to the root node} else {// not the root node, Put it after the last node this. root. add (newNode); // automatically arrange the location of this Node through Node} public void printNode () {// output all the contents of the linked list if (this. root! = Null) {// if the root element is not empty this. root. print (); // call the output operation in the Node class }}; public class LinkDemo02 {public static void main (String args []) {Link l = new Link (); l. addNode ("A"); // Add node l. addNode ("B"); // Add node l. addNode ("C"); // Add node l. addNode ("D"); // Add node l. addNode ("E"); // Add node l. printNode () ;}; 2. Search for nodes in a recursive manner to find the complete class Node of the [java] class Link {// chain table {// save each Node, to facilitate the definition of private String data; // Save the content of the Node private Node next; // save the next Node public Node (String data) {this. data = data; // set the Node content through the constructor} public void add (Node newNode) {// add the Node to the appropriate location if (this. next = null) {// if the next node is empty, set the new node to the next position this. next = newNode;} else {// if it is not empty, you need to continue searching for next this. next. add (newNode) ;}} public void print () {System. out. print (this. data + "\ t"); // output node content if (this. next! = Null) {// there is another element. You need to output this. next. print (); // The next node continues to call print} public boolean search (String data) {// internal search method if (data. equals (this. data) {// determine whether the input data is consistent with the data of the current node return true;} else {// continue to judge if (this. next! = Null) {// if the next node exists, continue to search for return this. next. search (data); // return the next query result} else {return false; // If NO content is equal after all nodes are queried, returns false }}}; private Node root; // a root Node public void addNode (String data) must exist in the linked list) {// Add Node newNode = new Node (data); // define a new Node if (this. root = null) {// No root node this. root = newNode; // set the first node to the root node} else {// not the root node, Put it after the last node this. root. add (newNode); // automatically arrange the location of this Node through Node }} Public void printNode () {// output all contents of the linked list if (this. root! = Null) {// if the root element is not empty this. root. print (); // call the output operation in the Node class} public boolean contains (String name) {// determine whether the element has return this. root. search (name); // call the search method} in the Node class; public class LinkDemo02 {public static void main (String args []) {Link l = new Link (); l. addNode ("A"); // Add node l. addNode ("B"); // Add node l. addNode ("C"); // Add node l. addNode ("D"); // Add node l. addNode ("E"); // Add node System. out. println (l. con Teains ("C") ;}; 3. delete a node, change the address passed by the reference [java] class Link {// complete class Node of the linked list {// save each Node, which is directly defined as an internal class private String data for convenience; // Save the content of the Node private Node next; // save the next Node public Node (String data) {this. data = data; // set the Node content through the constructor} public void add (Node newNode) {// add the Node to the appropriate location if (this. next = null) {// if the next node is empty, set the new node to the next position this. next = newNode;} else {// if it is not empty, you need to continue searching for next this. next. add (newNode );} Public void print () {System. out. print (this. data + "\ t"); // output the node content if (this. next! = Null) {// there is another element. You need to output this. next. print (); // The next node continues to call print} public boolean search (String data) {// internal search method if (data. equals (this. data) {// determine whether the input data is consistent with the data of the current node return true;} else {// continue to judge if (this. next! = Null) {// if the next node exists, continue to search for return this. next. search (data); // return the next query result} else {return false; // If NO content is equal after all nodes are queried, returns false }}public void delete (Node previous, String data) {if (data. equals (this. data) {// matched node previous is found. next = this. next; // empty current node} else {if (this. next! = Null) {// The next node still exists this. next. delete (this, data); // continue searching }}}; private Node root; // a root Node public void addNode (String data) must exist in the linked list) {// Add Node newNode = new Node (data); // define a new Node if (this. root = null) {// No root node this. root = newNode; // set the first node to the root node} else {// not the root node, Put it after the last node this. root. add (newNode); // automatically arrange the location of this Node through Node} public void printNode () {// output all the contents of the linked list if (this. root! = Null) {// if the root element is not empty this. root. print (); // call the output operation in the Node class} public boolean contains (String name) {// determine whether the element has return this. root. search (name); // call the search method in the Node class} public void deleteNode (String data) {// delete a Node if (this. contains (data) {// determines whether a node exists. // you must determine whether the element is currently equal to the root element if (this. root. data. equals (data) {// The content is the root node this. root = this. root. next; // modify the root node and set the first node to the root node} else {this. root. next. delete (root, data); // pass in the previous node and data of the next node together }}}; public class LinkDemo02 {public static void main (String args []) {Link l = new Link (); l. addNode ("A"); // Add node l. addNode ("B"); // Add node l. addNode ("C"); // Add node l. addNode ("D"); // Add node l. addNode ("E"); // Add node System. out. println ("======= before deletion ========"); l. printNode (); // System. out. println (l. contains ("X"); l. deleteNode ("C"); // delete node l. deleteNode ("D"); // delete node l. deleteNode ("A"); // Delete the node System. out. println ("\ n ======= after deletion =========="); l. printNode (); System. out. println ("\ n query node:" + l. contains ("B "));}};

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.