我是C ++的新手,目前正在试验链接列表,但我在程序中显示多个值时遇到了麻烦。问题出在DisplayAll函数中。
struct node
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
char ch;
static int count;
int count2;
node* next;
};
class InfoBook
{
private:
node* head;
node* current;
node* temp;
public:
InfoBook();
void userPromptStatement();
node* AddNode(node*);
void DisplayAll();
//node* SearchNode();
//void sort(node*, int);
void UpdateNode();
//node* DeleteNode(node*);
};
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)
{ ////////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++;
}
}
///////////////////////////////////////////////
程序仅显示“记录#:”,但不显示值。有什么想法吗?
//////////////////added//////////////////////
node* temp = new node;
node* current;
char ch;
int count2 = 0;
ad.userPromptStatement();
while(1)
{
cin >> ch;
switch(ch)
{
case '1':
system("cls");
cout << "\n\nINSERT RECORD";
cout << "\n\nInput FIRST NAME: ";
cin.ignore();
getline(cin,firstname,'\n');
cout << "\nRecord inserted!";
current = ad.AddNode(temp);
ad.userPromptStatement();
break;
case '2':
system("cls");
cout << "\n\nDISPLAY ALL\n\n";
ad.DisplayAll();
_getch();
ad.userPromptStatement();
break;
答案 0 :(得分:0)
可能出现的问题是:
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
分配了一个新节点,但是分配到current
的字段。
另一个问题是您实际上没有初始化您在AddNode
调用中使用的节点结构中的字段。
答案 1 :(得分:0)
如果您想使用链接列表,我建议您不要重新发明轮子而是使用 的std ::名单。使用std :: list时,您仍然可以挽救大部分节点类。
#include <list>
#include <string>
using namespace std;
struct node
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
char ch;
};
typedef std::list<node> NodeList;
class InfoBook
{
NodeList nList;
public:
InfoBook();
void userPromptStatement();
void AddNode(node& theNode) { nList.push_back(theNode); }
void DisplayAll();
};
void InfoBook::DisplayAll()
{
NodeList::iterator it = nList.begin();
while (it != nList.end())
{
node& curNode = *it;
// now print out curNode in whatever way you see fit
// ...
++it; // go to nest node in the list
}
}
注意代码的缩短程度,尤其是AddNode()函数。节点类不再需要保留事物的数量,因为nList.size()将为您提供节点数。
上面的示例向您介绍了标准库容器(std :: list)和迭代器(DisplayAll函数中的“it”)。我没有填写其他InfoBook功能,因为我会留给你做这个,但希望你能得到关于如何完成的想法。