我把类发给你
array:
#include <iostream.h>
#include <stdlib.h>
#define DefaultSize 10
template < class Type>
class Array {
public:
Array( int Size = DefaultSize );
Array( const Array <Type> &x );
~Array() { destroy(); }
Array<Type> &operator = ( const Array<Type> &A);
Type & operator [] ( int i );
// Array<Type> operator Type * ()const { return elements; }
int Length() const { return ArraySize; }
void ReSize ( int sz);
friend ostream& operator <<(ostream& , Array<Type>&);
// void Print();
private:
Type *elements;
int ArraySize;
void getArray();
void destroy(){delete[] elements;};
void copyFrom(const Array<Type> &x);
}
template <class Type>
void Array<Type>::copyFrom(const Array<Type> &x)
{
int n= x.ArraySize;
ArraySize=n;
elements = new Type[x.ArraySize];
if ( elements == 0 )
{
cerr << "Memory Allocation Error" << endl;
ArraySize=0;
return;
}
Type *srcptr = x.elements;
Type *destptr = elements;
while (n--) *destptr++ = * srcptr++ ;
};
template <class Type> void Array<Type>::getArray() {
elements = new Type[ArraySize];
if ( elements == 0 )
{
cerr << "Memory Allocation Error" << endl;
ArraySize=0;
}
}
template <class Type> void Array<Type>::Array (int sz) {
if ( sz <= 0 ) { cerr << "Invalid Array Size" << endl; return; }
ArraySize = sz;
getArray();
}
template <class Type> void Array<Type>::Array ( const Array<Type> &x ) {
copyFrom(x);
}
template <class Type> Type& Array<Type>::operator[] (int i) {
if ( i < 0 || i > ArraySize - 1 ) cerr << "Index out of Range" << endl;
return elements[i];
}
template <class Type>
void Array<Type>::ReSize( int sz ) {
if (( sz <= ArraySize )&&(sz>=0))
{
Type *newarray = new Type[sz];
if (newarray == 0)
{
cerr << "Memory Allocation Error" << endl;
return;
}
int n = ( sz <= ArraySize ) ? sz : ArraySize;
Type *srcptr = elements;
Type *destptr = newarray;
while (n--) *destptr++ = *srcptr++ ;
delete[] elements;
elements = newarray;
ArraySize = sz;
}
}
template <class Type>
Array<Type> & Array<Type>::operator = ( const Array<Type> &a)
{
destroy();
copyFrom(a);
return *this;
}
template <class Type>
ostream& operator <<(ostream& strm, Array<Type>& a)
{
Type *p=a.elements;
strm<<"Len:"<<a.ArraySize<<endl;
for (int i=0;i<a.ArraySize;i++,p++)
{
strm<<*p<<' ';
}
strm<<endl;
return strm;
}
/*
template <class Type>
void Array<Type>::Print()
{
Type *p=elements;
cout<<"Len:"<<ArraySize<<endl;
for (int i=0;i<ArraySize;i++,p++)
{
cout<<*p<<' ';
}
cout<<endl;
}
*/
seqlist:
#include <stdio.h>
#include <assert.h>
#define DefaultSize 100
template <class Type> class SeqList {
public:
SeqList ( const int size = DefaultSize );
~SeqList() { delete[] data; }
int Length() const { return last + 1; }
int Find( const Type & x ) const;
int IsIn ( Type & x);
int Insert ( Type & x, int i );
int Remove ( Type & x);
int Next ( Type & x );
int Prior ( Type & x );
int IsEmpty()const { return last == -1; }
int IsFull() const { return last == MaxSize - 1; }
Type Get( int i ) { return i < 0 || i > last ? NULL:data[i]; }
void Print();
private:
Type *data;
int MaxSize;
int last;
};
template < class Type > SeqList <Type>::SeqList( const int size ) {
assert ( size >= 0 );
if ( size > 0 ) {
MaxSize = size; last = -1;
data = new Type[MaxSize];
}
};
template < class Type > int SeqList <Type>::Find(const Type & x ) const {
int i = 0;
while ( i <= last && data[i] != x ) i++;
if ( i > last ) return -1;
else return i;
}
template < class Type > int SeqList <Type>::IsIn( Type & x ) {
int i = 0, found = 0;
while ( i <= last && !found)
if ( data[i] != x ) i++;
else found = 1;
return found;
}
template < class Type > int SeqList <Type>::Insert( Type & x, int i ) {
if ( i < 0 || i > last+1 || last == MaxSize - 1 ) return 0;
else {
last++;
for ( int j = last; j > i; j-- ) data[j] = data[j-1];
data[i] = x;
return 1;
}
}
template < class Type > int SeqList <Type>::Remove( Type & x ) {
int i = Find(x);
if ( i >= 0 ) {
last--;
for ( int j = i; j <= last; j++ ) data[j] = data[j+1];
return 1;
}
return 0;
}
template < class Type > int SeqList <Type>::Next( Type & x ) {
int i = Find(x);
if ( i >= 0 && i < last ) return i+1;
else return -1;
}
template < class Type > int SeqList <Type>::Prior( Type & x ) {
int i = Find(x);
if ( i > 0 && i <= last ) return i-1;
else return -1;
}
template < class Type > void Union( SeqList <Type> & LA, SeqList <Type> & LB ) {
int n = LA.Length(); int m = LB.Length();
for ( int i=0; i <= m; i++ ) {
Type x = LB.Get(i);
int k = LA.Find(x);
if ( k == -1 ) { LA.Insert( x, n ); n++;}
}
}
template < class Type > void Intersection ( SeqList <Type> & LA, SeqList <Type> & LB ) {
int n = LA.Length(); int m = LB.Length(); int i = 0;
while ( i < n ) {
Type x = LA.Get(i);
int k = LB.Find(x);
if ( k == -1 ) { LA.Remove(x); n--; }
else i++;
}
}
template < class Type > void SeqList <Type>::Print() {
if ( last == -1 ) cout << "It is empty" ;
else for ( int i=0; i<=last; cout << " data[" << i++ << "] = " << data[i] );
cout << endl;
}