2016-07-24 99 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++; 
} 

对于参考,我添加显示/插入的顺序。

显示/在IList.h

插入

显示/插入IList.cpp

我不知道在哪里印刷向后或向后插入。

你能告诉我如何让他们在前进吗?

+6

如果您[将您的代码解释为您的橡皮鸭](https://en.wikipedia.org/wiki/Rubber_duck_debugging),您的橡皮鸭应该能够回答您的问题。如果你的橡皮鸭正在度假,你的电脑上有一个非常有用的工具叫做“调试器”。使用这个神奇的工具,您可以一次一行地浏览代码,并检查所有变量的内容;并理解你的代码是如何工作的,以及它是如何工作的。了解如何使用调试器是每个C++开发人员必备的技能。 –

+0

#include“IList.h”,但文件:List.h和List.cpp ...可能是#include“List.h” – sitev

回答

3

你总是插入在列表的开头:

t.insert(integer, 0); 

比方说,你有数字1和2。你先插入1,您的列表变成{1}。然后在开始处插入2,现在您的列表包含{2,1}。你看?

相关问题