正在加载图片...
第2章数组 第2章数组 作为抽象数据类型数组的类声明。 #include <iostream h> 在头文件“ array. h”中 const int DefaultS ize =30: template <class Type> class array i ∥数组是数据类型相同的n(sie)个元素的一个集合,下标范围从0到n-1。对数组中元素 ∥可按下标所指示位置直接访问 /数组 ∥元素个数 public Array int Size DefaultSsize ) ∥构造函数 ∥复制构造函数 -Array (i delete elements; 3 ∥析构函数 Amay<Iype>& operator=( const array<Iype>&A);∥数组整体赋值(复制) Type& operator [( int 1 ) ∥按下标访问数组元素 int Length()const( return Array Size; 3 ∥取数组长度 void Resize( int Sz ) ∥修改数组长度 顺序表的类定义 ∥定义在头文件“ seqlist. h”中 #include <stdlib h> template <class Type> class Seqlist i private: ∥顺序表的存放数组 int MaxSize. ∥顺序表的最大可容纳项数 int last: ∥顺序表当前已存表项的最后位置 顺序表的当前指针(最近处理的表项) Seqlist( int MaxSize ) ∥构造函数 -Seqlist (i delete data; ∥析构函数 int Length()const i return last+1; 1 计算表长度 int Find( Type& x )const; ∥定位函数:找x在表中位置,置为当前表项 int IsIn( Type& x )i ∥判断x是否在表中,不置为当前表项 Type* GetData(){ return current=-1?NULL: data[ curren;}∥当前表项的值 插入x在表中当前表项之后,置为当前表项 int Append Type& x /加x到表尾,置为当前表项 Type Remove( Type& x) ∥删除x,置下一表项为当前表项 Type* First (; ∥取表中第一个表项的值,置为当前表项第 2 章 数组 9 第 2 章 数组 作为抽象数据类型数组的类声明。 #include <iostream.h> //在头文件“array.h”中 #include <stdlib.h> const int DefaultSize = 30; template <class Type> class Array { //数组是数据类型相同的 n(size)个元素的一个集合, 下标范围从 0 到 n-1。对数组中元素 //可按下标所指示位置直接访问。 private: Type *elements; //数组 int ArraySize; //元素个数 public: Array ( int Size = DefaultSize ); //构造函数 Array ( const Array<Type> & x ); //复制构造函数 ~Array ( ) { delete [ ] elements; } //析构函数 Array<Type> & operator = ( const Array<Type> & A ); //数组整体赋值 (复制) Type& operator [ ] ( int i ); //按下标访问数组元素 int Length ( ) const { return ArraySize; } //取数组长度 void ReSize ( int sz ); //修改数组长度 } 顺序表的类定义 #include < iostream.h> //定义在头文件“seqlist.h”中 #include <stdlib.h> template <class Type> class SeqList { private: Type *data; //顺序表的存放数组 int MaxSize; //顺序表的最大可容纳项数 int last; //顺序表当前已存表项的最后位置 int current; //顺序表的当前指针(最近处理的表项) public: SeqList ( int MaxSize ); //构造函数 ~SeqList ( ) { delete [ ] data; } //析构函数 int Length ( ) const { return last+1; } //计算表长度 int Find ( Type& x ) const; //定位函数: 找 x 在表中位置,置为当前表项 int IsIn ( Type& x ); //判断 x 是否在表中,不置为当前表项 Type *GetData ( ) { return current == -1?NULL : data[current]; } //取当前表项的值 int Insert ( Type& x ); //插入 x 在表中当前表项之后,置为当前表项 int Append ( Type& x ); //追加 x 到表尾,置为当前表项 Type * Remove ( Type& x ); //删除 x,置下一表项为当前表项 Type * First ( ); //取表中第一个表项的值,置为当前表项
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有