1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdexcept>
#include <sstream>
#include <limits> 

 
class Term 
{
   friend class LinkedList;
   friend class ListIterator;
    
   private: int exp, coef; 
   private: Term *next; 
              
   private: Term( int e, int c )
   : exp( e ), coef( c ), next( 0 ) 
   {  }
   
   private: Term( Term const &term )
   : exp( term.exp ), coef( term.coef ), next( 0 )
   {  }
   
   private: ~Term()
   { delete next; }
   
   public: int Exponent() const 
   { return exp; }
   
   public: int Coefficient() const 
   { return coef; }
   
   public: std::string toString() const
   {
      std::ostringstream oss;
      oss << '(' << exp << ',' << coef << ')'; 
      return oss.str();        
   } 
};

class ListIterator
{
   friend class LinkedList;
        
   private: Term *current;
   
   public: ListIterator()
   : current( 0 )
   {  }
   
   public: ~ListIterator()
   {  }
   
   private: ListIterator( Term *p )
   : current( p )
   {  }
  
   public: ListIterator( ListIterator const &it )
   : current( it.current )
   {  }
   
   public: Term const & operator =( ListIterator const &it )
   {
      current = it.current;        
   }
   
   public: Term const & next()
   {  
      return *(current -> next);
   }
   
   public: void forward( )
   {
      current = current -> next;        
   }
   
   public: bool hasNext()
   {
      return current -> next != 0;        
   }
};

class LinkedList
{
   private: Term *head; 
   private: Term *tail;         
   
   // cotr insert a dunmmy node 
   public: LinkedList()
   : head( new Term( std::numeric_limits<int>::max(),
                     std::numeric_limits<int>::max() ) ) 
   {
      tail = head;         
   }
   
   public: LinkedList( LinkedList const &otherList )
   : head( new Term( std::numeric_limits<int>::max(),
                     std::numeric_limits<int>::max() ) ) 
   {
      tail = head;
      
      for ( Term *p = otherList.head -> next; p != 0; p = p -> next )
         add( p -> coef, p -> exp );        
   }
   
   public: ~LinkedList() 
   { delete head; }
   
   public: void add( int exp, int coef )
   {
      tail->next = new Term( exp, coef );
      tail = tail -> next; 
   }
   
   public: std::string toString()
   {
      std::string retString; 
           
      for ( Term *p = head -> next; p != 0; p = p -> next )
         retString += "->" + p -> toString(); 
      
      retString += "->nil";
       
      return retString;        
   }
   
   public: ListIterator listIterator()
   {
      return ListIterator( head );        
   }
};


int main()
{
    using std::cout; 
    using std::endl; 
    using std::ifstream;
    
    LinkedList list; // 串列物件 
    
    /* 從檔案中讀取資料, 存入 LinkedList 物件中 
     * 檔案內容 : 
     * 4
     * 3 1000
     * 7 999
     * -10 998
     * 80 500 
     */ 
    int termCount, // 項數
        exp, coef; // 指數, 係數
         
    ifstream ifs("test.txt");
    
    ifs >> termCount; // 項數 
    for ( ; termCount > 0; --termCount ) 
    { 
       ifs >> exp >> coef; 
       list.add( exp, coef ); // 新增項 
    }    
    ifs.close();
    
    /* 將 LinkedList 物件裡的內容印出(唯讀) 
     * 第一種 : 用迭代器取得每一個節點, 並用 get 函式印出 
     * 第二種 : 以固定的字串型式印出 
     */ 
    ListIterator it = list.listIterator();
    
    while( it.hasNext() )
    { 
       cout << it.next().Exponent() << ',' << it.next().Coefficient() << endl;
       it.forward(); 
    }

    cout << list.toString() << endl;
    
    
    system("pause");   
}