#include "PersonList.h"
#include <iostream>
#include <string>
using namespace std;
PersonList::PersonList()
{
head=NULL; //Head is a PersonRec*
}
struct PersonRec
{
string aName;
int aBribe;
PersonRec* link;
};
void PersonList::AddToList()
{
//string a;
//int b;
PersonRec* p;
p=new PersonRec;
p->link=NULL;
cout << "\nEnter the person's name: ";
cin >> p->aName;
cout<< "\nEnter the person's contribution: ";
cin >> p->aBribe;
if(head==NULL)
{
cout<<1<<endl;
head=p;
}
else if(head!=NULL) //The problem is in here.
{
PersonRec *currPtr=head;
bool x=true;
while(x==true)
{
currPtr=currPtr->link;
if(currPtr==NULL)
{
currPtr=p;
x=false;
}
}
}
}
これは名前と賄賂を連結リストに動的メモリ割り当てで入力し、要求に応じて結果を出力することになっているプログラムです (入力関数だけが問題があるため、ここでは入力関数のみを配置しました)。最初の要素は正常に入力および出力されますが、2 番目の要素を入力しようとすると出力されません。プログラムはコンパイルされますが、ノードの追加は最初のノード以降のすべてのノードで異なるため、問題は私が問題としてコメントした部分にあるはずです。どんな助けでも大歓迎です。はい、これは宿題なので、ヒントをいただければ幸いです。