My writing is poor, the language has never passed, the foundation is not good, write this kind of article only for their own study, understanding queue and other knowledge, Master God please skip. Reference book "Data structure and algorithm analysis-java language description"
1.1 Introduction to Linear Tables
A linear table is a finite sequence of 0 or more elements . That is, the elements are ordered and limited. Assuming that the table has element A1,a2,a3,....,an, if there is 1<i<n, then ai is the direct successor of Ai-1, Ai-1 is the direct precursor of Ai. As a linear table, A1 must be satisfied without a precursor, an no successor (finite). n is the length of the linear table. When n=0, there are no elements in the table that are empty tables.
1.2 sequential storage of linear tables
The data elements of a linear table are stored sequentially in memory by a contiguous storage unit. If the daily commute to the subway line into a long queue, and sometimes a few good colleagues stand together in succession. The rest of us will not be able to occupy these positions. Conversion to computer language is to find a piece of land in memory, the memory space to take up, and then the same data type of elements (for the time being understood as a universal object type) placed in this piece of land. With an array implementation, the first person is stored in the position where the subscript is 0, and the length of the linear table is the number of people. At this time in waiting for the subway and met a colleague, we let him (she) with us, so the length of the linear table is added 1, but has already defined the length of the array, that is, the memory of only a few of us space, then a colleague looked to the left of a row found no one, take our past line, And then we can all be together. That is, the array is redefined, and the memory occupies more chunks. In another case, when a colleague bursting to go to the bathroom, we took a place. That is, the length of the linear table is constantly changing, and the array is pre-occupied enough space. So the length of the linear table is always less than or equal to the length of the array.
Each storage cell in the memory has its own number, and when the ToString method is not overridden, the result of an instance, the memory address, is printed. If you determine the position of any element, you can calculate the position of any other element by number. That is, it gets the time complexity of the element is O (1).
Assuming that the former colleague occupied the space of dozens of people, we only m individuals now consider the previous insertion problem that suddenly came to a colleague, if the last colleague in the position (assuming it is standing up to need t time, t is a fixed constant), then other colleagues do not move, time complexity is constant O (1) , if inserted in the middle of a certain position K (0<k<m), then the colleagues before K also do not move, but after K m-k a colleague each one to move back one, Time (m-k) *t, the longest situation is he (she) if he (she) inserted in front of the colleague, Each of us will move backwards a position will take time to m*t, so the average time-consuming m*t/2, the average complexity is O (n), n is the size, here is the number of colleagues M. Of course, the removal of elements means that each element moves forward, and the average time complexity is O (N), as is the insertion.
At this point, it is understood that the basic linear table sequential storage (in fact, the storage of other structures is also) includes inserting, deleting, fetching elements and getting the length of the table several operations. Simple implementation of Java code:
/*** Created with IntelliJ idea. * Createuser:blentle * Email: [Email protected] * CREATETIME:2014/11/6 12:40 * Modi Fyuser:blentle * MODIFYTIME:2014/11/6 12:40 * Class Description: * To change this template use File | Settings | File Templates. */ Public classSequencelist<t> { //default storage When storage space is not specified, exception is thrown when operation is out of bounds temporarily Private Static Final intDefault_capacity = 10; //maximum length of linear table (length of array) Private intcapacity; //number of linear table elements Private intsize; //arrays that store linear table elements Privateobject[] Elementdata; /*** Using the default maximum storage space, exceeding the maximum storage space will throw an exception*/ Publicsequencelist () { This(default_capacity); } /** * * @paraminitialcapacity Maximum storage space initialization*/ PublicSequencelist (intinitialcapacity) { This. Capacity =initialcapacity; This. Elementdata =NewObject[capacity]; } /** * * @returnLength of linear table*/ Public intsize () {return This. Size; } /** * * @returnwhether the linear table is an empty table*/ Public BooleanIsEmpty () {return This. Size = = 0; } /*** Get elements *@paramIndex *@returnT*/ PublicT Get (intindex) { //Check if the serial number is out of bounds, note that Inde is starting from 0 if(Index >=size) { Throw NewIndexoutofboundsexception ("index is:" + index + "but size is:" + This. Size); } returnElementdata (index); } Public BooleanAddintindex,t Element) { //Check if the serial number is out of bounds, note that Inde is starting at 0 and the maximum value is size-1 if(Index > Size | | Index < 0) { Throw NewIndexoutofboundsexception ("index is:" + index + ", but size is:" + This. Size); } //If the list is full (the number of elements equals the maximum capacity of the array initialization, general ArrayList continues to expand capacity, which throws an exception) if(Size = =capacity) { Throw NewIndexoutofboundsexception ("list is full ..."); } if(Index = =size) { //inserted at the end of the tableElementdata[size] =element; } Else { //in other places, each element behind the index is moved backward by a for(inti = size + 1; i > Index; i--) {Elementdata[i]= Elementdata[i-1]; } } //The index position is replaced with elementElementdata[index] =element; Size++; return true; } /*** Do not specify insertion position, insert footer *@paramt Linked list element *@return */ Public BooleanAdd (T t) {returnAdd (size,t); } /*** Remove elements from the table *@paramIndex Element Ordinal *@return */ Public BooleanRemoveintindex) { if(Index >= Size | | Index < 0) { Throw NewIndexoutofboundsexception ("index is:" + index + ", but size is:" + This. Size); } for(intK = index; K < size-1; k++) {Elementdata[k]= elementdata[k + 1]; } elementdata[size-1] =NULL; Size--; return true; } /*** Empty Linear table*/ Public voidClear () {if(Size > 0) { for(inti = 0; i < size; i + +) {Elementdata[i]=NULL; } size= 0; }} @Override Publicstring toString () {String a= ""; for(inti = 0; I < size (); i + +) {a+ = (T) elementdata[i] + ""; } returnA; } /*** Get elements and cast packages (refer to ArrayList source) *@paramIndex *@return */ PrivateT Elementdata (intindex) { return(T) Elementdata[index]; }}
1.3 from the code can see the characteristics of the linear table
- Quickly get elements from a table anywhere.
- Insert and delete operations require a large number of elements to be moved.
- When the length of a table changes relatively large, it is not very good to determine the capacity of the storage space (the initialization size of the array)
Linear table of data structures-sequential storage