2016-07-24 2 views
-3
#include <iostream> 
#include <fstream> 
using namespace std; 

#include "IList.h" 

int main(int argc, char* argv[]) 
{ 


//how to open a file 
ifstream inf(argv[1]); 
IList t; 
int integer; 



//reading integer from text file. 
inf >> integer; 

//Up untill the end of file, it is going to insert integer to the 
list and read integer from text file again. 

while(!inf.eof()) 
{ 
    t.insert(integer, 0); 
    inf >> integer; 
} 
//displaying the list 
t.display(cout); 




return 0; 
} 

******参照************なぜ私は後方に表示しているのですか?参考

List.h 

void display(ostream & out) const; 
//Display a list. 

//Precondition: The ostream out is open. 

//Postcondition: The list represented by this List object has been 
//inserted into out. 


void insert(ElementType item, int pos); 
//Insert a value into the list at a given position. 

//Precondition: item is the value to be inserted; there is room in 
//the array (mySize < CAPACITY); and the position satisfies 
//0 <= pos <= mySize. 

//Postcondition: item has been inserted into the list at the position 
//determined by pos (provided there is room and pos is a legal 
//position). 



List.cpp 



void IList::display(ostream & out) const 
{ 
for (int i = 0; i < mySize; i++) 
out << myArray[i] << " "; 
} 




void IList::insert(ElementType item, int pos) 
{ 
if (mySize == myCapacity) 
{ 
    cerr << "*** No space for list element -- terminating " 
      "execution ***\n"; 
    exit(1); 
} 
if (pos < 0 || pos > mySize) 
{ 
    cerr << "*** Illegal location to insert -- " << pos 
     << ". List unchanged. ***\n"; 
    return; 
} 

// First shift array elements right to make room for item 

for(int i = mySize; i > pos; i--) 
    myArray[i] = myArray[i - 1]; 

// Now insert item at position pos and increase list size 
myArray[pos] = item; 
mySize++; 
} 

、Iは表示/挿入の順序を追加しました。

表示/ IList.h

で挿入

表示/私は後方印刷または後方に挿入さIList.cpp

私にはわからないで挿入します。

私は彼らをどのようにして前進させてくれますか?あなたはいつもあなたのリストの先頭に挿入されている

+6

[ゴム製の鴨にコードを説明する](https://en.wikipedia.org/wiki/Rubber_duck_debugging)、あなたのラバーダックがこの質問に答えることができるはずです。あなたのラバーダックが休暇中の場合は、あなたのコンピュータに「デバッガ」と呼ばれる非常に便利なツールがあります。この素晴らしいツールを使用すると、一度に1行ずつコードをステップ実行し、すべての変数の内容を調べることができます。なぜ、そしてどのようにして、あなたのコードがその通りに動作するのかを理解してください。デバッガの使い方を知ることは、すべてのC++開発者にとって必要なスキルです。 –

+0

#include "IList.h"しかし、ファイル:List.hとList.cpp ... #include "List.h" – sitev

答えて

3

t.insert(integer, 0); 

は、あなたのリストは{1}になり、あなたはあなたが最初の1を挿入番号1と2を持っているとしましょう。次に、最初に2を挿入すると、あなたのリストには{2、1}が含まれます。分かりますか?

関連する問題