The sequential storage structure of linear tables, also known as sequential tables , refers to storing data elements in linear tables sequentially in successive storage units .
Based on the attributes of the sequential table, we use an array to implement the sequential table, and here is the sequential table of the Java versions that I implemented through the array.
Package com.phn.datestructure;/** * @author Pan Hainan * @Email [email protected] * @TODO Order table * @date July 16, 2015 */public CL foarraylist<e> {//Order table length private int size; Order table default array is null private object[] data = null; The initialization length of an array in a sequential table private int capacity; Default initialization length of the sequential table private static final int defualt_initial_size = 0; /** * Default parameterless constructor */public foarraylist () {this (defualt_initial_size); }/** * @TODO with parameter constructor * @param initialsize Initialization Order table length */public foarraylist (int initialsize) {if (i Nitialsize < 0) {throw new RuntimeException ("Array size error:" + initialsize); } this.data = new object[initialsize]; this.capacity = InitialSize; This.setsize (); }/** * @TODO set the length of the order table */private void SetSize () {this.size = 0; }/** * @TODO Gets the length of the sequential table * @return The length of the size order table */public int size () {return this.size; }/** * Add elements @TODO Order table * @param e data element type * @return True */public boolean Add (E e) {ensuresize (size); This.data[size] = e; this.size++; return true; }/** * @TODO sequential Table Insert Element * @param index Insert position * @param e data element type * @return True */public Boolean INS ERT (int index, E e) {if (index >= 0 && index <= size) {ensuresize (size); E temp = (e) this.data[index-1]; This.data[index-1] = e; this.size++; for (int i = index; i <= size; i++) {E Temp2 = (e) this.data[i]; This.data[i] = temp; temp = TEMP2; }} else {throw new RuntimeException ("Array subscript error:" + index); } return true; }/** * @TODO Order Table Delete element * @param index position of the element to be deleted * @return e deleted element */public e remove (int index) { Validateindex (index); E e = (e) this.data[index]; for (int i = index; i< size-1; i++) {This.data[i] = this.data[i + 1]; } this.size--; return e; /** * @TODO Gets the element based on the index position of the element * @param index position of the index element * @return e element e */public E get (int index) { Validateindex (index); Return (E) This.data[index]; }/** * @TODO Modify the element of the index position in the order table to element E * @param index position of the indexed element * @param e needs to be modified to the element * @return true to modify the success flag */public Boolean set (int index, E e) {validateindex (index); This.data[index] = e; return true; } @Override Public String toString () {return this.arraytostring (data); }/** * @TODO gets the array sequence in the sequential table in string form * @param an array in a sequential table * @return array sequence in the sequential table as String String */Private Strin G Arraytostring (object[] a) {if (a = = null) return "NULL"; int iMax = this.size-1; if (IMax = =-1) return "[]"; StringBuilder B = new StringBuilder (); B.append (' ['); for (int i = 0;; i++) {b.append (string.valueof (A[i])); if (i = = IMax) return b.append ('] '). toString (); B.append (","); }}/** * @TODO Verify that the given index position is valid * @param index position given by * * private void Validateindex (int index) { if (Index >= this.size | | Index <= 0) {throw new RuntimeException ("Array subscript error:" + index); }}/** * @TODO determine if you need to expand the sequential table capacity * @param currentsize the size of the current order table */private void ensuresize (int currentsiz e) {if (currentsize = = capacity) {this.capacity = (this.capacity * 3)/2 + 1; object[] NewData = new Object[this.capacity]; for (int i = 0; i < currentsize; i++) {newdata[i] = This.data[i]; } this.data = NewData; } }}
Note the above 3 private member variables as follows:
As the annotation explains, size is used to represent the length of the sequential table, data is used to represent the array, and capacity is used to represent the length of the array.
Believe that data should be better understood, and the corresponding two length variables are relatively difficult to understand some, the following explanation:
- Size refers to the value shown when accessing the length of the sequential table, which is the number of data elements in the sequential table, which changes with the sequential table insert and delete operations;
- Capacity represents the length of the data array, which is actually the capacity of the entire sequential table, which can be assigned when the sequential table is initialized, or it can then be changed by invoking the expansion of the sequential table;
- Size is less than or equal to capacity.
Here we mainly talk about the insertion and deletion of sequential tables:
Insert demo for sequential table:
As you can see from the picture, after inserting an element, the elements after the insertion position need to be moved back one position.
The delete operation is the inverse of the insert operation, and the element after the delete position needs to move forward one position.
Time Complexity Analysis:
- The average time complexity is O (1) When the sequential table is deposited, searched and modified;
- In the case of INSERT and delete operations, the quickest is O (1) and O (n) at the slowest, so the average time complexity is O (n).
Explanation: The above deposit and insert are different, the deposit representation is stored at the end of the array, and the insertion is inserted at any position.
Pros and Cons Analysis:
Advantages:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Data Structure-sequential table of linear tables ArrayList