Big talk data structure one: sequential storage structure of linear table

Source: Internet
Author: User
Tags int size

1. Sequential storage structure of linear tables: refers to the data elements of a linear table stored sequentially by a contiguous cell of the address.

2. Java implementation of linear table sequence storage structure:

Sequential storage structure public class Sequencelist {private static final int default_size = 10;//default initialization array size private int Size  
      
    Current array size private static object[] array;  
    Public sequencelist () {init (default_size);  
    public sequencelist (int size) {init (size);  
        }//Initialize array size public void init (int size) {this.size = 0;  
    Array = new Object[size];  
        public void Add (int index, Object obj) {checksize (index);  if (size = = Array.Length) {//Judge size if equal to length, the original array is filled with object[] NewArray = new Object[array.length * 3/2 + 1];//Create a larger array system.arraycopy (array, 0, NewArray, 0, array.length); Copy the original data to the new array = NewArray; Array points to the new array} for (int j = size; J > Index; j--) {array[j] = array[j-1];//will be inserted  
        The data element after position moves one} Array[index] = obj;  
  size++;  //delete an element public void remove (int index) {if (size = = 0) {throw new Runtim  
        Eexception ("list is empty");  
        } checksize (index);  
        for (int j = index; J < size-1; J +) {Array[j] = array[j + 1];  
    } size--;  
        ///Get element public Object gets (int index) {checksize (index) by index;  
    return Array[index]; //Show all elements public void display () {for (int i = 0; i < size; i++) {System.  
        Out.print ("" + array[i]);  
    }//Get list length public int size () {return size;  
    }//list is null public boolean IsEmpty () {return size = = 0;  
            private void checksize (int index) {if (Index < 0 | | | index > SIZE)//to throw an exception if you get the index out of bounds  
    throw new Indexoutofboundsexception ("index:" + index + ", size:" + size); }  
} 

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

Test program Public  
class Sequencetest {public  
    static void Main (string[] args) {  
        sequencelist seqlist = new Sequen Celist ();  
        Seqlist.add (0, 1);  
        Seqlist.add (1, 2);  
        Seqlist.add (2, 3);  
        Seqlist.add (3, 4);  
        Seqlist.add (4, 5);  
        Seqlist.add (5, 6);  
        Seqlist.add (6, 7);  
        Seqlist.add (7, 8);  
        Seqlist.add (8, 9);  
        Seqlist.add (9);  
        Seqlist.add (a);  
        Seqlist.add (one);  
        Seqlist.display ();  
        System.out.println ();  
        Seqlist.remove (1);  
        Seqlist.display ();  
    }  

3. Time complexity of insertion and deletion:

The element is inserted into the first position or the first element is deleted, and the n-i elements need to be moved, according to the probability principle, the probability of inserting or deleting elements is the same for each position, for (n-1)/2. The average time complexity is O (n)

4. The advantages and disadvantages of linear table sequential storage:

Advantage: There is no need to add extra storage space for the logical relationship of elements in the table, while you can quickly access elements from any location in the table.

Disadvantage: Insert and delete operations need to move a large number of elements, when the linear table length varies greatly, it is difficult to determine the storage space capacity.

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.