?? p88.cpp
字號:
#include <stdio.h> #include <iostream.h> template <class Type> class DblList; template <class Type> class DblNode { friend class DblList<Type>; private: Type data; //鏈表結點數據 DblNode<Type> *lLink, *rLink; //鏈表前驅(左鏈)、后繼(右鏈)指針 DblNode ( Type value, DblNode<Type> *left, DblNode *right ) : data (value), lLink (left), rLink (right) { } //構造函數 DblNode ( Type value ) : data (value), lLink (NULL), rLink (NULL) { } //構造函數 }; template <class Type> class DblList { public: DblList ( Type uniqueVal ); //構造函數: 建立雙向循環鏈表的表頭結點。 ~DblList ( ); //析構函數: 釋放雙向循環鏈表所用存儲。 int Length ( ) const; //計算雙向循環鏈表的長度。 int IsEmpty ( ) { return first->rLink == first; } //判雙向循環鏈表空否 int Find ( const Type & target ); //在鏈表中尋找等于給定值的結點。 Type getData ( ) const ; //返回當前結點中存儲的值。 void Firster ( ) { current = first; } //初始化: 將當前指針指到表頭結點。 int First ( ); //當前指針指向鏈表表頭結點。 int Next ( ); //當前指針指到當前結點的后繼結點。 int Prior ( ); //當前指針指到當前結點的前驅結點。 int operator ! ( ) { return current != NULL; } //重載操作符: 判當前指針current空否 void Insert ( const Type & value ); //插入一個包含有值value的新結點。 void Remove ( ); //刪除當前結點。 void Print (); private: DblNode<Type> *first, *current; }; template <class Type> DblList<Type>::DblList ( Type uniqueVal ) { //構造函數: 建立雙向循環鏈表的表頭結點, 它包含了一個用于某些特定情況的值。 first = new DblNode<Type> ( uniqueVal ); first->rLink = first->lLink = first; current = NULL; } template <class Type> DblList<Type>::~DblList() { current = first->rLink; while ( current != first ) { current = current->lLink; delete current->lLink; } delete first; } template <class Type> int DblList<Type>::Length ( ) const { //計算帶表頭結點的雙向循環鏈表的長度, 通過函數返回。 DblNode<Type> * p = first->rLink; int count = 0; while ( p != first ) { p = p->rLink; count++; } return count; } template <class Type> int DblList<Type>::Find ( const Type & target ) { //在帶表頭結點的雙向循環鏈表中尋找其值等于target的結點, 若找到, 則函數返回1, 同時current指針 //指向該結點, 否則函數返回0。 DblNode<Type> *p = first->rLink; while ( p != first && p->data != target ) p = p->rLink; //尋找含target的結點 if ( p != first ) { current = p; return 1; } //搜索成功, 返回1 return 0; //沒有找到, 返回0 } template <class Type> Type DblList<Type>::getData () const { if ( current == NULL ) return first->data; else return current->data; } template <class Type> int DblList<Type>::First ( ) { //若鏈表非空, 則將當前指針指向鏈表的第一個結點且函數返回1; 若鏈表空, 則令當前指針為NULL且 //函數返回0。 if ( !IsEmpty ( ) ) { current = first->rLink; return 1; } current = NULL; return 0; } template <class Type> int DblList<Type>::Next ( ) { //若當前結點有后繼結點, 則當前指針指到當前結點的后繼結點且函數返回1, 否則令當前指針為NULL //且函數返回0。 if ( current->rLink == first ) { current = NULL; return 0; } current = current->rLink; return 1; } template <class Type> int DblList<Type>::Prior ( ) { //若當前結點有前驅結點, 則當前指針指到當前結點的前驅結點且函數返回1; 否則令當前指針為NULL //且函數返回0。 if ( current->lLink == first ) { current = NULL; return 0; } current = current->lLink; return 1; } template <class Type> void DblList<Type>::Insert ( const Type & value ) { //建立一個包含有值value的新結點, 并將其插入到當前結點之后。 if ( current == NULL ) //原為空表 current = first->rLink = new DblNode<Type> ( value, first, first ); else { //原為非空表 current->rLink = new DblNode<Type> ( value, current, current->rLink ); current = current->rLink; //新結點成為當前結點 } current->rLink->lLink = current; //完成重新鏈接 } template <class Type> void DblList<Type>::Remove ( ) { //在帶表頭結點的雙向循環鏈表中刪除當前結點, 同時讓當前指針指到鏈表中的下一個結點, 若被刪結點 //是鏈表最后一個結點, 則讓當前指針指到表中最前端第一個結點。如果刪除后鏈表變成空鏈表, 則令當 //前指針為NULL。 if ( current != NULL ) { DblNode<Type> *temp = current; current = current->rLink; //當前指針指到下一結點 current->lLink = temp->lLink; //將被刪結點從鏈中摘下 temp->lLink->rLink = current; delete temp; //刪除 if ( current == first ) if ( IsEmpty ( ) ) current = NULL; //刪后鏈表變空 else current = current->rLink; } } template <class Type> void DblList<Type>::Print ( ) { if ( first->rLink == first ) cout << "It is empty" << endl; else { DblNode<Type> * temp = first; while ( temp->rLink != first ) { temp = temp->rLink; cout << temp->data << " " ; } cout << endl; } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -