Implementation of sequential Table C ++ for linear table
Order table of linear table
I. header file: SeqList. h
// Header file of the ordered linear table
# Include <iostream>
Const int MaxSize = 100;
// Define the template class of the sequence table SeqList
Template <class DataType>
Class SeqList {
Public:
// Sequence table without parameter Constructor (create an empty sequence table)
SeqList () {length = 0}
// Ordered table has a parameter Constructor (create an ordered table with a length of n)
SeqList (DataType array [], int n );
// Sequence table destructor
~ SeqList (){}
// Calculate the length of the sequence table
Int GetLength () {return length ;}
// The sequence table searches by bit and returns the elements at the I position.
DataType GetElement (int I );
// The sequence table is queried by value and returns the position of the element.
Int GetLocal (DataType x );
// Insert the specified element at the specified position in the sequence table
Void Insert (int I, DataType x );
// Delete an element from an ordered table. The deleted element is returned.
DataType Delete (int I );
// Output the elements in the sequence table
Void PrintSeqList ();
Private:
// One-dimensional array for storing data elements
DataType data [MaxSize];
// Sequence table length
Int length;
};
// Implement the sequence table with a parameter Constructor
Template <class DataType>
SeqList <DataType >:: SeqList (DataType array [], int n)
{
If (n> MaxSize)
{
Throw "the length of the input sequence table is too long ";
}
// Assign values to the array of elements stored in the sequence table
For (int I = 0; I <n; I ++)
{
Data [I] = array [I];
}
// Assign a value to the length of the sequence table
Length = n;
}
// Implement sequential table searching by bit
Template <class DataType>
DataType SeqList <DataType>: GetElement (int I)
{
// Determine whether the location is correct
If (I <1 | I> length)
{
Throw "incorrect position ";
}
Else
{
// Returns the element at the specified position.
Return data [I-1];
}
}
// Implement value-based search for the ordered table and return the position of the element
Template <class DataType>
Int SeqList <DataType >:: GetLocal (DataType x)
{
// Traverse the elements of the sequence table
For (int I = 0; I <length; I ++)
{
// Determine whether the specified element is in the sequence table
If (data [I] = x)
{
// Returns the position of the specified Element in the sequence table.
Return (I + 1 );
}
}
// If the specified element is not in the sequence table, the return position is 0.
Return 0;
}
// Insert elements into the sequence table
Template <class DataType>
Void SeqList <DataType>: Insert (int index, DataType x)
{
// Determine whether the insert position is reasonable
If (length> = MaxSize)
{
Throw "the sequence table has been fully stored ";
}
If (index <1 | index> length + 1)
{
Throw "An error occurred while inserting the element ";
}
// If the insert position is reasonable, the elements in the sequence table are moved backward from the last position to the specified insert position.
For (int j = length; j> = index; j --)
{
Data [j] = data [j-1];
}
// Insert the specified element to the inserted position
Data [index-1] = x;
Length ++;
}
// Deletes the elements at the specified position in the sequence table.
Template <class DataType>
DataType SeqList <DataType>: Delete (int index)
{
// Declare the elements to be retrieved
DataType x;
// Determine whether the location to be deleted is reasonable
If (index <1 | index> length)
{
Throw "The deletion location is incorrect ";
}
Else
{
// Retrieve the elements at the specified position
X = data [index-1];
// Move all elements at the specified position forward to a position
For (int I = index; I <length; I ++)
{
Data [I-1] = data [I];
}
// After the elements in the sequence table are deleted, the length of the elements is reduced by 1.
Length --;
}
Return x;
}
// Elements in the sequence output table
Template <class DataType>
Void SeqList <DataType>: PrintSeqList ()
{
If (length <1)
{
Throw "no element in the sequence table ";
}
Else
{
// Ordered output sequence table elements
For (int I = 0; I <length; I ++)
{
Cout <data [I] <"";
}
Cout <endl;
}
}
Ii. Test the sequence table of the linear table: TestSeqList. cpp
# Include <iostream>
# Include "SeqList. h"
Using namespace std;
Void show ()
{
Cout <"---------------------------------------" <endl;
}
Int main ()
{
Int array [10] = {1, 3, 4, 2, 5, 6, 8, 7, 9, 10 };
SeqList <int> seqList = SeqList <int> (array, 10 );
Cout <"sequence table:" <endl;
SeqList. PrintSeqList ();
Show ();
Cout <"sequence table length:" <seqList. GetLength () <endl;
Cout <"the third element is:" <seqList. GetElement (3) <endl;
Cout <"the position of element 3 is:" <seqList. GetLocal (3) <endl;
Show ();
Cout <"insert element 22 in 5th locations" <endl;
SeqList. Insert (5, 22 );
Cout <"sequence table:" <endl;
SeqList. PrintSeqList ();
Cout <"sequence table length:" <seqList. GetLength () <endl;
Show ();
Cout <"deleting elements in 5th locations" <endl;
SeqList. Delete (5 );
Cout <"sequence table:" <endl;
SeqList. PrintSeqList ();
Cout <"sequence table length:" <seqList. GetLength () <endl;
Show ();
Return 0;
}