Big talk. Data Structure II: Chain-type storage structure of linear table (single linked list)

Source: Internet
Author: User
Tags empty header int size integer

1. Linear table chain storage structure: Refers to the use of a set of arbitrary storage units to store the linear table data elements, this group of storage units can be continuous, or discontinuous, which means that these data elements can exist in the memory is not occupied anywhere.

2. Node: Nodes consist of data fields that hold data elements and pointer fields that store subsequent node addresses.

1. In sequential storage structure, each data element needs only the information of the data element to be stored.

2.) Chain storage structure, in addition to storing data element information, but also to store its successor elements of the storage address.

3.) The first node in the list is called the head node, its storage location is called the head pointer, its data field can not store any information, the list of the last node pointer is empty.

3. Java implementation single linked list:

One-way linked list public class Singlelinkedlist<e> {private node<e> head;//Header node private int size;//Linked list  
        Length//Set up an empty list of public singlelinkedlist () {head = NULL;  
    size = 0;   
        ////Insert public boolean Add (E data) {node<e> NewNode = new node<e> (data) at the end of the list;  
        if (IsEmpty ()) {head = NewNode;  
            }else{node<e> tail = get (size);  
        Tail.setnext (NewNode);  
        } size++;  
    return true; //Insert the node at the specified location public boolean insert (int position, E data) {if (position >= 1 && (  
            Position <= size + 1) {node<e> NewNode = new node<e> (data);  
                if (IsEmpty () | | | position = = 1) {//The linked list is empty or inserted newnode.setnext (head) before the header node;  
            head = NewNode; }else{//Insert node<e> Prenode = Get in middle position = Get (posiTION-1); Gets the previous node of the position node<e> Originalnode = Prenode.getnext ();  
                Gets the position position corresponding to the node Prenode.setnext (newNode) when the node is not inserted;  
            Newnode.setnext (Originalnode);  
            } size++;  
        return true;  
    return false;  
        //Deletes the node at the specified location public E Delete (int position) {E result = null; if (position >= 1 && position <= size) {if (position = 1) {//delete head node result =  
                Head.getdata ();  
                node<e> afterhead = Head.getnext ();  
            head = Afterhead; }else{//Delete other nodes node<e> Prenode = Get (position-1);//Obtain the previous node of the node to be deleted node<e& Gt Curnode = Prenode.getnext ();  
                Gets the node to be deleted result = Curnode.getdata (); Prenode.setnext (Curnode.getnext ()); Point the node before the deletion to the next node of the node to be deleted. Size--;  
    return result;  
        //Gets the node public node<e> get (int position) {node<e> TargetNode = null for a location;  
            if (!isempty () && position >= 1 && position <= size) {TargetNode = head;  
            for (int i = 1; i < position i++) {TargetNode = Targetnode.getnext ();//loop to get the node of the corresponding position  
    } return TargetNode;  
    //Get the length of the list of public int getsize () {return size;  
    //Determine if the list is empty public boolean isempty () {return size = = 0;  
        ///Print List data public void display () {node<e> Node = head;  
        System.out.print ("Single linked list:");  
            for (int i = 0; i < size; i++) {System.out.print ("" + Node.getdata ());  
        node = Node.getnext ();  
    } System.out.println (""); }  
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Node class, data containing nodes and references to the next node public  
class Node<e> {  
    private E data;  Data field  
    private node<e> next;  The pointer field holds the reference to the next node, public node  
      
    () {} public node  
      
    (E data) {  
        this.data = data;  
    }  
          
    Public Node (E data,node<e> next) {  
        this.data = data;  
        This.next = next;  
      
    public void Setnext (node<e> next) {  
        this.next = next;  
    }  
      
    Public node<e> GetNext () {return  
        next;  
    }  
      
    Public E GetData () {return  
        data;  
    }  
      
    public void SetData (E data) {  
        this.data = data;  
    }  
      
    @Override public
    String toString () {return  
        "" + Data;  
    }  
}
Test class public  
class Main {public  
    static void Main (string[] args) {  
        singlelinkedlist<integer> circular = new singlelinkedlist<integer> ();  
        Circular.add (1);  
        Circular.add (2);  
        Circular.add (3);  
        Circular.insert (4,4);  
        Circular.insert (5,5);  
        Circular.insert (6,6);  
        Circular.delete (3);  
        Circular.delete (5);  
        Circular.display ();  
        System.out.println ("The length of the list is:  " + circular.getsize ());  
    }  

4. Single linked list structure and sequential storage structure advantages and disadvantages:

1. If the linear table needs frequent lookup, rarely inserts and deletes operations, it is advisable to use sequential storage structure, if the need to frequently insert and delete, it is advisable to adopt a single linked list structure.

2. When the number of elements of a linear table changes greatly or does not know how big, it is best to use a single linked table structure, so that you do not need to consider the size of storage space problem. The size and position of a single linked list does not need to be allocated in advance, and can be generated instantly according to the system and actual requirements.

Author: csdn Blog zdp072

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.