Java Linked list structure

Source: Internet
Author: User

Link List Introduction

A linked list (Linked list) is a common basic data structure , a linear table , but does not store data in a linear order, but rather as a pointer to the next node (Pointer) in each node.


the difference between a linked list and an array

Linked lists and arrays are called linear tables,
The array is also called the sequential table, the main difference is that the sequential table is in memory to open up a contiguous space to store data , and the linked list is a pointer to join a number of discontinuous (or continuous) space, logically form a continuous space to store data.
Each has its own advantages, the list is easy to delete and insert, easy to sort the table and so on.


single-line linked list

Original Link: operation of the Java one-way list (add node, find node, delete node)

Class Link {//Chain list Classes * Node {//Save each node, here for the convenience of a direct definition             Internal class private String data;               The content of the node is private node next;        Save the next node, public node (String data) {//The node content is set by the constructor method This.data = data;                } public void Add (node node) {//Add nodes if (This.next = = null) {///If the next node is empty, add the new node to next position            This.next = node;            } else {//If the next node is not empty, continue looking for next This.next.add (node); }} public void print () {//Print node if (this.next! = null) {Syste                M.out.print (This.data + "-");            This.next.print ();            } else {System.out.print (this.data + "\ n");                }} public Boolean search (String data) {//internal method for searching nodes if (this.data.equals (data)) {    return true;        } if (This.next! = null) {return this.next.search (data);            } else {return false; }} public void Delete (node previous, String data) {//Internal Delete node method if (This.data.equals (data            ) {previous.next = This.next;                } else {if (this.next! = null) {This.next.delete (this, data);                              }}}} private Node root;    Define head node public void AddNode (String data) {//Add nodes based on content node NewNode = new node (data);        The node to be inserted if (this.root = = null) {//No head node, the node to be inserted is the head node this.root = NewNode;        } else {//If there is a head node, the method of calling the node class automatically increases This.root.add (NewNode);                              }} public void print () {//Show List method if (root = null) { Display This.root.print () when the list of nodes is present;        }} public Boolean Searchnode (String data) {//The node looking for the specified content in the linked list return Root.search (data);        Method of invoking internal search node} public void Deletenode (String data) {//delete the specified content node in the linked list  if (root.data.equals (data)) {//If is the head node if (root.next! = null) {root            = Root.next;            } else {root = null;        }} else {Root.next.delete (this.root, data);        }}}public class Linkdemo {public static void main (string[] args) {link L = new Link ();        L.addnode ("A");        L.addnode ("B");        L.addnode ("C");        L.addnode ("D");        System.out.println ("Original linked list:");        L.print ();        String Searchnode = "B";        System.out.println ("Find node:" + Searchnode); String result = L.searchnode (Searchnode)?        "Found!": "Not Found!";        System.out.println ("Find results:" + result);   System.out.println ("Delete node:" + Searchnode);     L.deletenode (Searchnode);        SYSTEM.OUT.PRINTLN ("Linked list after node deletion:");    L.print (); }}



Java Linked list structure

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.