This series of logs is an operation exerciseCode, Reference book C/C ++AlgorithmManual.
// Sequence table sequential list
# Include <stdio. h>
# Include <string. h>
# Define maxlen 100 // defines the maximum length of an ordered table
Typedef struct // define the node type
{
Char key [10]; // node keyword
Char name [20];
Int age;
} Data;
Typedef struct // define the sequence table structure
{
Data listdata [maxlen + 1]; // Save the structure array of the sequence table. The data node is recorded from subscript 1, and the subscript is not used at position 0.
Int listlen; // number of existing nodes in the sequence table
} Sltype;
Void slinit (sltype * SL) // initialize the sequence table
{
SL-> listlen = 0; // empty table is initialized, and no table is cleared. If data in the table can be overwritten
}
Int sllength (sltype * SL) // returns the number of elements in the sequence table.
{
Return sl-> listlen;
}
int slinsert (sltype * SL, int N, data) // insert a node
{< br> int I;
If (SL-> listlen> = maxlen)
{< br> printf ("the sequence table is full and cannot be inserted to nodes! \ N ");
return 0;
}< br> If (n <1 | n> sl-> ListLen-1)
{< br> printf ("the number of inserted elements is incorrect and cannot be inserted! \ N ");
return 0;
}< br> for (I = SL-> listlen; I >= N; I --) // move the sequence table node back
{< br> sl-> listdata [I + 1] = SL-> listdata [I];
}< br> sl-> listdata [I] = data;
sl-> listlen ++;
return 1;
}
int sladd (sltype * SL, data) // append node
{< br> If (SL-> listlen> = maxlen)
{< br> printf ("the sequence table is full and nodes cannot be inserted! \ N ");
return 0;
}< br> sl-> listdata [++ sl-> listlen] = data;
return 1;
}
Int sldelete (sltype * SL, int N) // delete a node
{
Int I;
If (n <1 | n> sl-> listlen)
{
Printf ("An error occurred while deleting the node number. You cannot delete the node! \ N ");
Return 0;
}
For (I = N; I <sl-> listlen; I ++)
{
SL-> listdata [I] = SL-> listdata [I + 1];
}
SL-> listlen --;
Return 1;
}
Data * slfindbynum (sltype * SL, int N) // search nodes by serial number
{
If (n <1 | n> sl-> listlen + 1)
{
Printf ("the node serial number is incorrect. The node cannot be returned! \ N ");
Return NULL;
}
Return & (SL-> listdata [N]);
}
Int slfindbycont (sltype * SL, char * Key) // search nodes by keyword
{
Int I;
For (I = 1; I <sl-> listlen; I ++)
{
If (strcmp (SL-> listdata [I]. key, key) = 0)
{
Return 1;
}
}
Return 0;
}
Int slall (sltype * SL)
{
Int I;
For (I = 1; I <= SL-> listlen; I ++)
{
Printf ("(% s, % s, % d) \ n", SL-> listdata [I]. key, SL-> listdata [I]. name, SL-> listdata [I]. age );
}
Return 0;
}
Int main ()
{
Int I;
Sltype SL;
Data data;
Data * pdata;
Char key [10];
Printf ("sequence table operation demo! \ N ");
Slinit (& SL );
Printf ("sequence table Initialization is complete! \ N ");
Do {
Printf ("Enter the added node (student ID name age ):");
Fflush (stdin );
Scanf ("% S % d", & Data. Key, & Data. Name, & Data. Age );
If (data. Age)
{
If (! Sladd (& SL, data ))
{
Break;
}
}
Else
{
Break;
}
} While (1 );
Printf ("\ n sequence table node order: \ n ");
Slall (& SL );
Fflush (stdin );
Printf ("\ n serial number of the node to be retrieved :");
Scanf ("% d", & I );
Pdata = slfindbynum (& SL, I );
If (pdata)
{
Printf ("Node % d is :( % s, % s, % d) \ n", I, pdata-> key, pdata-> name, pdata-> age );
}
Fflush (stdin );
Printf ("\ n enter the keyword to be searched :");
Scanf ("% s", key );
I = slfindbycont (& SL, key );
Pdata = slfindbynum (& SL, I );
If (pdata)
{
Printf ("Node % d is :( % s, % s, % d) \ n", I, pdata-> key, pdata-> name, pdata-> age );
}
Printf ("\ n enter the node number to be deleted :");
Scanf ("% d", & I );
Sldelete (& SL, I );
Slall (& SL );
Getchar ();
Return 0;
}