#include <iostream>
#include <string>
using namespace std;
struct Entry
{
string name,address,phone;
Entry *next;
};
Entry *GetNewEntry()
{
cout<<"enter name(enter to quit):";
string name;
getline(cin,name);
if (name=="") return NULL;
Entry *newOne=new Entry;
newOne->name=name;
cout<<"enter address:";
string address;
getline(cin,address);
newOne->address=address;
cout<<"enter phone:";
string phone;
getline(cin,phone);
newOne->phone=phone;
newOne->next=NULL;
return newOne;
}
Entry *BuildAddressBook()
{
Entry *listHead=NULL;
while (true)
{
Entry *newOne=GetNewEntry();
if (newOne==NULL) break;
newOne->next=listHead;
listHead=newOne;
}
return listHead;
}
void printEntry(Entry *entry)
{
cout<<entry->name<<" "<<entry->address<<" "<<entry->phone<<endl;;
}
/*void printList(Entry *list)//迭代的方式的输出实现,反序
{
for (Entry *cur=list;cur!=NULL;cur=cur->next)
printEntry(cur);
}*/
//用递归进行输出,反序
/*void printList(Entry *list)
{
if (list!=NULL)
{
printEntry(list);
printList(list->next);
}
}*/
//用递归进行输出,正序
void printList(Entry *list)
{
if (list!=NULL)
{
printList(list->next);
printEntry(list);
}
}
int main()
{
Entry *list=BuildAddressBook();
printList(list);
system("pause");
return 0;
}