// The operation implementation of linear tables under sequential storage is selected from data structure (C language description), Xu xiaokai, He Guiying, and Tsinghua University Press.
# Include <stdio. h>
# Include <stdlib. h>
Typedef int elemtype;
/* Sequential storage of linear tables (static)
Struct list
{
Elemtype list [maxsize];
Int size;
};
*/
// Sequential storage of linear tables (Dynamic Allocation)
Struct list
{
Elemtype * List;/* pointer to the dynamic storage space for storing linear table elements */
Int size;/* length of the saved linear table */
Int maxsize;/* the length of the list array, that is, the maximum length of the linear table that can be stored */
};
Void againmalloc (struct list * l)
{
Elemtype * P = realloc (L-> list, 2 * l-> maxsize * sizeof (elemtype ));
// The space is extended to 2 times of the original one and pointed by the P pointer. The original content is
// Automatically copy to the bucket pointed to by P
If (! P) {// allocation failed to exit
Printf ("storage space used up! \ N ");
Exit (1 );
}
L-> List = P; // point the list to the new linear table space.
L-> maxsize = 2 * l-> maxsize; // modify the space size of a linear table to a new length.
}
/*************************************** **************************************** ***
1. initialize linear table l, that is, allocate dynamic storage space and set L to an empty table.
**************************************** **************************************** **/
Void initlist (struct list * l, int MS)
{
/* Check whether Ms is valid. If not, exit the operation */
If (Ms <= 0) {printf ("the MS value is invalid! \ N "); exit (1 );}
/* Set the space size of a linear table to Ms */
L-> maxsize = MS;
/* Dynamic storage space allocation. If the allocation fails, the system exits */
L-> List = malloc (MS * sizeof (elemtype ));
If (! L-> List ){
Printf ("Dynamic Storage Allocation failed! \ N ");
Exit (1);/* Run this function to abort the program running. This function is defined in stdlib. H */
}
/* Initially set the linear table to null */
L-> size = 0;
}
/*************************************** **************************************** ***
2. Clear all elements in linear table L and release the dynamic storage space to make it an empty table.
**************************************** **************************************** **/
Void clearlist (struct list * l)
{
If (L-> list! = NULL ){
Free (L-> list);/* release the bucket */
L-> List = 0;
L-> size = L-> maxsize = 0;
}
}
/*************************************** **************************************** ***
3. returns the length of the linear table L. If l is null, 0 is returned.
**************************************** **************************************** **/
Int sizelist (struct list * l)
{
Return L-> size;
}
/*************************************** **************************************** ***
4. Determine whether the linear table L is null. If it is null, 1 is returned; otherwise, 0 is returned.
**************************************** **************************************** **/
Int emptylist (struct list * l)
{
If (L-> size = 0) return 1; else return 0;
}
/*************************************** **************************************** ***
5. Return the value of the POs element in the linear table L. If the POS is out of the range, stop running the program.
**************************************** **************************************** **/
Elemtype getelem (struct list * l, int POS)
{
If (Pos <1 | POS> L-> size) {/* exit if POS is out of bounds */
Printf ("element serial number out of bounds! \ N ");
Exit (1 );
}
Return L-> list [pos-1];/* returns the element value of the POs value in the linear table */
}
/*************************************** **************************************** ***
6. sequential scanning (that is, traversing) Outputs each element in linear table L.
**************************************** **************************************** **/
Void traverselist (struct list * l)
{
Int I;
For (I = 0; I <L-> size; I ++)
Printf ("% d", L-> list [I]);
Printf ("\ n ");
}
/*************************************** **************************************** ***
7. Find the element whose value is equal to X from the linear table L. if the search is successful, return its position; otherwise, return-1.
**************************************** **************************************** **/
Int findlist (struct list * l, elemtype X)
{Int I;
For (I = 0; I <L-> size; I ++)
If (L-> list [I] = x) return I;
Return-1;
}
// When elemtype is a string type, the IF condition expression should be (strcmp (L-> list [I], x) = 0)
/*************************************** **************************************** ***
8. Change the POs element value in linear table L to the value of X. If the modification is successful, 1 is returned; otherwise, 0 is returned.
**************************************** **************************************** **/
Int updateposlist (struct list * l, int POs, elemtype X)
{
If (Pos <1 | POS> L-> size + 1)/* If POS is out of bounds, modification fails */
Return 0;
L-> list [pos-1] = X;
Return 1;
}
/*************************************** **************************************** ***
9. insert element X to the header of linear table L
**************************************** **************************************** **/
Void insertfirstlist (struct list * l, elemtype X)
{
Int I;
If (L-> size = L-> maxsize)
Againmalloc (l);/* call this function to reassign a larger bucket */
For (I = L-> size-1; I> = 0; I --)
L-> list [I + 1] = L-> list [I];
L-> list [0] = X;
L-> size ++;
}
/*
The following describes the function definition of againmalloc:
Void againmalloc (struct list * l)
{
Elemtype * P = realloc (L-> list, 2 * l-> maxsize * sizeof (elemtype ));
// The space is extended to 2 times of the original one and pointed by the P pointer. The original content is
// Automatically copy to the bucket pointed to by P
If (! P) {// allocation failed to exit
Printf ("storage space used up! \ N ");
Exit (1 );
}
L-> List = P; // point the list to the new linear table space.
L-> maxsize = 2 * l-> maxsize; // modify the space size of a linear table to a new length.
}
// When an element is inserted to the header of a linear table stored in sequence, all elements in the linear table need to be moved. the time complexity of all algorithms is O (n), and N indicates the length of the linear table.
*/
/*************************************** **************************************** ***
10. insert element X to the end of the linear table L
**************************************** **************************************** **/
Void insertlastlist (struct list * l, elemtype X)
{
If (L-> size = L-> maxsize)
Againmalloc (l);/* call this function to reassign a larger bucket */
L-> list [L-> size] = X;
L-> size ++;
}
/*************************************** **************************************** ***
11. insert element X to the position of the POs element in linear table L. If the insertion is successful, 1 is returned; otherwise, 0 is returned.
**************************************** **************************************** **/
Int insertposlist (struct list * l, int POs, elemtype X)
{
Int I;
If (Pos <1 | POS> L-> size + 1)/* insertion fails if POS is out of bounds */
Return 0;
If (L-> size = L-> maxsize)/* reassign a larger bucket */
Againmalloc (L );
For (I = L-> size-1; I> = pos-1; I --)
L-> list [I + 1] = L-> list [I];
L-> list [pos-1] = X;
L-> size ++;
Return 1;
}
/*************************************** **************************************** ***
12. insert element X to the ordered linear table l so that the inserted element is still ordered.
**************************************** **************************************** **/
Void insertorderlist (struct list * l, elemtype X)
{
Int I, J;
/* If the array space is used up, allocate a larger bucket */
If (L-> size = L-> maxsize)
Againmalloc (L );
/* Find the insert position of X in sequence */
For (I = 0; I <L-> size; I ++)
If (x <L-> list [I]) break;
/* Move one position from the end of the table to the subscript I element one by one and leave the I position blank */
For (j = L-> size-1; j> = I; j --)
L-> list [J + 1] = L-> list [J];
/* Assign the X value to the element whose subscript is I */
L-> list [I] = X;
/* Increase the length of a linear table by 1 */
L-> size ++;
}
/*************************************** **************************************** ***
13. Delete the Header element from the linear table L and return it. If the deletion fails, stop running the program.
**************************************** **************************************** **/
Elemtype deletefirstlist (struct list * l)
{
Elemtype temp;
Int I;
If (L-> size = 0)
{
Printf ("the linear table is empty and cannot be deleted! \ N ");
Exit (1 );
}
Temp = L-> list [0];
For (I = 0; I <L-> size; I ++)
-> List [I-1] = L-> list [I];
L-> size --;
Return temp;
}
/*************************************** **************************************** ***
14. Delete the end element of the linear table L and return it. If the deletion fails, stop running the program.
**************************************** **************************************** **/
Elemtype deletelastlist (struct list * l)
{
If (L-> size = 0)
{
Printf ("the linear table is empty and cannot be deleted! \ N ");
Exit (1 );
}
L-> size --;
Return L-> list [L-> size];
}
/*************************************** **************************************** ***
15. Delete the POs element from the linear table L and return it. If the deletion fails, stop running the program.
**************************************** **************************************** **/
Elemtype deleteposlist (struct list * l, int POS)
{
Elemtype temp;
Int I;
If (Pos <1 | POS> L-> size) {/* If POS is out of bounds, deletion fails */
Printf ("the POs value is out of bounds and cannot be deleted! \ N ");
Exit (1 );
}
Temp = L-> list [pos-1];
For (I = Pos; I <L-> size; I ++)
-> List [I-1] = L-> list [I];
L-> size --;
Return temp;
}
// In this algorithm, the running time is mainly used to move forward N-pos elements in step (3. If the probability of deleting an element at any position is the same, that is, all elements are 1/N, the average number of Moving Elements for each element is, therefore, the time complexity of this algorithm is O (n ).
/*************************************** **************************************** ***
16. Delete the first element with the value of X from the linear table L. If the deletion is successful, 1 is returned. Otherwise, 0 is returned.
**************************************** **************************************** **/
Int deletevaluelist (struct list * l, elemtype X)
{
Int I, J;
/* Find the first element with the value of X from the linear table in sequence */
For (I = 0; I <L-> size; I ++)
If (L-> list [I] = x) break;
/* If the search fails, it indicates that no element with the value of X exists. 0 */is returned */
If (I = L-> size) return 0;
/* Delete the element L-> list [I] */
For (j = I + 1; j <L-> size; j ++)
-> List [J-1] = L-> list [J];
/* The length of a linear table is reduced by 1 */
L-> size --;
/* If the deletion is successful, 1 is returned */
Return 1;
}
Void main ()
{
Int A [10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Int I;
Struct list l;
Initlist (& L, 5 );
For (I = 0; I <10; I ++)
Insertlastlist (& L, A [I]);
Insertposlist (& L, 11, 48 );
Insertposlist (& L, 1, 64 );
Printf ("% d \ n", getelem (& L, 4 ));
Traverselist (& L );
Printf ("% d \ n", findlist (& L, 10 ));
Updateposlist (& L, 3, 20 );
Deletefirstlist (& L );
Deletefirstlist (& L );
Deletelastlist (& L );
Deletelastlist (& L );
Deleteposlist (& L, 5 );
Deleteposlist (& L, 7 );
Printf ("% d \ n", sizelist (& L ));
Printf ("% d \ n", emptylist (& L ));
Traverselist (& L );
/*
Clearlist (& L );
*/
}