Statement:
# Include <stdio. h>
# Include "stdlib. h"
Typedef int DataType;
1. Construct a body
// Create an empty sequence table
PSeqList createNullList_seq (int m)
{
Required qlist palist = (required qlist) malloc (sizeof (struct SeqList ));
If (palist! = NULL)
{
Palist-> element = (DataType *) malloc (sizeof (DataType) * m );
If (palist-> element)
{
Palist-> MAXNUM = m;
Palist-> n = 0;
Return palist;
}
Else
{
Free (palist );
}
}
Printf ("out of space !! \ N ");
Return NULL;
}
2.
// Determine whether the sequence table indicated by palist is empty.
Int isNullList_Seq (paiqlist palist)
{
Return (palist-> n = 0 );
}
3.
// Search for the element subscript whose first value is X in the sequence table indicated by palist
Int locate_seq (PSeqList palist, DataType x)
{
Int q;
For (q = 0; q <palist-> n; q ++)
{
If (palist-> element [q] = x ){
Return q;
}
}
Return-1;
}
4.
// Insert the sequence table. insert element x before the element of the sequence table indicated by palist with the subscript p.
Int insertPre_seq (partition qlist palist, int p, DataType x)
{
DataType * pos1;
Int q;
If (palist-> n> = palist-> MAXNUM)
{
// Extended sequential tablespace www.2cto.com
Pos1 = (DataType *) malloc (sizeof (DataType) * palist-> MAXNUM * 2 );
If (pos1 = NULL)
{
Printf ("Overflow! ");
Return 0;
}
For (q = 0; q <palist-> MAXNUM; q ++)
{
Pos1 [q] = palist-> element [q];
}
Free (palist-> element );
Palist-> element = pos1;
Palist-> MAXNUM * = 2;
}
If (p <0 | p> palist-> n ){
Printf ("Not Exist !! \ N ");
Return 0;
}
For (q = palist-> n-1; q> = p; q --)
{
Palist-> element [q + 1] = palist-> element [q];
}
Palist-> element [p] = x;
Palist-> n = palist-> n + 1;
Return 1;
}
5.
// Insert the sequence table. Insert the element x after the subscript of the sequence table indicated by palist is p.
Int insertPost_seq (PSeqList palist, int p, DataType x)
{
Int q;
If (palist-> n> = palist-> MAXNUM)
{
Printf ("OverFlow \ n ");
Return 0;
}
If (p <0 | p> palist-> n ){
Printf ("Not Exist !! \ N ");
Return 0;
}
For (q = palist-> n-1; q> = p + 1; q --)
{
Palist-> element [q + 1] = palist-> element [q];
}
Palist-> element [p + 1] = x;
Palist-> n = palist-> n + 1;
Return 1;
}
6.
// Delete the sequence table. Delete the element whose subscript is p in the sequence table specified by palist.
Int deleteP_seq (partition qlist palist, int p)
{
Int q;
If (q <0 | q> palist-> N-1)
{
Printf ("Not Exist !! \ N ");
Return 0;
}
For (q = p; q <palist-> n-1; q ++)
{
Palist-> element [q] = palist-> element [q + 1];
}
Palist-> n = palist-> n-1;
Return 1;
}
7.
Int deleteV_seq (PSeqList palist, DataType x)
{
Int q = locate_seq (palist, x );
DeleteP_seq (palist, q );
Return 1;
}
8.
Test
Int main ()
{
Int I;
Required qlist list1 = createNullList_seq (50 );
InsertPre_seq (list1, 0, 12 );
InsertPre_seq (list1, 1, 22 );
InsertPre_seq (list1, 2, 32 );
InsertPost_seq (list1, 3,43 );
InsertPost_seq (list1, 3,53 );
InsertPost_seq (list1, 4,63 );
InsertPost_seq (list1, 1, 73 );
I = locate_seq (list1, 73 );
Printf ("% d", I );
Return 1;
}
From yucong