Answer to Question #28533 in C++ for maha

Question #28533
Create a class to represent PMU student. This class should contain the following public members:

1) ID: int
2) FirstName: string
3) College: string
4) Major: string
5) GPA: double
6) Friends: Student *
7) next: Student *

Implement the following for this class:

1) Default and Overloaded constructors.
2) Write a function "void CreateList(Node * & h)" which will create a linked list of couple of students (say 5 students) and for each student this function will add a number of friends as another linked list.
3) Write a Print function to print the entire linked list as mentioned in the project file.
4) Write a function "bool FindFriendship(3, 6)" which will return true if student with id=6 is a friend of student with id=3. Otherwise it will return false.
5) Write main() where you will call CreateList(), Print() and FindFriendship() functions to check their working.

Please help me solve this question
1
Expert's answer
2013-04-17T08:38:55-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
22.04.13, 14:39

You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!

maha
21.04.13, 19:13

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS