In the previous blog post-the implementation of the linear table interface, we implemented the interface of the linear table, and today let us implement the sequential storage structure of the linear table-the Sequential table class (_java).
First, let's look at the definition of the sequential table:
Sequential storage of linear table is the data element of linear table which is stored sequentially by a contiguous set of memory units, the elements are stored in the same order as their logical order in the linear table, that is, element ai and its direct precursor ai-1 and direct successor ai+1 adjacent to the storage location. Sequential stored linear tables also become sequential tables (sequential list).
The Sequential table class Seqlist provides an implementation of the linear table based on the sequential storage structure, which has two private member variables table and n,table are an array of objects that hold the elements; n is the linear table length, n≤table.length. Seqlist declares the following, which implements the interface llist of the linear table.
package datastructure.linearlist; import datastructure.linearlist.llist; public class SeqList<E> implements LList<E> //Sequential table classes, implementing linear table Interfaces { private object[] table; //Object array, private member private int n; //Sequential table Length public&nbsP Seqlist (int capacity) //construct method, create empty table with top capacity { this.table = new object[math.abs (capacity)];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;THIS.N = 0; } public seqlist () &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//Specifies the default capacity for empty tables { this (; ) } &nbSp; public boolean isempty () //determine if the order table is empty, if NULL returns true { return this.n == 0; } public Int length () //return Order table length { return this.n; } &nBsp; public e get (Int index) //returns an object with an index (initial value of 0) and returns null {if the ordinal is invalid if (index>=0 && index <&NBSP;THIS.N) { return (E) this.table[index]; } return null; } public e set (int index,e element) //the object to set the index position is element, if the operation succeeds, put back the original object, otherwise return null { if (Index >= 0 && index < this.n && element != NULL) { e old = (E) this.table[index]; this.table[index] = element; return old; } return null; } public boolean&nbsP;add (int index,e element) //insert element Object at index position, cannot insert null {if operation returns true successfully if (element == null) //cannot insert null { return false; } if (this.n == table.length) //if the array is full, you need toExpanded sequential table capacity { Object[] temp = this.table; this.table = new object[temp.length *2]; //re-apply for a larger size array for (int i = 0;i < temp.length;i++) { this.table[i] = temp[i]; } } &nBsp; if (index < 0) //Subscript Fault Tolerance { index = 0; } if (INDEX&NBSP;>&NBSP;THIS.N) { index =this.n; } for (int j = this.n-1;j >= index; j--) //element moves back, moving n/2 average { this.table[j+1] = this.table[j]; } this.table[index] = element; this.n++; return true; } public boolean add (e element) //insert Element Object at the end of the order table { return&Nbsp;add (this.n,element); } public e remove (Int index) //removes the object from the index position, returns the removed object if the operation succeeds, or returns the null { if (this.n != 0 && INDEX&NBSP;>=&NBSP;0&NBSP;&&&NBSP;INDEX&NBSP;<&NBSP;THIS.N) { e old = (E) this.table[index]; for (int j = index;j < this.n-1;j++) //element forward, average move n/2 { this.table[j] = this.table[j + 1]; } this.table[this.n - 1] = null; this.n--; return old; } return null; } public void Clear () &NBSP;&NBSP;&NBSP;&NBsp; //Empty Order table { if (this.n != 0) { for (int i = 0;i < this.n;i++) { this.table[i] = null; } this.n=0; } } public string tostring () //returns a string that displays all elements, in the form (,) { string str = "("; if (this.n != 0) { for (int i = 0;i < this.n - 1;i++) { str += this.table[i]. ToString () + ","; } str += this.table[this.n - 1].tostring (); } return str + ")"; } }
A sequential table is an immediate access structure, and the time complexity of accessing the Get (), set () method of any element is O (1).
Inserting or deleting a sequential table is the time the algorithm spends primarily on moving elements. In the case of equal probabilities, inserting an element on average requires moving half of the elements, with a time complexity of O (n). In the same, the time complexity of deleting an element is also O (n).
In summary, the order table has the following characteristics:
①: The physical storage order of an element directly reflects the logical order of elements in the table and can be accessed randomly.
②: Insert and delete operations are inefficient. Each insertion or deletion of an element may require moving a large number of elements, with the average number of moves being half the length of the sequential table. In addition, the array capacity can not be changed, there is a small capacity caused by data overflow, or due to excessive capacity of memory resources wasted. The workaround for data overflow is to request an array of a larger size and copy the array elements, but the insert operation is inefficient.
A sequential table is an immediate access structure, and the time complexity of accessing the Get (), set () method of any element is O (1).
Inserting or deleting a sequential table is the time the algorithm spends primarily on moving elements. In the case of equal probabilities, inserting an element on average requires moving half of the elements, with a time complexity of O (n). In the same, the time complexity of deleting an element is also O (n).
In summary, the order table has the following characteristics:
①: The physical storage order of an element directly reflects the logical order of elements in the table and can be accessed randomly.
②: Insert and delete operations are inefficient. Each insertion or deletion of an element may require moving a large number of elements, with the average number of moves being half the length of the sequential table. In addition, the array capacity can not be changed, there is a small capacity caused by data overflow, or due to excessive capacity of memory resources wasted. The workaround for data overflow is to request an array of a larger size and copy the array elements, but the insert operation is inefficient.
Implementation of sequential table classes for sequential storage structures of linear tables _java