#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
class Student
{
public:
int ID;
string FirstName;
string College;
string Major;
double GPA;
Student* Friends;
Student* next;
Student();
Student(int setID, string setFirstName, string setCollege, string setMajor, double setGPA);
};
Student::Student()
{
ID=0;
FirstName="";
College="";
Major="";
GPA=0.0;
Friends=NULL;
next=NULL;
}
Student::Student(int setID, string setFirstName, string setCollege, string setMajor, double setGPA)
{
ID=setID;
FirstName=setFirstName;
College=setCollege;
Major=setMajor;
GPA=setGPA;
Friends=NULL;
next=NULL;
}
void CreateList(Student *&h)
{
Student* head;
for(int i=0;i<5;i++)
{
int setID, n;
string setFirstName, setCollege, setMajor;
double setGPA;
cout<<"Enter the data about student:"<<endl;
cout<<"ID: ";
cin>>setID;
cout<<"First Name: ";
cin>>setFirstName;
cout<<"College: ";
cin>>setCollege;
cout<<"Major: ";
cin>>setMajor;
cout<<"GPA: ";
cin>>setGPA;
if(i==0)
{
h=new Student(setID,setFirstName,setCollege,setMajor,setGPA);
head=h;
} else
{
Student* x=new Student(setID,setFirstName,setCollege,setMajor,setGPA);
h->next=x;
h=h->next;
}
cout<<"Enter the amount of the friends:"<<endl;
cin>>n;
if(n!=0)
{
Student *pos=h;
cout<<"Enter the data about friends:"<<endl;
for(int j=0;j<n;j++)
{
cout<<"Friend "<<j+1<<":"<<endl;
cout<<"ID: ";
cin>>setID;
cout<<"First Name: ";
cin>>setFirstName;
cout<<"College: ";
cin>>setCollege;
cout<<"Major: ";
cin>>setMajor;
cout<<"GPA: ";
cin>>setGPA;
Student* x=new Student(setID,setFirstName,setCollege,setMajor,setGPA);
pos->Friends=x;
pos=pos->Friends;
}
}
cout<<endl;
}
h=head;
}
void OutputData(Student *p)
{
cout<<"ID: "<<p->ID<<endl;
cout<<"First Name: "<<p->FirstName<<endl;
cout<<"College: "<<p->College<<endl;
cout<<"Major: "<<p->Major<<endl;
cout<<"GPA: "<<p->GPA<<endl;
}
void Print(Student *head)
{
cout<<"There are 5 students."<<endl;
Student *p=head;
for(int i=0;i<5;i++)
{
cout<<"Student's "<<i+1<<" data is following:"<<endl;
OutputData(p);
if(p->Friends==NULL) cout<<"This student has got no friends."<<endl; else
{
cout<<"This student has got the following friends:"<<endl;
Student *l=p->Friends;
int j=0;
while(l!=NULL)
{
j++;
cout<<"Friend "<<j<<":"<<endl;
OutputData(l);
l=l->Friends;
}
}
cout<<endl;
p=p->next;
}
}
bool FindMembership(Student *head, int i, int j)
{
Student* p=head;
while(p!=NULL && p->ID!=i) p=p->next;
if(p==NULL)
{
p=head;
return false;
}
p=p->Friends;
while(p!=NULL && p->ID!=j) p=p->Friends;
if(p==NULL) return false; else
return true;
}
Student* StudentList;
int main()
{
CreateList(StudentList);
cout<<endl;
Print(StudentList);
int i,j;
cout<<"Input 2 ID's to check friendship:"<<endl;
cin>>i>>j;
if(FindMembership(StudentList,i,j)) cout<<"These students are friends!"<<endl; else
cout<<"These students are not friends."<<endl;
system("pause");
return 0;
}
Comments
You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!
Thank you for your reply. It worked and did help me alot. I am now working on it and trying to understand it thoroughly. Thank you once again
Leave a comment