# Include <stdio. h>
# Include "sqlist. H"
Int main (void)
{
Int num = 0;
Char E;
Sqlist L;
Initlist (& L );
// Insert element
Inselem (& L, 'A', 1 );
Inselem (& L, 'B', 2 );
Inselem (& L, 'C', 3 );
Inselem (& L, 'D', 4 );
Inselem (& L, 'E', 5 );
Inselem (& L, 'F', 6 );
Inselem (& L, 'G', 7 );
Inselem (& L, 'h', 8 );
Inselem (& L, 'I', 9 );
Printf ("linear table :");
Displist (L );
Printf ("Length :");
Printf ("% d", getlength (l ));
Printf ("/N ");
Num = 3;
Getelem (L, num, & E );
Printf ("element % d: % C/N", num, e );
E = 'D ';
Printf ("element % C is the % d element/N", E, locate (L, E ));
Num = 4;
Printf ("delete element % d/N", num );
Delelem (& L, num );
Printf ("linear table :");
Displist (L );
Printf ("Length :");
Printf ("% d", getlength (l ));
Printf ("/N ");
Return 0;
}
[CPP] view plaincopyprint?
# Ifndef sqlist
# Define sqlist
# Deprecision max_size 100
Typedef struct {
Char data [max_size];
Int length;
} Sqlist;
Extern void initlist (sqlist * l); // initialize the linear table
Extern int getlength (sqlist L); // evaluate the length of a linear table
Extern int getelem (sqlist L, int num, char * E); // evaluate the I element in a linear table
Extern int locate (sqlist L, char X); // search for elements by value
Extern int inselem (sqlist * l, char X, int num); // insert an element
Extern int delelem (sqlist * l, int num); // delete an element
Extern void displist (sqlist L); // output element value
# Endif
[CPP] view plaincopyprint?
# Include "sqlist. H"
/*************************************** ******************
** Function name: void initlist (sqlist * l)
** Function: initialize a linear table.
** Description: The length is initialized to 0.
** By Pang Hui
**************************************** ******************/
Void initlist (sqlist * l)
{
L-> length = 0;
}
/*************************************** ******************
** Function name: int getlength (sqlist L)
** Function: Calculate the length of a linear table.
** Description: None
** By Pang Hui
**************************************** ******************/
Int getlength (sqlist L)
{
Return L. length;
}
/*************************************** ******************
** Function name: int getelem (sqlist L, int I, char * E)
** Function: Calculate the I element of a linear table.
** Description:-1 is returned for an error. 0 is returned if it is found. The element value is stored in parameter E.
** By Pang Hui
**************************************** ******************/
Int getelem (sqlist L, int num, char * E)
{
If (Num <1 | num> L. length)
{
Return-1;
}
Else
{
* E = L. Data [num-1];
Return 0;
}
}
/*************************************** ******************
** Function name: int locate (sqlist L, char X)
** Function: Search for elements by value
** Description:-1 is returned for an error. If yes, the return position is returned (metering starts from 1)
** By Pang Hui
**************************************** ******************/
Int locate (sqlist L, char X)
{
Int I = 0;
For (I = 0; I <L. length; I ++)
{
If (L. Data [I] = X)
{
Return I + 1;
}
}
Return-1;
}
/*************************************** ******************
** Function name: int inselem (sqlist * l, char X, int num)
** Function: insert an element at a certain position
** Description:-1 is returned for an error, and 0 is returned for a successful error.
** By Pang Hui
**************************************** ******************/
Int inselem (sqlist * l, char X, int num)
{
Int I = 0;
If (Num <1 | num> L-> Length + 1) | (L-> length = max_size ))
{
Return-1;
}
Else
{
For (I = L-> length-1; I> num-2; I --)
{
L-> data [I + 1] = L-> data [I];
}
L-> data [I + 1] = X;
L-> length ++;
Return 0;
}
}
/*************************************** ******************
** Function name: int delelem (sqlist * l, int num)
** Function: delete an element in a certain position.
** Description:-1 is returned for an error, and 0 is returned for a successful error.
** By Pang Hui
**************************************** ******************/
Int delelem (sqlist * l, int num)
{
Int I = 0;
If (Num <1 | num> L-> length)
{
Return-1;
}
Else
{
For (I = num; I <L-> length; I ++)
{
L-> data [I-1] = L-> data [I];
}
L-> length --;
}
}
/*************************************** ******************
** Function name: void displist (sqlist L)
** Function: Output element value
** Description: None
** By Pang Hui
**************************************** ******************/
Void displist (sqlist L)
{
Int I = 0;
For (I = 0; I <L. length; I ++)
{
Printf ("% C", L. Data [I]);
}
Printf ("/N ");
}