Linear structure: Its orderly order of orders rather than numerical order.
Linear table (list): A finite sequence consisting of 0 or more data elements.
If you are using a mathematical language to define, you can do the following:
If the linear memento is (A1,..., ai-1,ai,ai+1,... An), the ai-1 in the table is ahead of Ai,ai, which is called ai+1 is the direct precursor element of AI, Ai-1 is the direct successor of AI.
the number of linear table elements N (n>=0) is defined as the length of a linear table, when n=0 is called an empty table.
Note: precursor, successor element. The number of linear table elements is defined as the length of the linear table, and 0 o'clock is the empty table.
linear table is a kind of common Data Structure . In practical applications, linear tables are used in the form of stacks , queues , strings , arrays and other special linear tables.
a linear table is a linear structure, which is a finite sequence of n≥0 nodes, for which there is only one starting node without a precursor but there is a successor, and there is only one terminal node that has no successor but a precursor node. Other nodes have and have only one precursor and one subsequent node. In addition, linear table emphasis is limited, that is, the elements it handles are finite.
The relationship between data elements is a one-to-one relationship.
Characteristics:
1. There must be a unique "first element" in the set;
2. There must be only one "last element" in the set;
3. Except for the last element, there is a unique successor (the latter);
4. In addition to the first element, there is a unique precursor (the preceding piece).
Structure code for linear table sequential storage:
#define MAXSIZE typedef int ELEMTYPE;//uses ELEMTYPE to replace the int typedef struct{ elemtype data[maxsize];int length;< c2/>//linear table Current length} sqlist;
here we encapsulate a structure that is actually encapsulated in an array, adding variables of the current length.
Having finished with the basic concepts of linear tables, let's look at the sequential storage structure of linear tables.
The storage structure of linear table: linear storage structure, chained storage structure.
sequential storage structure encapsulation requires three properties:
1> of storage space starting position, array data , its storage location is the storage location of the linear table storage space.
2> maximum storage capacity of a linear table: the length of the array is maxsize.
3> The current length of the linear table: length.
Note that the length of the array needs to be distinguished from the current length of the linear table:
The length of the array is the total length of the storage space in which the linear table is stored, usually unchanged after initialization.
The current length of the linear table is the number of elements in the linear table, which can change. (the number of current elements in a linear table will change)
The linear table is sorted starting from 1, that is, A1,a2,a3,,,, subscript, or starting from 0.
Assuming that elemtype occupies a C storage unit (byte), the relationship between the i+1 data element in the linear table and the storage location of the I Data element is (Loc represents the function that obtains the storage location): loc (ai+1) = loc (AI) + C
So the storage location of the AI for the I data element can be inferred from A1: loc (AI) = loc (A1) + (I-1) *c
With this formula, we can always calculate the address of any position in the linear table, whether it is the first or the last, it is the same time.
then it's storage Time performance is of course O (1) , which we commonly call random storage structure.
get element Operations
Implementation of the Getelem operation, will be the linear table L in the first position element value returned.
As far as the program is concerned, we just need to return the value of the array i-1.
Insert Operation
The sequential storage structure of linear table has the characteristics of random storage structure, and the time Complexity is O (1).
Now let's consider if we are going to implement Listinsert (*l, I, E), that is, to insert a new element E in the I position of the linear table L, how should the code be written?
the idea of inserting an algorithm:
If the insertion position is unreasonable, throw an exception;
If the linear table length is greater than or equal to the array length, an exception is thrown or the array capacity is dynamically increased;
Move forward from the last element to the I position, moving each of them backward one position;
Fill in position I with insert element;
Linear table Length +1.
Delete Operation the idea of deleting an algorithm:
If the deletion position is unreasonable, throw an exception;
Remove the deleted element;
Moving from the location of the deleted element to the last element position, respectively, moves them all forward one position;
table Length-1.
Advantages and disadvantages of linear table sequential storage structure
sequential storage structure of linear tables, in storage Storage , Read data, regardless of location, the time complexity is O (1). In the case of insertions or deletions, the time complexity is O (n)(in general, we are taking the worst case scenario).
This shows that it is more suitable for the number of elements more stable, not often insert and delete elements, and more operations are access to data applications.
Advantages:
There is no need to add additional storage space to represent the logical relationship between elements in a table.
You can quickly access elements from anywhere in the table.
Disadvantages:
Insert and delete operations require a large number of elements to be moved.
When the linear table length varies greatly, it is difficult to determine the capacity of the storage space.
"Fragmentation" that easily creates storage space.
Sequential storage structure of linear tables