Linear table summarizes knowledge

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43633379/article/details/102770491

Linear table:
linear table referred to the table, it is a finite sequence of n elements.

Characterized each element has only one predecessor and successor, the first element-free precursor, the last element no successor, and the same type of all the elements of the element.

Linear table with data of other logical structure of the same, and sequentially stored into storage structure storing the chain.

Sequential storage table called sequence table, the table is referred to the list stored in the chain.
A logical structure of linear form
1,
the linear form: is zero or more finite sequences of data elements having the same type.
Non-empty table: L = (a1, a2, ..., an) (ai is a data element)
2, a linear table abstract data type definitions
Date
of data elements in the linear form of the same type, with adjacent elements predecessor and successor relationships
Operation
IniList
preconditions: linear table does not exist
input: None
function: linear initialization table
output: None
postcondition: a linear form empty
DestroyList
preconditions: linear table already exists
input: None
function: destruction linear form
output: None
postcondition: linear release storage space occupied by the table
length
preconditions: linear table already exists
input: None
function: table length of linear
output: the number of data elements in a linear form
postcondition: invariant linear form
Get
preconditions: linear existing table
entries: element number i
functionality: linear taken table No. data element i
output: If the sequence number method, returns the number of elements of the value of i, otherwise an exception is thrown
postcondition: linear table unchanged
Locate
Preconditions: linear table already exists
Input: data element x
Function: linear lookup table element is equal to the value of x
output: If found, the element returns the serial number in table x, 0 otherwise
postcondition: linear form constant
insert
preconditions: linear existing table
entries: insertion position i; element x to be interpolated
functions: the position of the i-th line of the table insert a new element x
output: If the insertion is not successful, an exception is thrown
postconditions : If the insert is successful, the table adds a new element
delete
preconditions: linear table already exists
, type: delete a location i
function: delete i-th element linear table
output: If you delete succeeds, the return of elements erased, otherwise throw abnormal
postcondition: if the deletion is successful, the table element in a reduced
empty
pre-condition: linear table already exists
input: None
function: determining a linear table is empty table
output: if empty list, return to 1, 0 otherwise
post conditions: invariant linear form
PrintList
preconditions: linear table already exists
input: None
function: output sequentially according to the order position of the table element linear
output: line Each data element table
postcondition: invariant linear form

Second, the structure of the linear sequential storage table and to achieve
an order of Table
respective elements using a set of addresses of the storage unit sequentially stores the successive linear table
role: to reflect the relationship between the physical storage through the adjacent data elements between data elements logically adjacent relationship
with the one-dimensional array of data stored in the sequence table
2, the order table is provided for each element c representing a storage unit, the storage address of i-th element:
the LOC (AI) = the LOC (A1) + (. 1-I) * C
(random access):
Here Insert Picture Description
3, the sequence table implementation
const int Maxsize = 100;
Template
class SeqList {
Private:
T data [the MaxSize]; // array of data elements stored
int length; // linear form the length of the
public:
SeqList (); // no argument constructor
SeqLIst (T a [], int n); // constructor parameters have
~ SeqLIst () {} // destructor empty
int length () {return length;} // find the length of the linear form
T get (int i); // Find bit, take the i-th element of the linear form
int locate (T x); // Search by values, table values for linear the element number x
void insert (int i, T x ); // linear table is the i-th position of the insertion element x
T delete (int i); // delete the linear form of the i Element
void PrintList (); // linear traversing table, the elements are sequentially output by serial number
};

4, the insertion operation of
the linear interpolation operating table is defined in clause i (1 <= i <= n + 1) th position, inserting a new element E, so that the length of length n into a table is a linear table n + 1 the linear table
(note that the mobile element and increasing the length of the sequence table)
5, the insertion algorithm implemented
Template
void :: SeqLIst the insert (int I, T X) {
int J;
"overflow" IF (length> = the MaxSize) the throw;
IF (i <1 || i> length + 1) throw " position";
for (J = length; J> = I; J-)
Data [J] = Data [-J. 1];
Data [-I. 1] = X;
length ++;
}
. 5, the analysis
is inserted at the end without moving elements, the time complexity is O (1);
inserted at the beginning of the need to move n elements, O (n);
. 6, delete algorithm to achieve
Template
void SeqLIst :: the delete (int I, T X) {
int J;
T X;
IF (length == 0) the throw "underflow";
IF (I <. 1 || I> length) the throw "position";
X = Data [I. 1- ];
for (J =. 1; J <length;j++)
data[j-1]=data[j];
length–;
return X;
}
. 7, analysis of
the final deleted, O (1);
at the beginning of deleting, moving, n-1 elements, O (n-);
. 8, a sequential search operation table
① by position search: Find the specified position element
Template
T SeqList :: the Get (int I)
{
IF (I <. 1 && I> length) the throw "Find position illegal";
the else return Data [I-. 1];
}
time complexity is O (. 1)
② Search by Found: Find the position value specified in the sequence table
Template
int :: SeqList the locate (T X) {
for (int I = 0; I <length; I ++)
IF (Data [I] == X)
return I +. 1; / / element at index i is equal to x, which returns the sequence number. 1 + i
return 0; // exit the loop described lookup fails
}

Third, the chain storage structure and implementation of linear table
1, the chain storage allocation characteristics:
the length of the linear form of dynamic memory application, to address sequentially stored in the storage space is difficult to determine the presence of
2, Storage Structure implemented
a single linked list, a doubly linked list, circular linked list
3, list node data type definitions
Template
struct the node
{
T data;
the node * Next; / herein may be omitted
};
4, single-linked list
Template
class LinkList {
public :
LinkList () {First = new new the Node; First-> Next = NULL;}
LinkList (A T [], int n-);
~ LinkList ();
int the Length ();
T the Get (I int);
int the Locate (T X);
void the Insert (int I, T X);
T the Delete (I int);
void printlist ();
Private:
the Node * first; // single list head pointer can be omitted
};
5, the first interpolation method
6, tail interpolation

Fourth, the single list and comparison of the sequence table
1, comparative performance time
when the operation of the linear ① If the main table lookup, insert, and delete rarely done, should be done using the sequence table storage structure
② linear frequent insertion and deletion for table, linked list should do storage structure
2, comparing performance space
storage density = storage node data itself occupied / total occupied storage node structure
when little change in length of the linear form, when its size easily determined in advance, in order save memory space, it should be used as the sequence table storage structure

Five other storage Linear Table
1, a circular linked list,
a doubly linked list,
static list: need not be moved when inserting and removing elements, can directly modify the pointer, and therefore high efficiency. But not on-demand allocation of storage space
2, indirect addressing (Pointer Array)
advantage ① sequentially stored in the linear form: support random access
advantages of a linear chain tables stored ②: no need to move data insert and delete data
③ indirect addressing: a method array pointer and combining, he stores the cell data in the array elements to store a pointer to the element

Guess you like

Origin blog.csdn.net/qq_43633379/article/details/102770491
Recommended