SlideShare a Scribd company logo
1 of 11
Download to read offline
Write a program that creates a linked list of families. Each family contains a husband, his wife
and a set of children. Each family is linked to another by a pointer called nextFamily. Husband is
linked to his wife by a pointer called myWife. The children are linked to their mother through a
pointer called myChildren. Children are linked by a pointer called mySibling.
Therefore, there are three types of nodes. The Husband node (class) contains the following
information:
SSN of type long
firstName of type string
lastName of type string
nextFamily of Husband*
myWife of type Wife*
The Wife class contains the following information:
SSN of type long
firstName of type string
lastName of type string
myChildren of Child*
The Child class contains the following information:
SSN of type long
firstName of type string
lastName of type string
mySibling of Child*
Your program reads a set of commands from the transaction file and acts accordingly. The
commands in the transaction file include
AddHusband
RemoveHusband
AddWife
RemoveWife
AddAChild
RemoveAChild
PrintAFamily
PrintAllFamilies
AddHusbad comes with three more information: his SSN, his first name and his last name. When
you get this command, you need to create a node of type Husband and insert it to the top of the
Link list of the families
RemveHusband comes with one more information: the SSN. This node should be deleted. In
order to delete this node, first you need to remove the children (if any), then remove the wife (if
he is married) and then remove the node itself
AddWife comes with 4 more information, the SSN of the wife, her first name, her last name and
finally the SSN of her husband. Based on the SSN of her husband, her husband must be found in
the linked list and the wife node should be linked to it.
RemoveWife, comes with the SSN of her husband. In this case, the husband should be searched
and his wife node should be deleted. If the wife node is attached to the children, first remove all
the children and then remove the wide node
AddAchild comes with 4 more information, the SSN of the child, child first name, child last
name and finally the SSN of the father. Based on the SSN of the father, the father must be found
in the linked list and the child should be attached to the mother.
RemoveAchild, comes with the SSN of the father and SSN of the child. In this case, the father
should be searched and his wife node should be followed to find the child that should be deleted.
If you find the child, you need to remove it from the family
PrintAFamily comes with the SSN of the father. You need to search that family in the linked list
and print the information of the father. The mother (if any) and the children (if any) on the screen
PrintAllFamilies, goes through the entire linked list of the family and prints the information of
each family one by one.
Test your program with the following transaction file
AddHusband 100100100 Jim Smith
AddHusband 200200200 Joe Brown
AddHusband 300300300 Kevin Tarr
AddHusband 400400400 Richard Anderson
AddHusband 500500500 Ken Baker
PrintAllFamilies
AddWife 210210210 Linda Brown 200200200
AddWife 410410410 Mary Anderson 400400400
AddWife 510510510 Beth Baker 500500500
AddWife 310310310 Ana Tarr 333333333
PrintAllFamilies
AddAchild 211211211 Tina Brown 200200200
AddAchild 212212212 Rick Brown 200200200
AddAchild 211211211 Nina Brown 200200200
AddAchild 511511511 Sam Baker 500500500
AddAchild 512512512 Serge Baker 500500500
RemoveAchild 200200200 211211211
RemoveAchild 200200200 311311311
PrintAFamily 333333333
PrintAFamily 400400400
PrintAFamily 200200200
PrintAllFamilies
You can create a class called Family with the following properties
class Family
{
protected:
top Husband*;
public:
void AddHusband(long SSN, string Fname, string Lname);
void RemoveHusband(long SSN);
void AddWife(long SSN, string Fname, string Lname, long husbandSSN);
void RemoveWife(long husbandSSN);
void AddAChild(long SSN, string Fname, string Lname, long fatherSSN);
void RemoveAChild(long FatherSSN, long childSSN);
void PrintAFamily(long fatherSSN);
void PrintAllFamilies();
void ReadTransactionFile();
}
You can write the following in your main program
int main()
{
Family USAFamilies;
USAFamilies. ReadTransactionFile( );
return 0;
}
You must create a class called Person and make the Husband, Wife and Child classes to inherit
from it. The class Person can include SSN, first name and last name which arte common to all
three classes.
Solution
#include
#include
#include
using namespace std;
// create a class Family
class Family;
// create a class Husband
class Husband;
typedef Husband* Hsbndptr;
// create a class Wife
class Wife;
typedef Wife* Wifeptr;
// create a class Child
class Child;
typedef Child* Childptr;
// create a class Person
class Person
{
protected:
long SSN;
string Fname;
string Lname;
};
//------------------------------------------------------------------------------------------------
class Husband: public Person
{
friend class Family;
protected:
Hsbndptr nextFamily;
Wifeptr myWife;
public:
Husband(){SSN = 0; Fname = Lname = ""; nextFamily = NULL; myWife = NULL;}
Husband(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;}
};
//------------------------------------------------------------------------------------------------
class Wife: public Person
{
friend class Family;
protected:
Childptr myChildren;
public:
Wife(){SSN = 0; Fname = Lname = ""; myChildren = NULL;}
Wife(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;}
};
//------------------------------------------------------------------------------------------------
class Child: public Person
{
friend class Family;
protected:
Childptr mySibling;
public:
Child(){SSN = 0; Fname = Lname = ""; mySibling = NULL;}
Child(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;}
};
//------------------------------------------------------------------------------------------------
class Family
{
protected:
Hsbndptr top;
static bool failure;
static bool success;
public:
Family(){top = NULL;}
~Family(){destroy();}
bool AddHusband(long id, string Fn, string Ln);
bool RemoveHusband(long id);
bool AddWife(long id, string Fn, string Ln, long husbandSSN);
bool RemoveWife(long husbandSSN);
bool AddAChild(long id, string Fn, string Ln, long fatherSSN);
bool RemoveAChild(long fatherSSN, long childSSN);
bool PrintAFamily(long fatherSSN);
bool PrintAllFamilies();
void ReadTransactionFile();
bool SearchForHusband(long husbandSSN);
void destroy();
};
//------------------------------------------------------------------------------------------------
bool Family::failure = false;
bool Family::success = true;
//------------------------------------------------------------------------------------------------
bool Family::AddAChild(long id, string Fn, string Ln, long fatherSSN)
{
Hsbndptr H = top;
Childptr LSib;
if(SearchForHusband(fatherSSN) == failure)
{
cout << "The child " << Fn << " could not be created because there is no father with the SSN
" << fatherSSN << endl;
return failure;
}
else if(H -> myWife == NULL)
{
cout << "The child " << Fn << " could not be added because the father does not have a wife"
<< endl;
return failure;
}
else
{
while(fatherSSN != H -> SSN)
H = H -> nextFamily;
if(H -> myWife -> myChildren == NULL)
{
Childptr C = new Child(id,Fn, Ln);
H -> myWife -> myChildren = C;
}
else if(H -> myWife -> myChildren -> mySibling == NULL)
{
if(id == H -> myWife -> myChildren -> SSN)
{
cout << "The child " << Fn << " could not be added because a child with the SSN " << id <<
" already exists" << endl;
return failure;
}
else
{
LSib = LSib -> mySibling;
}
Childptr C = new Child(id, Fn, Ln);
H -> myWife -> myChildren -> mySibling = C;
return success;
}
else
{
if(id == H -> myWife -> myChildren -> SSN)
{
cout << "The child " << Fn << " could not be added because a child with the SSN " << id <<
" already exist" << endl << endl;
}
}
}
}
//------------------------------------------------------------------------------------------------
bool Family::RemoveAChild(long fatherSSN, long childSSN)
{
Hsbndptr H = top;
Childptr C;
Childptr Sib;
if(SearchForHusband(fatherSSN) == failure)
{
cout << "The child " << childSSN << " could not be removed because the father does not
exist" << endl;
return failure;
}
else
{
while(fatherSSN != H -> SSN)
H = H -> nextFamily;
if(H -> myWife == NULL)
{
cout << "The child " << childSSN << " could not be removed because the mother does not
exist" << endl;
return failure;
}
else if(H -> myWife -> myChildren == NULL)
{
cout << "The child could not be removed because the child does not exist" << endl << endl;
return failure;
}
else if(H -> myWife -> myChildren -> mySibling == NULL)
{
if(childSSN != H -> myWife -> myChildren -> SSN)
{
cout << "The child could not be removed because the child does not exist" << endl << endl;
return failure;
}
C = H -> myWife -> myChildren;
H -> myWife -> myChildren = NULL;
delete C;
return success;
}
else
{
C = H -> myWife -> myChildren;
Sib = H -> myWife -> myChildren -> mySibling;
if(childSSN == C -> SSN)
{
H -> myWife -> myChildren == Sib;
delete C;
return success;
}
else
{
H -> myWife -> myChildren -> mySibling = NULL;
delete Sib;
return success;
}
}
}
}
//------------------------------------------------------------------------------------------------
void Family::ReadTransactionFile()
{
string cmd;
long id;
string Fn;
string Ln;
long hId; // stands for husbandId
fstream fin;
fin.open("data.txt");
if(!fin)
cout << "The file does not exist." << endl << endl;
else
{
while(!fin.eof())
{
fin >> cmd;
if(cmd == "AddHusband")
{
fin >> id >> Fn >> Ln;
AddHusband(id, Fn, Ln);
}
else if(cmd == "RemoveHusband")
{
fin >> id;
RemoveHusband(id);
}
else if(cmd == "AddWife")
{
fin >> id >> Fn >> Ln >> hId;
AddWife(id, Fn, Ln, hId);
}
else if(cmd == "RemoveWife")
{
fin >> id;
RemoveWife(id);
}
else if(cmd == "AddAChild")
{
fin >> id >> Fn >> Ln >> hId;
AddAChild(id, Fn, Ln, hId);
}
else if(cmd == "RemoveAChild")
{
fin >> hId >> id;
RemoveAChild(hId, id);
}
else if(cmd == "PrintAFamily")
{
fin >> id;
PrintAFamily(id);
}
else if(cmd == "PrintAllFamilies")
PrintAllFamilies();
else
cout << "The command " << cmd << " does not exist." << endl;
}
}
}
//------------------------------------------------------------------------------------------------
bool Family::SearchForHusband(long husbandSSN)
{
Hsbndptr H = top;
while(H != NULL)
{
if(husbandSSN == H -> SSN)
return success;
else
H = H -> nextFamily;
}
return failure;
}
//------------------------------------------------------------------------------------------------
int main()
{
Family USAFamilies;
USAFamilies.ReadTransactionFile();
return 0;
}

More Related Content

More from info998421

Describe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdfDescribe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdf
info998421
 
Describe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdfDescribe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdf
info998421
 
Year d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdfYear d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdf
info998421
 
Write a C program that converts an NFA file to a DFA, where the lang.pdf
Write a C program that converts an NFA file to a DFA, where the lang.pdfWrite a C program that converts an NFA file to a DFA, where the lang.pdf
Write a C program that converts an NFA file to a DFA, where the lang.pdf
info998421
 
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
Which of these hypotheses cannot be tested by an experiment  Biologi.pdfWhich of these hypotheses cannot be tested by an experiment  Biologi.pdf
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
info998421
 
Which is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdfWhich is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdf
info998421
 
What happens at the mid-blastula transition in Xenopus Which part o.pdf
What happens at the mid-blastula transition in Xenopus  Which part o.pdfWhat happens at the mid-blastula transition in Xenopus  Which part o.pdf
What happens at the mid-blastula transition in Xenopus Which part o.pdf
info998421
 
C++ Help, I cant find whats wrong in my program. The sample outp.pdf
C++ Help, I cant find whats wrong in my program. The sample outp.pdfC++ Help, I cant find whats wrong in my program. The sample outp.pdf
C++ Help, I cant find whats wrong in my program. The sample outp.pdf
info998421
 
The many organisms designated as protists are closely related to bac.pdf
The many organisms designated as protists are  closely related to bac.pdfThe many organisms designated as protists are  closely related to bac.pdf
The many organisms designated as protists are closely related to bac.pdf
info998421
 

More from info998421 (20)

do it on excel and please kindly upload the excel file. or explain m.pdf
do it on excel and please kindly upload the excel file. or explain m.pdfdo it on excel and please kindly upload the excel file. or explain m.pdf
do it on excel and please kindly upload the excel file. or explain m.pdf
 
Describe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdfDescribe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdf
 
Describe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdfDescribe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdf
 
C. wye D. delta E. not defined Question 6 What is the configuration o.pdf
C. wye D. delta E. not defined Question 6 What is the configuration o.pdfC. wye D. delta E. not defined Question 6 What is the configuration o.pdf
C. wye D. delta E. not defined Question 6 What is the configuration o.pdf
 
A bullet that lodges in the heart would a. be located in the ventra.pdf
A bullet that lodges in the heart would  a. be located in the ventra.pdfA bullet that lodges in the heart would  a. be located in the ventra.pdf
A bullet that lodges in the heart would a. be located in the ventra.pdf
 
Year d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdfYear d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdf
 
Why do Radicals maintain capitalism will always tend to enter into c.pdf
Why do Radicals maintain capitalism will always tend to enter into c.pdfWhy do Radicals maintain capitalism will always tend to enter into c.pdf
Why do Radicals maintain capitalism will always tend to enter into c.pdf
 
Write a C program that converts an NFA file to a DFA, where the lang.pdf
Write a C program that converts an NFA file to a DFA, where the lang.pdfWrite a C program that converts an NFA file to a DFA, where the lang.pdf
Write a C program that converts an NFA file to a DFA, where the lang.pdf
 
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
Which of these hypotheses cannot be tested by an experiment  Biologi.pdfWhich of these hypotheses cannot be tested by an experiment  Biologi.pdf
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
 
Which is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdfWhich is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdf
 
Which of the following is a good example of homoplasy (convergent ev.pdf
Which of the following is a good example of homoplasy (convergent ev.pdfWhich of the following is a good example of homoplasy (convergent ev.pdf
Which of the following is a good example of homoplasy (convergent ev.pdf
 
What happens at the mid-blastula transition in Xenopus Which part o.pdf
What happens at the mid-blastula transition in Xenopus  Which part o.pdfWhat happens at the mid-blastula transition in Xenopus  Which part o.pdf
What happens at the mid-blastula transition in Xenopus Which part o.pdf
 
What is an example of Mendel’s first hypothesis regarding patterns o.pdf
What is an example of Mendel’s first hypothesis regarding patterns o.pdfWhat is an example of Mendel’s first hypothesis regarding patterns o.pdf
What is an example of Mendel’s first hypothesis regarding patterns o.pdf
 
What are three concepts that characterize the components of a system.pdf
What are three concepts that characterize the components of a system.pdfWhat are three concepts that characterize the components of a system.pdf
What are three concepts that characterize the components of a system.pdf
 
True or False Explain your answer. If 2 , then cos 2 0..pdf
True or False Explain your answer. If 2    , then cos 2  0..pdfTrue or False Explain your answer. If 2    , then cos 2  0..pdf
True or False Explain your answer. If 2 , then cos 2 0..pdf
 
C++ Help, I cant find whats wrong in my program. The sample outp.pdf
C++ Help, I cant find whats wrong in my program. The sample outp.pdfC++ Help, I cant find whats wrong in my program. The sample outp.pdf
C++ Help, I cant find whats wrong in my program. The sample outp.pdf
 
Based on the sections Multicellularity in the Volvocine Algae and.pdf
Based on the sections Multicellularity in the Volvocine Algae and.pdfBased on the sections Multicellularity in the Volvocine Algae and.pdf
Based on the sections Multicellularity in the Volvocine Algae and.pdf
 
The parallel axis theorum... can only be used to find the moment of .pdf
The parallel axis theorum...  can only be used to find the moment of .pdfThe parallel axis theorum...  can only be used to find the moment of .pdf
The parallel axis theorum... can only be used to find the moment of .pdf
 
The many organisms designated as protists are closely related to bac.pdf
The many organisms designated as protists are  closely related to bac.pdfThe many organisms designated as protists are  closely related to bac.pdf
The many organisms designated as protists are closely related to bac.pdf
 
The following table gives the average monthly exchange rate between t.pdf
The following table gives the average monthly exchange rate between t.pdfThe following table gives the average monthly exchange rate between t.pdf
The following table gives the average monthly exchange rate between t.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

Write a program that creates a linked list of families. Each family .pdf

  • 1. Write a program that creates a linked list of families. Each family contains a husband, his wife and a set of children. Each family is linked to another by a pointer called nextFamily. Husband is linked to his wife by a pointer called myWife. The children are linked to their mother through a pointer called myChildren. Children are linked by a pointer called mySibling. Therefore, there are three types of nodes. The Husband node (class) contains the following information: SSN of type long firstName of type string lastName of type string nextFamily of Husband* myWife of type Wife* The Wife class contains the following information: SSN of type long firstName of type string lastName of type string myChildren of Child* The Child class contains the following information: SSN of type long firstName of type string lastName of type string mySibling of Child* Your program reads a set of commands from the transaction file and acts accordingly. The commands in the transaction file include AddHusband RemoveHusband AddWife RemoveWife AddAChild RemoveAChild PrintAFamily PrintAllFamilies AddHusbad comes with three more information: his SSN, his first name and his last name. When you get this command, you need to create a node of type Husband and insert it to the top of the Link list of the families RemveHusband comes with one more information: the SSN. This node should be deleted. In
  • 2. order to delete this node, first you need to remove the children (if any), then remove the wife (if he is married) and then remove the node itself AddWife comes with 4 more information, the SSN of the wife, her first name, her last name and finally the SSN of her husband. Based on the SSN of her husband, her husband must be found in the linked list and the wife node should be linked to it. RemoveWife, comes with the SSN of her husband. In this case, the husband should be searched and his wife node should be deleted. If the wife node is attached to the children, first remove all the children and then remove the wide node AddAchild comes with 4 more information, the SSN of the child, child first name, child last name and finally the SSN of the father. Based on the SSN of the father, the father must be found in the linked list and the child should be attached to the mother. RemoveAchild, comes with the SSN of the father and SSN of the child. In this case, the father should be searched and his wife node should be followed to find the child that should be deleted. If you find the child, you need to remove it from the family PrintAFamily comes with the SSN of the father. You need to search that family in the linked list and print the information of the father. The mother (if any) and the children (if any) on the screen PrintAllFamilies, goes through the entire linked list of the family and prints the information of each family one by one. Test your program with the following transaction file AddHusband 100100100 Jim Smith AddHusband 200200200 Joe Brown AddHusband 300300300 Kevin Tarr AddHusband 400400400 Richard Anderson AddHusband 500500500 Ken Baker PrintAllFamilies AddWife 210210210 Linda Brown 200200200 AddWife 410410410 Mary Anderson 400400400 AddWife 510510510 Beth Baker 500500500 AddWife 310310310 Ana Tarr 333333333 PrintAllFamilies AddAchild 211211211 Tina Brown 200200200 AddAchild 212212212 Rick Brown 200200200 AddAchild 211211211 Nina Brown 200200200 AddAchild 511511511 Sam Baker 500500500 AddAchild 512512512 Serge Baker 500500500 RemoveAchild 200200200 211211211
  • 3. RemoveAchild 200200200 311311311 PrintAFamily 333333333 PrintAFamily 400400400 PrintAFamily 200200200 PrintAllFamilies You can create a class called Family with the following properties class Family { protected: top Husband*; public: void AddHusband(long SSN, string Fname, string Lname); void RemoveHusband(long SSN); void AddWife(long SSN, string Fname, string Lname, long husbandSSN); void RemoveWife(long husbandSSN); void AddAChild(long SSN, string Fname, string Lname, long fatherSSN); void RemoveAChild(long FatherSSN, long childSSN); void PrintAFamily(long fatherSSN); void PrintAllFamilies(); void ReadTransactionFile(); } You can write the following in your main program int main() { Family USAFamilies; USAFamilies. ReadTransactionFile( ); return 0; } You must create a class called Person and make the Husband, Wife and Child classes to inherit from it. The class Person can include SSN, first name and last name which arte common to all three classes. Solution #include #include
  • 4. #include using namespace std; // create a class Family class Family; // create a class Husband class Husband; typedef Husband* Hsbndptr; // create a class Wife class Wife; typedef Wife* Wifeptr; // create a class Child class Child; typedef Child* Childptr; // create a class Person class Person { protected: long SSN; string Fname; string Lname; }; //------------------------------------------------------------------------------------------------ class Husband: public Person { friend class Family; protected: Hsbndptr nextFamily; Wifeptr myWife; public: Husband(){SSN = 0; Fname = Lname = ""; nextFamily = NULL; myWife = NULL;} Husband(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;} }; //------------------------------------------------------------------------------------------------ class Wife: public Person { friend class Family;
  • 5. protected: Childptr myChildren; public: Wife(){SSN = 0; Fname = Lname = ""; myChildren = NULL;} Wife(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;} }; //------------------------------------------------------------------------------------------------ class Child: public Person { friend class Family; protected: Childptr mySibling; public: Child(){SSN = 0; Fname = Lname = ""; mySibling = NULL;} Child(long id, string Fn, string Ln){SSN = id; Fname = Fn; Lname = Ln;} }; //------------------------------------------------------------------------------------------------ class Family { protected: Hsbndptr top; static bool failure; static bool success; public: Family(){top = NULL;} ~Family(){destroy();} bool AddHusband(long id, string Fn, string Ln); bool RemoveHusband(long id); bool AddWife(long id, string Fn, string Ln, long husbandSSN); bool RemoveWife(long husbandSSN); bool AddAChild(long id, string Fn, string Ln, long fatherSSN); bool RemoveAChild(long fatherSSN, long childSSN); bool PrintAFamily(long fatherSSN); bool PrintAllFamilies(); void ReadTransactionFile(); bool SearchForHusband(long husbandSSN);
  • 6. void destroy(); }; //------------------------------------------------------------------------------------------------ bool Family::failure = false; bool Family::success = true; //------------------------------------------------------------------------------------------------ bool Family::AddAChild(long id, string Fn, string Ln, long fatherSSN) { Hsbndptr H = top; Childptr LSib; if(SearchForHusband(fatherSSN) == failure) { cout << "The child " << Fn << " could not be created because there is no father with the SSN " << fatherSSN << endl; return failure; } else if(H -> myWife == NULL) { cout << "The child " << Fn << " could not be added because the father does not have a wife" << endl; return failure; } else { while(fatherSSN != H -> SSN) H = H -> nextFamily; if(H -> myWife -> myChildren == NULL) { Childptr C = new Child(id,Fn, Ln); H -> myWife -> myChildren = C; } else if(H -> myWife -> myChildren -> mySibling == NULL) { if(id == H -> myWife -> myChildren -> SSN) { cout << "The child " << Fn << " could not be added because a child with the SSN " << id <<
  • 7. " already exists" << endl; return failure; } else { LSib = LSib -> mySibling; } Childptr C = new Child(id, Fn, Ln); H -> myWife -> myChildren -> mySibling = C; return success; } else { if(id == H -> myWife -> myChildren -> SSN) { cout << "The child " << Fn << " could not be added because a child with the SSN " << id << " already exist" << endl << endl; } } } } //------------------------------------------------------------------------------------------------ bool Family::RemoveAChild(long fatherSSN, long childSSN) { Hsbndptr H = top; Childptr C; Childptr Sib; if(SearchForHusband(fatherSSN) == failure) { cout << "The child " << childSSN << " could not be removed because the father does not exist" << endl; return failure; } else { while(fatherSSN != H -> SSN)
  • 8. H = H -> nextFamily; if(H -> myWife == NULL) { cout << "The child " << childSSN << " could not be removed because the mother does not exist" << endl; return failure; } else if(H -> myWife -> myChildren == NULL) { cout << "The child could not be removed because the child does not exist" << endl << endl; return failure; } else if(H -> myWife -> myChildren -> mySibling == NULL) { if(childSSN != H -> myWife -> myChildren -> SSN) { cout << "The child could not be removed because the child does not exist" << endl << endl; return failure; } C = H -> myWife -> myChildren; H -> myWife -> myChildren = NULL; delete C; return success; } else { C = H -> myWife -> myChildren; Sib = H -> myWife -> myChildren -> mySibling; if(childSSN == C -> SSN) { H -> myWife -> myChildren == Sib; delete C; return success; } else {
  • 9. H -> myWife -> myChildren -> mySibling = NULL; delete Sib; return success; } } } } //------------------------------------------------------------------------------------------------ void Family::ReadTransactionFile() { string cmd; long id; string Fn; string Ln; long hId; // stands for husbandId fstream fin; fin.open("data.txt"); if(!fin) cout << "The file does not exist." << endl << endl; else { while(!fin.eof()) { fin >> cmd; if(cmd == "AddHusband") { fin >> id >> Fn >> Ln; AddHusband(id, Fn, Ln); } else if(cmd == "RemoveHusband") { fin >> id; RemoveHusband(id); } else if(cmd == "AddWife") {
  • 10. fin >> id >> Fn >> Ln >> hId; AddWife(id, Fn, Ln, hId); } else if(cmd == "RemoveWife") { fin >> id; RemoveWife(id); } else if(cmd == "AddAChild") { fin >> id >> Fn >> Ln >> hId; AddAChild(id, Fn, Ln, hId); } else if(cmd == "RemoveAChild") { fin >> hId >> id; RemoveAChild(hId, id); } else if(cmd == "PrintAFamily") { fin >> id; PrintAFamily(id); } else if(cmd == "PrintAllFamilies") PrintAllFamilies(); else cout << "The command " << cmd << " does not exist." << endl; } } } //------------------------------------------------------------------------------------------------ bool Family::SearchForHusband(long husbandSSN) { Hsbndptr H = top; while(H != NULL) {
  • 11. if(husbandSSN == H -> SSN) return success; else H = H -> nextFamily; } return failure; } //------------------------------------------------------------------------------------------------ int main() { Family USAFamilies; USAFamilies.ReadTransactionFile(); return 0; }