Linear table
Applications: polynomial representation
What is the linear form
Revelation given polynomial representation issues:
The same question can have different representations (stored) method
One class of common problems: rental and management of orderly linear sequence
"Linear table (Linear List)": linear structure constituted by an ordered sequence of data elements of the same type
The number of elements in the table is called the length of the linear form
When the table is not linear elements, it referred to the empty list
Table origin location called a header, referred to the end position of the table footer
Linear table abstract data type description
Type Name: Linear meter (List)
Sets of data objects: linear table is n (> = 0) of an ordered sequence of elements constituting the (a_1, a_2, a_3, ...., a_n)
The set of operations: a linear list L List belongs, represents the position of the integer i, the element X belonging ElementType
Linear form basic operations are:
MakeEmpty List () : initializing an empty list of L;
FindKth the ElementType (int K, L List) : The rank K, returns the corresponding element;
the Find int (ElementType X, List L) : L linear table find the first occurrence of X position;
the Insert void (the ElementType X-, int i, List L) : i preamble of X-bit inserting a new element;
the Delete void (int i, List L) : Deletes the specified rank i of the element;
the Length int (List L) : returns the length of the linear form of n-L;
Linear form of sequential storage implemented
Using an array of contiguous memory storage order of the elements of the linear form
#define MAXSIZE <............>
typedef struct LNode *List;
struct LNode{
ElementType Data[MAXSIZE];
int Last;
};
struct LNode L;
List PtrL;
Access subscript element i: L.Data [i] or PtrL-> Data [i]
Length linear form: L.Last + 1 or PtrL-> Last + 1
To achieve the main operation
List MakeEmpty()
{
List PtrL;
PtrL = ( List )malloc( sizeof( struct LNode ) );
PtrL->Last = -1;
return PtrL;
}
Seek
int Find(ElementType K, List PtrL)
{
int i = 0;
while ( i <= PtrL->Last && PtrL->Data[i] != X)
i++;
IF (I> PtrL-> Last) return -1; // If not found, returns -1
return the else I; // find the storage location of the return
}
Insert (the i (1 <= i <= n + inserting a new element value X 1) th position)
Here it is to be noted that the code used here is counted from 1 Design
Insertion operation embodied
It should be noted here that, where the position is counted from 1 (the default is the human element in the first position is the first, for the computer, it can also start from a zero count, generally, in the computer large are counted starting from 0)
void Insert ( ElementType X, int i, List PtrL)
{
int j;
IF (PtrL-> Last the MAXSIZE == -. 1) {// the table is full, the new element can not be inserted (here MAXSiZE - 1 can be inserted into the final position, and represents MASIZE table element the most number of elements)
printf ( "table full");
return ;
}
if ( i < 1 || i > PtrL->Last+2){
/ * (Counting from zero using the position information here) for PtrL-> position information of the last element in the array element number represented by the list Last, so if using numbered from 1, then this is the first position PtrL- > last + 1 position on the element, but the scope is to be inserted in the element (1 <= i <= n + 1) that is, a first start position, the start position of the n + 1, and therefore to the last position where the inserted location (n + 1-position) with PtrL-> Last word is represented PtrL-> Last + 2 positions,
Therefore, the range of i is determined herein, can easily know the meaning of this range. * /
printf ( "illegal position");
return;
}
for ( j = PtrL->Last; i >= i - 1; j--)
PtrL-> Data [j + 1] = PtrL-> Data [j]; // will move rearwardly descending a_i .... a_n
PtrL-> the Data [I -. 1] = X- ; // new element inserted
PtrL-> Last ++; // Last still points to the last element
return;
}
// insert another version count is employed herein starts counting at 0
void Insert(List L, Elementype X, Position P)
Remove (delete Table i-th (1 <= i <= n) elements on locations)
Delete operation implementation (there is also a position count for the title, the following is used in the first position from the start of counting)
void Delete( int i, List PtrL)
{
int j;
if ( i < 1 || i > PtrL->Last + 1){
printf ( "% d absence of element", i);
return;
}
for ( j = i; j <= PtrL->Last; j++)
PtrL-> Data [j - 1] = PtrL-> Data [j]; // The a_ (i + 1) ..... a_n order to move forward
PtrL-> Last -; // Last still points to the last element
return ;
}
// count positions adopted here is counted from 0
Complete test code:
/*list_array.c*/
Compile and run:
Print Results:
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
10 9 8 7 6 5 4 3 2 1 0
this list length : 11
the positon 2 is : 0
---------- linear form of sequential storage ------------------