正在加载图片...
非序 静态数据表类定义 #include <iostream. h> template <class Type> class dataList ∥数据表的前视声明 template <class Type> class Element ∥数据表元素类的定义 Type key ∥排序码 ∥其它数据成 Type getKey (i return key: ∥取当前结点的排序码 void setKey( const Type x )i key=x;) ∥将当前结点的排序码修改为x Element.<Iype>& operator=(emen<Iyp>&x)结点x的值赋给this i key =x->key; otherdata =x-otherdata;) ey;}消判this与x相等 int operator<=(Type&x){ return key s=x->key;}/判this小于或等于x int operator>(Type&x){ return key>x->key;}m判this大于x nt operator<(Iype&x){ return key>x->key;}m判this小于 template <class Type> class dataList 用顺序表来存储待排序的元素,这些元素的类型是Type Element <Type>*Vector; ∥存储待排序元素的向量 ∥最大元素个数与当前元素个数 int Partition( const int low, const int high ∥用于快速排序的一次划分算法 datalist( int MaxSz= DefaultSize ) Maxsize( Maxsz ) CurrentSize(0) Vector=new Element <Type>[MaxSize:;) ∥构造函数 int length(( return CurrentSize; U(int i)( return Vector[;) void swap( Element<ype>&x, lement<Iype>&y)∥交换x,y ∥排序 静态链表类定义 template <class Type> class staticlinkList; 静态链表类的前视声明 template <class Type> class Element i 1静态链表元素类的定义 friend class staticlinkList<Type>: Type key: ∥排序码,其它信息略 ∥结点的链接指针第 9 章 排序 1 静态数据表类定义 #include <iostream.h> const int DefaultSize = 100; template <class Type> class dataList //数据表的前视声明 template <class Type> class Element { //数据表元素类的定义 friend class dataList <Type>; private: Type key; //排序码 field otherdata; //其它数据成员 public: Type getKey ( ) { return key; } //取当前结点的排序码 void setKey ( const Type x ) { key = x; } //将当前结点的排序码修改为 x Element<Type>& operator = ( Element<Type>& x ) //结点 x 的值赋给 this { key = x->key; otherdata = x->otherdata; } int operator == ( Type& x ) { return key == x->key; } //判 this 与 x 相等 int operator <= ( Type& x ) { return key <= x->key; } //判 this 小于或等于 x int operator > ( Type& x ) { return key > x->key; } //判 this 大于 x int operator < ( Type& x ) { return key > x->key; } //判 this 小于 x } template <class Type> class dataList { //用顺序表来存储待排序的元素,这些元素的类型是 Type private: Element <Type> * Vector; //存储待排序元素的向量 int MaxSize, CurrentSize; //最大元素个数与当前元素个数 int Partition ( const int low, const int high ) //用于快速排序的一次划分算法 public: datalist ( int MaxSz = DefaultSize ) : MaxSize ( Maxsz ), CurrentSize (0) { Vector = new Element <Type> [MaxSize]; } //构造函数 int length ( ) { return CurrentSize; } Element<Type>& operator [ ] ( int i ) { return Vector[i]; } void swap ( Element <Type>& x, Element <Type>& y ) //交换 x, y { Element <Type> temp = x; x = y; y = temp; } void Sort ( ); //排序 } 静态链表类定义 template <class Type> class staticlinkList; //静态链表类的前视声明 template <class Type> class Element { //静态链表元素类的定义 friend class staticlinkList<Type>; private: Type key; //排序码,其它信息略 int link; //结点的链接指针 public:
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有