Write a program that creates a linked list of families Each

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 <string>

#include <iostream>

#include <fstream>

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;

}

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 po
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 po
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 po
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 po
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 po
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 po
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 po
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 po
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 po
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 po
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 po

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site