#include<iostream>
usingnamespace std;
struct DataNode
{
string name;
int age{-99};
DataNode *next;
};
int main()
{
DataNode* head{nullptr};
DataNode* pnew{nullptr};
DataNode* pc{nullptr};
//initialize
head = new DataNode;
head -> next = nullptr;
pc = head;
//add
for(char ans= 'y' ;ans != 'n';)
{
pnew = new DataNode;
pc-> next = pnew;
cout<<"Name: ";
cin>>pnew -> name;
cout<<"Age: ";
cin>>pnew -> age;
pnew -> next = nullptr;
pc = pnew;
cout<<"\n Do you want to add record? (y/n)";
cin>>ans;
}
//display
int max_age{0};
pc = head -> next;
while (pc != nullptr)
{
if(pc->age > max_age)
max_age = pc->age;
cout << pc->name << "\t" << pc->age << endl;
pc = pc->next;
}
cout << "Maximum age: " << max_age << '\n';
return 0;
}
Name: aa
Age: 11
Do you want to add record? (y/n)y
Name: bb
Age: 7
Do you want to add record? (y/n)y
Name: cc
Age: 14
Do you want to add record? (y/n)n
aa 11
bb 7
cc 14
Maximum age: 14
Program ended with exit code: 0