Advertisement
uopspop

Untitled

May 11th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T> class Chain;
  5.  
  6. template <class T>
  7. class ChainNode{
  8.     friend class Chain <T>;
  9.     public:
  10.         ChainNode(T element, ChainNode<T>* next = NULL);
  11.     private:
  12.         T data;
  13.         ChainNode<T>* link;
  14. };
  15.  
  16. template <class T>
  17. class Chain{
  18.     public:
  19.         Chain(): first(NULL){}  //empty constructor
  20.         void insertAtBack(const T & value);
  21.         void insertAtFront(const T & value);
  22.         void deletefromBack();
  23.         void deletefromFront();
  24.         void printAll();
  25.         T getFisrtValue(){
  26.             return first->data;
  27.         }
  28.         class ChainIterator{
  29.             public:
  30.                 // constructor
  31.                 ChainIterator(ChainNode<T>* startNode = 0){
  32.                     current = startNode;
  33.                 }
  34.                 // dereference
  35.                 T& operator*()const
  36.                     {return current->data;} // 取值,再取址
  37.                 T* operator->()const
  38.                     {return &current->data;} // 取址,再取值
  39.                 T getValue(){
  40.                     return current->data;
  41.                 }
  42.                
  43.                 // equality verifying
  44.                 bool operator!=(const ChainIterator right)const
  45.                     { return current != right.current; }
  46.                 bool operator==(const ChainIterator right)const
  47.                     { return current == right.current; }
  48.  
  49.                 // adding up
  50.                 ChainIterator operator++(){ // 放前面的
  51.                     current = current->link;
  52.                     return current; //回傳加之後的值
  53.                 }
  54.                 ChainIterator operator++(int){ // 放後面的
  55.                     ChainIterator old = *this;
  56.                     current = current-> link;
  57.                     return old; //回傳加之前的值
  58.                 }
  59.                    
  60.             private:
  61.                 ChainNode<T>* current; 
  62.         };
  63.         ChainIterator begin(){
  64.             return ChainIterator(first);
  65.         }
  66.         ChainIterator end(){
  67.             return ChainIterator(0);
  68.         }      
  69.         ~Chain();
  70.     private:
  71.         ChainNode<T>* first;
  72. };
  73.  
  74. int main(){
  75.     Chain<int> myChainInt;
  76.     myChainInt.insertAtBack(10);
  77.     myChainInt.insertAtBack(20);
  78.     myChainInt.insertAtBack(20);
  79.     myChainInt.insertAtBack(30);
  80.     myChainInt.insertAtBack(40);
  81.    
  82.     cout << myChainInt.getFisrtValue() << endl;
  83.    
  84.     Chain<int>::ChainIterator it = myChainInt.begin();
  85.     while (it != myChainInt.end()){
  86.         //int tmpValue = *it;
  87.         cout << it.getValue() << " ";
  88.         it++;
  89.     }  
  90.     cout << endl;
  91.  
  92.     it = myChainInt.begin();
  93.     while (it != myChainInt.end()){
  94.         cout << *it << " ";
  95.         it++;
  96.     }  
  97.     cout << endl;
  98.  
  99.    
  100. }
  101.  
  102. template <class T>
  103. ChainNode<T>::ChainNode( T element, ChainNode<T>* next): data(0), link(NULL)
  104. {
  105.     data = element;
  106.     link = next;
  107. }  
  108.  
  109. template <class T>
  110. void Chain<T>::insertAtBack(const T& value){
  111.     if(first){
  112.         ChainNode<T>* tmpPtr = new ChainNode<T>(value,0);      
  113.         ChainNode<T>* last = first;
  114.         while(last->link != NULL)
  115.             last = last->link;
  116.         last->link = tmpPtr;
  117.     }else{
  118.         first  = new ChainNode<T>(value,0);
  119.     }
  120. }
  121. template <class T>
  122. void Chain<T>::insertAtFront(const T& value){
  123.     ChainNode<T>* tmpPtr = new ChainNode<T>(value,0);
  124.     if(first){
  125.         tmpPtr->link = first;
  126.         first = tmpPtr;
  127.     }else{
  128.         first  = tmpPtr;
  129.     }
  130. }
  131. template <class T>
  132. void Chain<T>::deletefromBack(){
  133.     T value;
  134.     // get the mry location of the last node
  135.     if (first){            
  136.         ChainNode<T>* last = first;
  137.         while(last->link != NULL){
  138.             last = last->link;
  139.         }// end while
  140.    
  141.    
  142.         if(first == last){
  143.             value = last->data;
  144.             delete last;
  145.             first = NULL;
  146.             cout << value << " is removed" << " and nothing remained"<< endl;
  147.         }else{
  148.             ChainNode<T>* previous = first;
  149.             while(previous->link != last){
  150.                 previous = previous->link;             
  151.             }
  152.                            
  153.             previous->link = NULL;
  154.             value = last->data;
  155.             delete last;
  156.             cout << value << " is removed" << endl;
  157.         }// end inner if
  158.     }else{
  159.         cout << "remove unsuccessful: no nodes remained" << endl;
  160.     }// end if
  161. }
  162. template <class T>
  163. void Chain<T>::deletefromFront(){
  164.     T value;
  165.     // get the mry location of the last node
  166.    
  167.     if(first){             
  168.         ChainNode<T>* tmpPtr = first;
  169.         first = first->link;
  170.         value = tmpPtr->data;
  171.         delete tmpPtr;
  172.         cout << value << " is removed" << endl;
  173.     }else{
  174.         cout << "remove unsuccessful: no nodes remained" << endl;
  175.     }
  176. }  
  177. template <class T> 
  178. void Chain<T>::printAll(){
  179.     if(first != NULL){
  180.         ChainNode<T>* tmpPtr = first;
  181.         while(tmpPtr != NULL){
  182.             cout << tmpPtr->data << " ";
  183.             tmpPtr = tmpPtr->link;
  184.         }// end while          
  185.         cout << endl;
  186.     }else{
  187.         cout << "nothing" << endl;
  188.     }// end if
  189. }
  190. template <class T>
  191. Chain<T>::~Chain(){
  192.    
  193.     ChainNode<T>* tmpPtr = first;
  194.     while(tmpPtr != NULL){
  195.         first = first->link;
  196.         delete tmpPtr;
  197.         tmpPtr = first;
  198.     }// end while
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement