// ----------------------------------------- Classarrlist. h --------------------------------------------------
Template <class T>
Class arrlist
{// Sequence table, Vector
PRIVATE: // value type and value space of a linear table
T * alist; // Private variable, which stores the sequence table instance
Int maxsize; // Private variable, maximum length of the sequence table instance
Int curlen; // Private variable, current length of the sequence table instance
Int position; // Private variable, current processing location
Public: // operation set of the sequence table
Arrlist (const int size)
{// Create a new sequence table. The parameter is the maximum length of the table instance.
Maxsize = size; alist = new T [maxsize]; curlen = position = 0;
}
~ Arrlist ()
{// Destructor used to remove the table instance
Delete [] alist;
Cout <"destructor" <Endl;
}
Void clear ()
{// Clear the content stored in the sequence table to an empty table
Delete [] alist; curlen = position = 0;
Alist = new T [maxsize];
}
Int length (); // returns the actual length of the table in this sequence.
Bool append (const T value); // Add an element value at the end of the table. The length of the table increases by 1.
Bool insert (const int P, const T value); // insert an element value on position P, and the table length increases by 1
Bool delete1 (const int P); // Delete the element on position P. The table length is reduced by 1.
Bool setvalue (const int P, const T value); // use value to modify the element value of position P
Bool getvalue (const int P, T & value); // return the element value of position P to the variable value.
Bool getpos (Int & P, const T value); // find the element whose value is value, and return the position where it appears 1st times.
Void output ();
};
Template <class T>
Int arrlist <t >:: length ()
{
Return curlen;
}
Template <class T>
Bool arrlist <t >:: append (const T value)
{
Alist [curlen ++] = value;
Return true;
}
Template <class T>
Bool arrlist <t>: insert (const int P, const T value) // insert an element value on position P, increasing the length of the table by 1
{
If (curlen> = maxsize)
Cout <"The list is overflow! "<Endl;
If (p <0 | P> = maxsize)
Cout <"The position is illegal! "<Endl;
For (INT I = curLen-1; I> = P; I --)
Alist [I + 1] = alist [I];
Curlen ++;
Alist [p] = value;
Return true;
}
Template <class T>
Bool arrlist <t>: delete1 (const int p) // Delete the element on position P. The table length is reduced by 1.
{
If (curlen> = maxsize)
Cout <"The list is overflow! "<Endl;
If (p <0 | P> = maxsize)
Cout <"The position is illegal! "<Endl;
For (INT I = P; I <curLen-1; I ++)
Alist [I] = alist [I + 1];
Curlen --;
Return true;
}
Template <class T> bool arrlist <t>: setvalue (const int P, const T value) // use value to modify the element value of position P
{
If (curlen> = maxsize)
Cout <"The list is overflow! "<Endl;
If (p <0 | P> maxsize)
Cout <"The position is illegal! "<Endl;
Alist [p] = value;
Return true;
}
Template <class T>
Bool arrlist <t>: getvalue (const int P, T & Value) // return the element value of position P to the variable value.
{
If (curlen> = maxsize)
Cout <"The list is overflow! "<Endl;
If (p <0 | P> maxsize)
Cout <"The position is illegal! "<Endl;
Value = alist [p];
Return true;
}
Template <class T>
Bool arrlist <t>: getpos (Int & P, const T value) // finds the element whose value is value and returns the position where it appears 1st times.
{
If (curlen> = maxsize)
Cout <"The list is overflow! "<Endl;
For (INT I = 0; I <curlen; I ++)
{
If (alist [I] = value)
{
P = I;
Break;
}
}
Return true;
}
Template <class T>
Void arrlist <t>: output () // output sequence table function
{
For (INT I = 0; I <length (); I ++)
{
T value;
Getvalue (I, value );
Cout <value <"";
}
Cout <Endl;
}
// -------------------------------------- Main fun. cpp -------------------------------------
# Include "iostream"
# Include "classarrlist. H"
Using namespace STD;
Int main ()
{
Cout <"---------------- initialization sequence table -----------------" <Endl;
Arrlist <int> List (13 );
Int input;
Int I;
Cout <"Enter 6 numbers :";
For (I = 0; I <6; I ++)
{
Cin> input;
List. append (input );
}
List. Output ();
Cout <"-------------- initial sequence table length ---------------" <Endl;
Cout <"length of arrlist:" <list. Length () <Endl;
Cout <"----------- insert a data ------------" <Endl;
List. insert (3,7 );
List. Output ();
Cout <"------------- delete data on the third digit ------------" <Endl;
List. delete1 (3 );
List. Output ();
Cout <"------------- modify the third data on --------------" <Endl;
List. setvalue (3, 7 );
List. Output ();
Cout <"------------ returns the third data value -------------" <Endl;
Int value;
List. getvalue (3, value );
Cout <"the third value of the List is" <value <Endl;
Cout <"----- output the result of the first location search operation ------" <Endl;
Int P;
List. getpos (p, 7 );
Cout <"7 is first appeared in list is the position" <p <Endl;
Cout <"---------------- call the clearing function ---------------" <Endl;
List. Clear ();
Cout <"the longth of list is" <list. Length () <Endl;
Cout <"---------------- call the Destructor -----------------" <Endl;
Return 0;
}