?? p82.cpp
字號:
#include <stdio.h> #include <iostream.h> enum Boolean { False, True }; template <class Type> class CircList; template <class Type> class CircListNode { friend class CircList<Type>; public: CircListNode ( Type d=0, CircListNode<Type> *next=NULL ) : data (d), link (next) { } private: Type data; CircListNode<Type> *link; }; template <class Type> class CircList { public: CircList (); CircList ( Type value ); //構造函數 ~CircList ( ); //析構函數 int Length ( ) const; //計算循環鏈表長度 Boolean IsEmpty ( ) { return first->link == first; } //判表空否 Boolean Find ( const Type & value ); //在循環鏈表中尋找其值等于value的結點 Type getData ( ) { return current->data; } //返回當前結點中存放的值 Type getNextData () ; void Firster ( ) { current = first; } //將當前指針置于頭結點 void First ( ); //將當前指針指向鏈表的第一個結點 void Next ( ); //將當前指針指到當前結點的后繼結點 void Prior ( ); //將當前指針指到當前結點的前驅結點 void Insert ( const Type & value ); //插入新結點 void RemoveNext ( ); void Josephus ( int n, int m ); //刪除當前結點 private: CircListNode<Type> *first, *current, *last; //頭指針, 當前指針, 尾指針 }; template <class Type> void CircList <Type> :: CircList () { current = last = first = new CircListNode<Type> (); first->link = first; } template <class Type> void CircList <Type> :: ~CircList () { CircListNode<Type> *q; while ( first->link != first ) { q = first->link; first->link = q->link; delete q; } delete first; current = first = last = NULL; } template <class Type> void CircList <Type> :: Next () { if ( current->link == first ) current = first; current = current->link; } template <class Type> Type CircList <Type> :: getNextData() { if ( current->link == first ) return first->link->data; else return current->link->data; } template <class Type> void CircList <Type> :: RemoveNext () { CircListNode<Type> *p = current->link; if ( p == last ) last = current; if ( p != first ) current->link = p->link; else { p = first->link; first->link = p->link; } delete p; } template <class Type> void CircList <Type> :: Insert (const Type & value) { CircListNode<Type> *p = new CircListNode<Type> ( value, first ); last = last->link = p; } template <class Type> void CircList <Type> :: Josephus ( int n, int m ) { for ( int i=0; i<n-1; i++ ) { //循環n-1趟,讓n-1個人出列 for ( int j=0; j<m-1; j++ ) Next ( ); //讓current向后移動m-1次 cout << "Delete person " << getNextData ( ) << endl; RemoveNext ( ); //刪去該結點,讓current指示下一結點 } cout << "the winner is : " << getData () << endl; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -