Big talk. Data structure three: chain-type storage structure of linear table (static list)

Source: Internet
Author: User
Tags int size

1. Static linked list: The list described by the array is called static linked list, usually for the convenience of data insertion, we will set the number of larger.

2. Array element (node): Consists of two data fields (Data,cursor). Data fields are used to hold data elements, which are usually the data we want to process, whereas cursor corresponds to a pointer in a single linked list and holds the subscript of the element's successor in the array.

3. Java implementation static linked list:

Static linked list class Staticlinkedlist {private int size;  
      
    Private node[] Node = new node[100]; Initialize static list public staticlinkedlist (int arr[]) with array {for (int i = 0; i < i++) {Node[i = new Node (); Initialize 100 node objects (sensory performance is not good) for (int j = 0; J < Arr.length; J + +) {node[j + 1].setdata  (Arr[j]);  
        The first node is the head node, the head node has no data, only the index node[j].setcursor (j + 1);  
    size = Arr.length;  
        The value public void Insert (int index, int value) {Validateindex (index) is inserted at a location.    int curindex = Node[index].getcursor (); Gets the previous node pointer to be inserted into the node, which remembers the location of the original insertion point node[size + 1].setdata (value); The first node is the head node, so the newly inserted node is node[size + 1] node[size + 1].setcursor (curindex); Let the newly inserted node remember the original location node angle node[index].setcursor (size + 1);  
    Let the original position of the previous node remember the new insertion point angle size++;   
       //Deletes the value of the specified location public void Delete (int index) { Validateindex (index); int curindex = Node[index].getcursor (); Gets the previous node pointer to the deleted nodes, and it remembers the point to be deleted (note: The first node is the head node) int nextindex = Node[curindex].getcursor (); Gets the node pointer to delete, which remembers the next knot to be deleted node[index].setcursor (Nextindex);  
    The previous node pointer of the node to be deleted points to the next node size--;  
    //Verify that the subscript is legitimate and throw an exception when it is illegal. private void Validateindex (int index) {if (Index < 0 | | | index > size) {throw new Indexouto  
        Fboundsexception ("Invalid subscript:" + index);  Output all elements public void display () {int nextindex = Node[0].getcursor ();  
        Node[0] is the head node, and the next node is the dot int i = 0;  
            while (I < size) {System.out.printf ("%d\t", Node[nextindex].getdata ());  
            Nextindex = Node[nextindex].getcursor ();  
        i++; }  
    }  
}

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

Node (array element)  
class Node {  
    int data;//record stored data   
    int cursor;//Record the next subscript public  
    int GetData () of the next data () {  
        return data;  
    }  
    public void SetData (int data) {  
        this.data = data;  
    }  
    public int getcursor () {return  
        cursor;  
    }  
    public void setcursor (int cursor) {  
        this.cursor = cursor;  
    }  
}
Test class public  
class Main {public  
    static void Main (string[] args) {  
        int arr[] = {1, 3, 4, 5, 6};  
        Staticlinkedlist list = new Staticlinkedlist (arr);  
        System.out.print ("initialization: \ n");  
        List.display ();  
        System.out.print ("\ n) after inserting 2 at the corner Mark 1: \ n");  
        List.insert (1, 2);  
        List.display ();  
        System.out.print ("\ n Delete the node with a corner mark of 5: \ n");  
        List.delete (5);  
        List.display ();  
    }  

4. The superiority point of the static link list:

Advantages: There is no need to move elements while inserting and deleting operations, thereby improving the disadvantage of moving a large number of elements in a sequential storage structure by inserting and deleting operations.

Disadvantage: There is no solution to the problem that the table length is difficult to determine for continuous storage allocation.

5. In general, static linked list is actually to give no pointers to the high-level language design of a single linked list of methods, generally rarely used.

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.