Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从LinkedList插入和打印_C++ - Fatal编程技术网

C++ 从LinkedList插入和打印

C++ 从LinkedList插入和打印,c++,C++,我创建了一个LinkedList,它取自一个学生班级。我在将文件中的数据插入LinkedList或检索该数据时遇到问题 这是我从中获取输入的文件: 117324 Chris Johnson Freshman 15 47 231098 Pat Smith Freshman 13 42 220982 Sally Larson Sophomore 48 168 320981 Mary Wilson Sophomore 52 149 439083 Larry Edwards Junior 82 210

我创建了一个LinkedList,它取自一个学生班级。我在将文件中的数据插入LinkedList或检索该数据时遇到问题

这是我从中获取输入的文件:

117324 Chris Johnson Freshman 15 47
231098 Pat Smith Freshman 13 42
220982 Sally Larson Sophomore 48 168
320981 Mary Wilson Sophomore 52 149
439083 Larry Edwards Junior 82 210
439871 Samuel Johnson Freshman 16 31
465132 Ashley Edwrads Senior 112 359
以下是我将数据添加到LinkedList的方式:

 int i = 1;
 while(!StudentFile.eof())
     {
       int id;
       string firstname;
       string lastname;
       string level;
       double credithours;
       double gradepoints;

       StudentFile >> id >> firstname >> lastname >> level >> credithours >> gradepoints;

       Student myStudent(id, firstname, lastname, level, credithours, gradepoints);
       myStudentList.insert(i,myStudent);

       i++;
}
LinkedList<Student> myStudentList;
这就是我试图打印数据的方式:

for(int i = 1; i <= myStudentList.getLength(); i++)
  {
    Student StuPtr = myStudentList.getEntry(i);
    cout << "Student: " << StuPtr.getID() << "; " << StuPtr.getFirstName() << " " << StuPtr.getLastName() << ", " << StuPtr.getLevel() << ", " << StuPtr.getGPA() << " GPA\n";
  }

getNodeAt
中循环的作用是什么,它只执行
curPtr->getNext()而不保留结果。非常确定应该是
curPtr=curPtr->getNext()是的,你说得对,我更改了它,但我仍然遇到同样的问题?你的
SLNode
构造函数是什么样子的?用SLNode文件更新。你的索引变量从1开始,到list->getLength()。数组和列表索引通常从0开始
#ifndef SLNODE_H
#define SLNODE_H
template <typename T>
class SLNode // A node in a singly linked list
{
private:
    T item;
    SLNode* next; // we can only go "forward" along our chain
public:

    SLNode(T anItem) : item(anItem), next(nullptr) {}
    SLNode(T anItem, SLNode* nextSLNode) : item(anItem), next(nextSLNode) {}
    T getItem() const { return item; }
    SLNode* getNext() const { return next; }
    void setItem(T newItem) { item = newItem; }
    void setNext(SLNode* nextSLNode) { next = nextSLNode; }
};
#endif
LinkedList<Student> myStudentList;
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA
Student: 117324; Chris Johnson, Freshman, 3.13333 GPA