Home > Back-end >  Data structure sequence table C class implements
Data structure sequence table C class implements

Time:11-27

#include
using namespace std;
Const int MaxSize=100;
The template & lt; The class DataType>
The class SeqList
{
Public:
SeqList () {length=0; }
SeqList (DataType a [], int n);
~ SeqList () {};
Int Length () {return Length; }//o long
DataType Get (int I);//by a lookup
Int the Locate DataType (x);//by value lookup
Void Insert (int I DataType x);//insert
DataType Delete (int I);//positioning delete
Void PrintList ();//print
Void Reverse ();//inverse buy
Void Cancel (int I);//delete the specific number
Void the Sort ();//sort (insert)
Private:
DataType data [MaxSize];
Int length;
};

The template & lt; The class DataType>
SeqList : : SeqList (DataType a [], int n)
{
If (n> MaxSize) throw "wrong parameter";
for(int i=0; iData=[I] a [I];
Length=n;
}

The template & lt; The class DataType>
DataType SeqList : : Get (int I)
{
if(i<1 & amp; & I> Length) throw "wrong Location";
The else return data [I - 1);
}

The template & lt; The class DataType>
Int SeqList : : Locate DataType (x)
{
for(int i=0; iIf (data [I]==x) return the I + 1;
return 0;
}

The template & lt; The class DataType>
Void SeqList : : Insert (int I, DataType x)
{
If (length>=MaxSize) throw "Overflow";
if(i<1 | | i> Length + 1) throw "Location";
For (int j=length; j>=i; J -)
Data [j]=data [j - 1);
data[i-1]=x;
length++;
}

The template & lt; The class DataType>
DataType SeqList : : Delete (int I)
{
int x;
If (length==0) throw "Underflow";
if(i<1 | | i> Length) throw "Location";
X=data [I - 1);
For (int j=I; jData [1]=data [j];
Length -;
Return the x;
}

The template & lt; The class DataType>
Void SeqList : : PrintList ()
{
for(int i=0; icout}

The template & lt; The class DataType>
Void SeqList : : Reverse ()
{
Int j=length;
for(int i=0; i{
Int TMP=0;
TMP=data [I];
The data=data [I] [1];
Data [1]=TMP;
J=j - 1;
}
}

The template & lt; The class DataType>
Void SeqList : : Sort ()
{
Int I, j, temp.
for(i=1; i{
Temp=data [I];
for(j=i-1; j>=0 & amp; & TempThe data=data [m + 1] [j];
Data [j + 1)=temp;
}
}


The template & lt; The class DataType>
Void SeqList : : Cancel (int x)
{
Int I, a=0, k;
Int j=length - 1;
for(i=0; i<=j; I++)
{
Int k=I;
If (data [k]==x)
{
A=1;
- length;
}
While (a==1 & amp; & K<=j)
{
Data=data [k] [k + 1);
k++;
}
a=0;
}
}

Int main ()
{
SeqList p;
SeqList Q;
P.I nsert (1, 5);
P.I nsert (2, 9);
P.I nsert (3, 3);
P.I nsert (4, 2);
P.I nsert (5, 7);
P. ort ();
//p.R everse ();
//p.C ancel (5);
P.P rintList ();
return 0;
}
  • Related