我是C++新手,目前正在嘗試關於鏈接列表,並且在我的程序中顯示多個值時遇到問題。我知道問題出在指針(DisplayAll函數)的某處,但我不知道如何解決它。顯示多個值鏈接列表
node* InfoBook::AddNode(nodePtr temp)
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
InfoBook ad;
if(head != NULL)
{
current = head;
while(current -> next != NULL)
{
current = current -> next;
}
current -> next = new node;
current -> firstname = temp -> firstname;
current -> lastname = temp -> lastname;
////code here to add the other values////
current -> zipcode = temp -> zipcode;
current -> next -> next = nullptr;
return current;
ad.userPromptStatement();
}
else
{
head = new node;
head -> firstname = temp -> firstname;
head -> lastname = temp -> lastname;
////code here to add the other values////
head -> zipcode = temp -> zipcode;
head -> next = nullptr;
return current;
}
}
////////////////////////////////DisplayAll/////////////////////////////////
void InfoBook::DisplayAll()
{
current = head;
int count = 1;
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
if(current == nullptr)
{
cout << "\n\n\No Record exists.";
}
while(current != NULL)
{ ////////I know the problem is somewhere between here////////
cout << "Record # " << count << " : ";
cout << current -> firstname << endl;
cout << current -> lastname << endl;
cout << current -> phonenumber << endl;
cout << current -> dayofbirth << endl;
cout << current -> monthofbirth << endl;
cout << current -> yearofbirth << endl;
cout << current -> age << endl;
cout << current -> streetname << endl;
cout << current -> city << endl;
cout << current -> state << endl;
cout << current -> zipcode << endl;
cout <<"\n\n\n";
current = current -> next;
count++;
}
}
///////////////////////////////////////////////
////指針
InfoBook :: InfoBook() {
head = NULL;
current = NULL;
temp = NULL;
}
////////
class InfoBook
{
private:
nodePtr head;
nodePtr current;
nodePtr temp;
public:
InfoBook();
void userPromptStatement();
node* AddNode(nodePtr);
void DisplayAll();
/////////////
typedef struct node
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
static int count;
node* next;
} *nodePtr;
該程序只顯示'記錄#:'但不是值。有任何想法嗎?
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Loghorn