Write a program containing two linked list of 6 user profile

Write a program containing two linked list of 6 user profiles each. Each person is identified by his/her first name, last name and 10-digit telephone number member variables. Be sure to add constructors, destructors, accessor and mutator member functions. For each list, the program should add a new person at the back of the list and display the people in the list. Then, the program should combine their nodes so that the nodes of the new list alternate between those of the two original nodes: . Do not allocate any new nodes. Lastly, the program should display this final linked list. Your program must not take any user input. These lists should be initialized using one function call in the execution function.

Solution

Program code:

// LinkedList-AddingPersons-MergeLists.cpp.

#include \"stdafx.h\"

#include \"LinkedList.h\"

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

class Person

{

private:

     string fname, lname, phno;

public:

     Person()

     {

          fname =\"\";

          lname =\"\";

          phno =\"\";

     }

     Person(string fnam, string lnam, string ph)

     {

          fname = fnam;

          lname = lnam;

          phno = ph;

     }

     void setFirstName(string fnam)

     {

          fname = fnam;

     }

    

     void setLastName(string lnam)

     {

          lname = lnam;

     }

    

     void setPhoneNumber(string phnum)

     {

          phno = phnum;

     }

     string getFirstName()

     {

          return fname;

     }

    

     string getLastName()

     {

          return lname;

     }

    

     string getPhoneNumber()

     {

          return phno;

     }

     friend ostream &operator<<(ostream &out, Person p)

     {

          out<<\"First Name: \"<<p.fname;

          out<<\"Last Name: \"<<p.lname;

          out<<\"Phone Number: \"<<p.phno;

          return out;

     }

};

void execute(SLList<Person> list1, SLList<Person> list2);

int main()

{

     SLList<Person> list1, list2, list3;

     execute(list1, list2);

     list1.printAll();

     list2.printAll();

     for (SLLNode<Person> *tmp = list1.head,SLLNode<Person> *tmp1 = list2.head ; tmp != 0 && tmp1!=0; tmp = tmp->next, tmp1=tmp1->next)

     {

          list3.addBack(tmp->info);

          list3.addBack(tmp1->info);

     }

     cout << endl;

     system(\"pause\");

     return 0;

}

void execute(SLList<Person> list1, SLList<Person> list2)

{

     ifstream file1(\"Person1.txt\");

     ifstream file2(\"Person2.txt\");

     string fname, lname, phone;

     if(file1)

     {

          file1>>fname;

         

          while(!file1.eof())

          {

              file1>>lname;

              file1>>phone;

              Person p(fname, lname, phone);            

              list1.addBack(p);

              file1>>fname;

          }

     }

     file1.close();

     if(file2)

     {

          file2>>fname;     

          while(!file2.eof())

          {

              file2>>lname;

              file2>>phone;

              Person p(fname, lname, phone);            

              list2.addBack(p);

              file2>>fname;

          }

     }

     file2.close();

}

//************************ LinkedList.cpp **************************

#include \"stdafx.h\"

#include \"LinkedList.h\"

#include <iostream>

using namespace std;

template <class T>

SLList<T>::~SLList()

{

     for (SLLNode<T> *p; !isEmpty(); )

     {

          p = head->next;

          delete head;

          head = p;

     }

}

template <class T>

void SLList<T>::addBack(T el)

{

     if (tail != 0)

     {    

          tail->next = new SLLNode<T>(el);

          tail = tail->next;

     }

     else

          head = tail = new SLLNode<T>(el);

}

template <class T>

void SLList<T>::deleteNode(T el)

{

     if (head != 0)   

     {

          if (head == tail && el == head->info)

          {

              delete head;                      

              head = tail = 0;

          }

          else if (el == head->info)

          {

              SLLNode<T> *tmp = head;

              head = head->next;

              delete tmp;             

          }

          else

          {                      

              SLLNode<T> *pred, *tmp;

              for (pred = head, tmp = head->next;

                   tmp != 0 && !(tmp->info == el);

                   pred = pred->next, tmp = tmp->next);

              if (tmp != 0)

              {

                   pred->next = tmp->next;

                   if (tmp == tail)

                        tail = pred;

                   delete tmp;

              }

          }

     }

}

template <class T>

bool SLList<T>::isInList(T el)

{

     SLLNode<T> *tmp;

     for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);

     return tmp != 0;

}

template <class T>

void SLList<T>::printAll()

{

     for (SLLNode<T> *tmp = head; tmp != 0; tmp = tmp->next)

          cout << tmp->info << \" \";

     cout << endl;

}

/*******************LinkedList.h**********************/

#ifndef LinkedList_H

#define LinkedList_H

template <class T>

class SLLNode

{

public:

    SLLNode()

    {

         info =0;

         next = 0;

    }

    SLLNode(T el, SLLNode<T> *ptr = 0)

    {

        info = el;

        next = ptr;

    }

    T info;

    SLLNode<T> *next;

};

template <class T>

class SLList

{

public:

    SLList()

    {

        head = tail = 0;

    }

    //~SLList();

    int isEmpty()

    {

        return head == 0;

    }

    void addBack(T);   

    void deleteNode(T);

    bool isInList(T);

    void printAll();

    SLLNode<T> *head, *tail;

};

#endif

Sample text files:

Person1.txt

Lary Pick 2348989890

James Erick 2356568971

Alferd Lissy 2349988007

Urin Toshy 2347766889

Imarn Jacker 2340090090

Edward Corner 2342233445

Person2.txt:

Robert Williams 1234567890

Rose Mary 1239898980

William Goose 1234565487

Tiny Shine 1237869504

Olive Queen 1239856740

Ginger Red 1234455667


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site