P38test.cpp
资源名称:ds1-3.rar [点击查看]
上传用户:chaiyuqiu
上传日期:2022-08-03
资源大小:27k
文件大小:2k
源码类别:
数据结构
开发平台:
C/C++
- #include <stdio.h>
- #include <assert.h>
- #define DefaultSize 100
- template <class Type> class SeqList {
- public:
- SeqList ( int size = DefaultSize );
- ~SeqList() { delete[] data; }
- int Length() const { return last + 1; }
- int Find( 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() { return last == -1; }
- int IsFull() { 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( int size = DefaultSize ) {
- assert ( size >= 0 );
- if ( size > 0 ) {
- MaxSize = size; last = -1;
- data = new Type[MaxSize];
- }
- }
- template < class Type > int SeqList <Type>::Find( 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;
- }
- }
- template < class Type > int SeqList <Type>