Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that creates a linked list of families. Each family contains

ID: 3692567 • Letter: W

Question

Write a C++ 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 if this person is not in the linked list already. Print appropriate report if this transaction fails or if the transaction succeeds.

If this husband exist print appropriate message and return false

If this husband is successfully inserted just report appropriate message and return true

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. Print appropriate report if this transaction fails or if the transaction succeeds.

If this husband does not exist print appropriate message and return false

Otherwise, remove the entire family and report appropriate message and return true

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.

Report appropriate error message if either the husband does not exists or husband already has a wife

Otherwise add the wife, report succeeding message and return true  

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

Report appropriate error message if either the husband does not exists or husband does not have a wife

Otherwise remove the wife (and possible children), report appropriate message, and return true  

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.

If the father does not exist, report appropriate message and return false

If father exists but has no wife, report appropriate message and return false

Otherwise, if the child is already there, report appropriate message and return false

Otherwise, add the child, report appropriate succeeding message and return true

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

If the father does not exist, report appropriate message and return false

If father exists but has no wife, report appropriate message and return false

Otherwise, if the child is not already there, report appropriate message and return false

Otherwise, remove the child, report appropriate succeeding message and return true

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

If the father in the family does not exist, report appropriate message and return false

Otherwise, print the family and report appropriate succeeding message and return true

PrintAllFamilies, goes through the entire linked list of the family and prints the information of each family one by one. Report a failing message if the linked list is empty.

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.

Explanation / Answer

The given below is the code with comment to explain the working of code;

//class husband//

class Habby;

typedef Habby* habbyPtr;

//class wife//

class Wif;

typedef Wif* wifPtr;

//class child//

class Chd;

typedef Chd* ChdPtr;

class People

{//class family//

    friend class Fam;

    protected:

        long SSN;

        string Fnam;

        string Lnam;

    public:

        People(){};

        People(long id, string Fn, string Ln)

        {SSN = id; Fnam = Fn; Lnam=Ln;}

};

class Fam

{

    protected:

        habbyPtr top;

        static bool Failure;

        static bool Success;

    public:

        void Addhusband(long SSN, string Fnam, string Lnam);

        void RemoveHasband(long SSN);

        void AddWife(long SSN, string Fnam, string Lnam, long habbySSN);

        void RemoveWife(long habbySSN);

        void AddAChild(long SSN, string Fnam, string Lnam, long fatherSSN);

        void RemoveAChild(long FatherSSN, long ChdSSN);

        void PrintAFamily(long fatherSSN);

        void PrintAllFamilies();

        void ReadTransaction();

        bool habbyExist(long id);

       bool wifExist(long id);

        bool Familyschild(long id);

};

class Habby:public People

{

    friend class Fam;

    protected:

        habbyPtr nextFam;

        wifPtr myWif;

    public:

        Habby(){nextFam = NULL; myWif = NULL;}

        Habby(long id, string Fn, string Ln):People(id, Fn, Ln)

        {nextFam = NULL; myWif = NULL;}

};

class Wif:public People

{

    friend class Fam;

    protected:

        ChdPtr mChildren;

    public:

        Wif(){mChildren = NULL;}

        Wif(long id, string Fn, string Ln):People(id, Fn, Ln)

        {mChildren = NULL;}

};

class Chd:public People

{

    friend class Fam;

    protected:

        ChdPtr mSibiling;

    public:

        Chd(){mSibiling = NULL;}

        Chd(long id, string Fn, string Ln):People(id, Fn, Ln)

        {mSibiling = NULL;}

};

bool Fam::Success = true;

bool Fam::Failure = false;

void Fam::Addhusband(long id, string Fn, string Ln)

{

    if(habbyExist(id))

    {

        cout << "This people already exist!" << endl;

        return;

    }

    habbyPtr hPtr = new Habby(id, Fn, Ln);

    hPtr->myWif = NULL;

    hPtr->nextFam = top;

    top = hPtr;

}

void Fam::RemoveWife(long habbySSN)

{

    if(!habbyExist(habbySSN))

    {

        cout << "Husband does not exist!";

        return;

    }

    if(!wifExist(habbySSN))

    {

        cout << "Wife does not exist!";

        return;

    }

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    ChdPtr cPtr2;

    while(hPtr != NULL)

    {

        if(hPtr->SSN == habbySSN)

        {

            wPtr=hPtr->myWif;

            if (wPtr != NULL)

            {

                cPtr = wPtr->mChildren;

                while(cPtr != NULL)

                {

                    cPtr2 = cPtr->mSibiling;

                    wPtr->mChildren=cPtr2;

                    delete (cPtr);

                    cPtr = cPtr2;

                }

                delete (wPtr);

                hPtr->myWif = NULL;

            }

        }

        hPtr = hPtr->nextFam;

    }

}

bool Fam::habbyExist(long id)

{

    habbyPtr hPtr = top;

    while(hPtr != NULL)

    {

        if (hPtr->SSN == id)

        {

            return Success;

        }

        hPtr = hPtr->nextFam;

    }

    return Failure;

}

bool Fam::wifExist(long id)

{

    habbyPtr hPtr = top;

    wifPtr wPtr;

    while (hPtr != NULL)

    {

        if (hPtr->SSN == id)

        {

            wPtr = hPtr->myWif;

            if (wPtr == NULL)

                return Failure;

        }

        hPtr = hPtr->nextFam;

    }

    return Success;   

}

bool Fam::Familyschild(long id)

{

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    while (hPtr != NULL)

    {

        if (hPtr->SSN == id)

      {

            wPtr = hPtr->myWif;

            if (wPtr == NULL)

            {

                cout << "NO Children!" << endl;

                return Success;

            }

            cPtr = wPtr->mChildren;

            if (cPtr == NULL)

            {

                cout << "This couple has no Children!" << endl;

                return Success;

            }

        }

        hPtr = hPtr->nextFam;

    }

    cout << "This family has Children" << endl;

    return Failure;

}

void Fam::RemoveHasband(long SSN)

{

    if(!habbyExist(SSN))

        return;

    if(wifExist(SSN))

    {

        RemoveWife(SSN);

    }

    habbyPtr hPtr = top;

    habbyPtr next;

    habbyPtr prev;

    if (hPtr == NULL)

        return;

    else if (hPtr->nextFam == NULL)

    {

        delete (hPtr);

        hPtr = NULL;

        return;

    }

    else

        while (hPtr->SSN != SSN)

        {

            next = hPtr->nextFam;

            prev = hPtr;

            hPtr = hPtr->nextFam;

        }

    next = hPtr->nextFam;

    delete (hPtr);

    prev->nextFam = next;

}

void Fam::AddWife(long SSN, string Fnam, string Lnam, long habbySSN)

{

    habbyPtr hPtr = top;

    wifPtr wPtr;

    if(!habbyExist(habbySSN))

        return;

    while(hPtr!=NULL)

    {

        if(hPtr->SSN == habbySSN)

        {

            wPtr = new Wif(SSN, Fnam, Lnam);

            hPtr->myWif=wPtr;

            return;

        }

        hPtr = hPtr->nextFam;

    }

    return;

}

void Fam::AddAChild(long SSN, string Fnam, string Lnam, long fatherSSN)

{

    if(!habbyExist(fatherSSN))

    {

        cout << "Husband Does Not Exist!" << endl;

        return;

    }

    if(!wifExist(fatherSSN))

    {

        cout << "Wife Does Not Exist!" << endl;

        return;

    }

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    while (hPtr != NULL)

    {

        if(hPtr->SSN == fatherSSN)

        {

            cPtr = new Chd(SSN,Fnam,Lnam);

            wPtr = hPtr->myWif;

            cPtr->mSibiling = wPtr->mChildren;

            wPtr->mChildren = cPtr;

            return;

        }

        hPtr=hPtr->nextFam;

    }

}

void Fam::RemoveAChild(long FatherSSN, long ChdSSN)

{

    if(!habbyExist(FatherSSN))

        return;

    if(!wifExist(FatherSSN))

        return;

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    ChdPtr cPtr2;

    while (hPtr != NULL)

    {

        if (hPtr->SSN == FatherSSN)

        {

            wPtr = hPtr->myWif;

            if (wPtr != NULL)

                cPtr = wPtr->mChildren;

                cPtr2 = cPtr;

            while (cPtr != NULL)

            {

                if (cPtr->SSN == ChdSSN)

                {

                    cPtr2 = cPtr->mSibiling;

                    wPtr->mChildren = cPtr2;

                    delete (cPtr);

                }

                cPtr = cPtr->mSibiling;

            }

        }

        hPtr = hPtr->nextFam;

    }

}

void Fam::PrintAFamily(long fatherSSN)

{

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    if(!habbyExist(fatherSSN))

        return;

    while(hPtr != NULL)

    {

        if(hPtr->SSN == fatherSSN)

        {

            cout << hPtr->SSN << ""< hPtr->Fnam << ""<< hPtr->Lnam << "" << endl;

            wPtr = hPtr->myWif;

            if (wPtr != NULL)

            {

                cout << wPtr->SSN << ""<< wPtr->Fnam << ""<< wPtr->Lnam << "" << endl;

                cPtr = wPtr->mChildren;

            }

                while (cPtr != NULL)

                {

                    cout << cPtr->SSN << ""<< cPtr->Fnam << "" << cPtr->Lnam << "" << endl;

                    cPtr = cPtr->mSibiling;

              }

        }

        hPtr=hPtr->nextFam;

    }

}

void Fam::PrintAllFamilies()

{

    habbyPtr hPtr = top;

    wifPtr wPtr;

    ChdPtr cPtr;

    while (hPtr != NULL)

    {

        cout << hPtr->SSN << "" << hPtr->Fnam << "" << hPtr->Lnam << "" << endl;

        wPtr = hPtr->myWif;

        if(wPtr != NULL)

        {

            cout << wPtr->SSN << ""<< wPtr->Fnam << "" << wPtr->Lnam << "" << endl;

            cPtr = wPtr->mChildren;

            while (cPtr != NULL)

            {

                cout << cPtr->SSN << ""<< cPtr->Fnam << "" << cPtr->Lnam << "" << endl;

                cPtr = cPtr->mSibiling;

            }

        }

        hPtr = hPtr->nextFam;

    }

}

void Fam::ReadTransaction()

{

    long id, hubId, FaId;

    string Cmd, Fn, Ln;

    fstream fin;

    fin.open("TransactionFile.txt");

    if (!fin)

    {

        cout << "Cannot Open File";

        return;

    }

    while (fin >> Cmd)

    {

        if (Cmd == "Addhusband")

        {

            fin >> id >> Fn >> Ln;

            Addhusband(id, Fn, Ln);

        }

        else if (Cmd == "RemoveHasband")

      {

            fin >> id;

            RemoveHasband(id);

        }

        else if (Cmd == "AddWife")

        {

            fin >> id >> Fn >> Ln >> hubId;

            AddWife(id, Fn, Ln, hubId);

        }

        else if (Cmd == "RemoveWife")

        {

            fin >> hubId;

            RemoveWife(hubId);

        }

        else if (Cmd == "AddAChild")

        {

            fin >> id >> Fn >> Ln >> FaId;

            AddAChild(id, Fn, Ln, FaId);

        }

        else if (Cmd == "RemoveAChild")

        {

            fin >> FaId >> id;

            RemoveAChild(FaId, id);

        }

        else if (Cmd == "PrintAFamily")

        {

            fin >> id;

            PrintAFamily(id);

        }

        else if (Cmd == "PrintAllFamilies")

        {

            PrintAllFamilies();

        }

    }

    fin.close();

}

//main function//

int main()

{

    Fam USAFamilies;

    USAFamilies.ReadTransaction();

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote