C ++ version-linear table

Source: Internet
Author: User

# Include "head. H"
# Include "iostream. H"

# Define list_init_size 100

Template <class elemtype>
Class sqlist
{
PRIVATE:
Elemtype * ELEM;
Int length;
Int listsize;
Public:
Sqlist (INT max = list_init_size );
~ Sqlist () {Delete [] ELEM ;}
Void clearlist ();
STATUS input (int n );
Status output ();
Status listempty ();
Int listlength ();
Elemtype getelem (int I );
Status nextelem (elemtype I, elemtype & next_e );
Status listinsert (int I, elemtype E );
Status listdelete (int I, elemtype & E );
Status inver (); // reverse
// Status mergelist (sqlist la, sqlist lb );
};
Template <class elemtype> sqlist <elemtype>: sqlist (INT max)
{
Listsize = max;
Length = 0;
ELEM = new elemtype [listsize];
};
Template <class elemtype> void sqlist <elemtype>: clearlist ()
{
Delete [] ELEM;
ELEM = new elemtype [listsize];
Length = 0;
};
Template <class elemtype> Status sqlist <elemtype>: input (int n)
{
If (n <= 0) return error;
For (INT I = 0; I <n; I ++)
{
Cout <"Enter the" <I + 1 <"number :";
Cin> ELEM [I];
}
Length = N;
Return OK;
};
Template <class elemtype> Status sqlist <elemtype>: output ()
{
If (length = 0) return error;
Else
{
For (INT I = 0; I <length; I ++)
{
Cout <ELEM [I] <"-> ";
}
Cout <"null" <Endl;
}
Return OK;
};
Template <class elemtype> Status sqlist <elemtype>: listempty ()
{
If (length = 0) return true;
Else return false;
};
Template <class elemtype> int sqlist <elemtype>: listlength ()
{
Return length;
};
Template <class elemtype> elemtype sqlist <elemtype>: getelem (int n)
{
Elemtype E;
If (length = 0) return error;
If (n <1 | n> listlength () return overflow;
E = ELEM [n-1];
Return E;
};
Template <class elemtype> Status sqlist <elemtype>: nextelem (elemtype loc_e, elemtype & next_e)
{
If (length = 0) return error;
For (INT I = 1; loc_e! = ELEM [I-1]; I ++ );
If (I = length) return overflow;
Next_e = ELEM [I];
Return OK;
};
Template <class elemtype> Status sqlist <elemtype>: listinsert (int I, elemtype E)
{
If (I <1 | I> length) return error;
If (listsize = length) return overflow;
For (INT n = length-1; n> = I-1; n --)
ELEM [n + 1] = ELEM [N];
ELEM [I-1] = E;
Length ++;
Return OK;
};
Template <class elemtype> Status sqlist <elemtype>: listdelete (int I, elemtype & E)
{
If (I <1 | I> length) return error;
E = ELEM [I-1];
For (INT n = I-1; n <length-1; n ++)
ELEM [N] = ELEM [n + 1];
Length --;
Return OK;
};
Template <class elemtype> Status sqlist <elemtype>: inver ()
{
Int I, m, n;
Elemtype temp;
N = length;
M = n/2;
For (I = 0; I <m; I ++)
{
Temp = ELEM [I]; ELEM [I] = ELEM [n-i-1]; ELEM [n-i-1] = temp;
}
Return OK;
};
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.