#include <iostream>
using namespace std;
struct list
{
char name[100];
int year;
struct list *next;
};
typedef struct list node;
node *start=NULL;
void insert_node()
{
node *last;
if(start==NULL)
{
start = new node();
cout<<"Name: ";
cin>>start->name;
cout<<"Birth Year: ";
cin>>start->year;
}
else
{ node * temp = new node();
cout<<"Name: ";
cin>>temp->name;
cout<<"Birth Year: ";
cin>>temp->year;
temp->next= NULL;
last = start;
while(last->next!=NULL)
{
last = last->next;
}
last->next= temp;
}
}
void show()
{
node *temp=start;
cout<<"
People Over 18 years: "<<endl;
while(temp!=NULL)
{
if((2018-temp->year)>=18) ///find the age who is more then 18
{
cout<<temp->name<<endl;
}
temp = temp->next;
}
}
int main()
{
int flag=1;
while(flag==1)
{
cout<<"Press 1 to Enter New Node: ";
cin>>flag;
if(flag!=1)break;
insert_node(); ///call this function to insert new node
}
show(); ///print function
return 0;
}
Output
Press 1 to Enter New Node: 1
Name: Abir
Birth Year: 2000
Press 1 to Enter New Node: 1
Name: Ali
Birth Year: 2005
Press 1 to Enter New Node: 1
Name: Rahim
Birth Year: 1985
Press 1 to Enter New Node: 0
People Over 18 years:
Abir
Rahim
Add your comment