Learning Objective: . This assignment will help you to become proficient in using C++ pointers and working with Object Orientated Programs. Starter Code: You can find starter code for this assignment...

1 answer below »


Learning Objective:. This assignment will help you to become proficient in using C++ pointers and working with Object Orientated Programs.



Starter Code:You can find starter code for this assignment on Canvas under Files > Assignments > Assignment 4. You should start by downloading all the starter code files, build and run the code, run the code using valgrind. Note: the starter code does leak memory - you should probably fix this first.



Assignment.Develop a full ordered template List class, for discussion purposes, of Object. A template allows any specified type of Object to be stored. You can find starter class List code with the starter code in Canvas Files. Test Assignment 4 on two different types of Objects (code found in Canvas Files),



(a). Employee -- to be sorted alphabetically by last name, then first name



(b). NodeData -- to be sorted by numerical value, then char


As a template, put your declarations in the .h file and your implementation in the .cpp file. You must include your .cpp file in your .h file (after the declarations) for compilation. You should only build nodedata.cpp and listdriver.cpp. The templated classes are built by including the List interface in listdriver.cpp.


The List class includes the following functions. For the examples, assume the following are declared and suppose there are n items of data in any list .



ifstream infile1("data31.txt"), infile2("data32.txt");
bool success;
Employee* oneEmployee;
List company1, company2, company3, company4;


  1. All necessary constructors and destructor. (Should all take O(n) time, e.g., something done in one loop is O(n) )

  2. void buildList : given datafile, build an ordered list (note that buildList puts the responsibility on the Object class to know how to read from a file.) (insertion sort, O(n2) time, nested loop) E.g., company1.buildList(infile1);

  3. bool insert : insert one Object* into its correct sorted position, return whether successful (duplicates allowed)

  4. bool remove: remove first parameter, the object, from the list and set the second (pass by reference) parameter to the Object* from the list if it is found. The return value is whether the removal was successful. Second parameter is unreliable (although it's best to set it to nullptr) if return value is false. Remove from list. E.g.,



Employee target("duck", "donald"); // assume constructor exists
success = company1.remove(target, oneEmployee);
if (success) {
cout

delete oneEmployee; // could be inserted into another list
}


Prototype: bool remove(const Object&, Object*&);



  1. bool retrieve : similar to remove, but not removed from list. If there are duplicates in the list, the first one encountered is retrieved. Second parameter is null if the return value is false. E.g.,



Employee target("duck", "donald");
success = company1.retrieve(target, oneEmployee);
if (success) { cout


  1. void merge -- takes 2 sorted lists and merge into one long sorted list (No new memory is allocated. At termination of function, the two parameter lists are empty unless one is also the current object. Duplicates are allowed in a merged list.) (O(n) time, so insert must not be used or time would be O(n2)) E.g.,



// after this merge, company1 and company2 are empty,
// company3 contains all employees of company1 and company2
company3.merge(company1, company2);
// after merge, company3 is empty,
// company4 contains all employees of company3 and company4
company4.merge(company4, company3);



  1. void intersect -- takes 2 sorted lists and finds the items in common in both lists (New memory is allocated; at termination of the function, the two parameter lists are unchanged unless one is also the current object.) E.g.,



// after intersect, company1 and company2 are unchanged,
// company3 holds intersection of company1 and 2
company3.intersect(company1, company2);
// after intersect, company3 is unchanged,
// company4 holds the intersection of company3 and 4
company4.intersect(company3, company4);


  1. overload

  2. bool isEmpty() : determine whether a list is empty

  3. void makeEmpty() : empty out the list, deallocate all memory for all Nodes and each Object (note that the destructor can just call makeEmpty)

  4. operator= : to assign one list to another, deep copy (all new memory) (Takes O(n) time. No insert() call.)

  5. bool operator== and operator!= : compare two lists, == is true if the lists hold identical Objects as defined by Object’s operator==


Notes


You must implement the List class without using a dummy head node. In other words, the first node that the head pointer points to holds a pointer to real data. This forces you to think about the special cases involving the head pointer. You may use the insert and any other functions found in the starter code found or on thissample code page(Links to an external site.).


Implement the list using Nodes defined in the node.h file. Note that the Node class is also partially implemented and that each Node does not hold an Item, but a pointer to an Item. You will need to implement the copy constructor and destructor for the Node class.


NodeData data files are formatted so that each line of data includes an int and a char. For example,



100 x


Code for the NodeData class is with the starter code.


You can assume correctly formatted data (each line will always have one int and one char). So that file reading works in a similar manner under both linux and windows/mac, make sure the last line of data has an end-of-line character (under windows or mac, cursor is on the line below the last line of good data). Checking for eof() as in the List class’ buildList() routine works correctly, so do it that way. Do not use any other libraries.


Employee data files are formatted so that each line of data is the employee's name (last name then first name) followed by IDnumber and salary. For example,



mouse mickey 8584 55000

You can assume correctly formatted data (each line will always have two strings and two ints). Code for the Employee class is with the starter code. The string class has all the expected operators: ==, !=,


The list.h file should not #include other .h files, no nodedata.h, no employee.h.
I will provide my own main, classes, and data files to test your List class.
Any classes used to test your List class will have setData, ==, !=, <,>,


See starter code for a sample main - listdriver.cpp.

Answered 8 days AfterMay 13, 2021

Answer To: Learning Objective: . This assignment will help you to become proficient in using C++ pointers and...

Kamal answered on May 21 2021
152 Votes
list.cpp
list.cpp
////////////////////////////////  list.cpp file  /////////////////////////////
#include "list.h"
//----------------------------------------------------------------------------
// Constructor 
List::List() {
   head = NULL;
}
//----------------------------------------------------------------------------
// isEmpty 
// check to see if List is empty
bool List::isEmpty() const {
   return head == NULL;
}
//----------------------------------------------------------------------------
// insert 
// insert an item into list; ope
rator< of the NodeData class
// has the responsibility for the sorting criteria;
// Assume parameter is pointing to valid object
bool List::insert(NodeData* dataptr) {
   Node* ptr= new Node;
   if (ptr == NULL) return false;                 // out of memory, bail
   ptr->data = dataptr;                           // link the node to data
   // if the list is empty or if the node should be inserted before 
   // the first node of the list
   if (isEmpty() || *ptr->data < *head->data) {
      ptr->next = head;
      head = ptr;
   }

   // then check the rest of the list until we find where it belongs 
   else {
      Node* current = head->next;          // to walk list
      Node* previous = head;               // to walk list, lags behind
      // walk until end of the list or found position to insert
      while (current != NULL && *current->data < *ptr->data) {
            previous = current;                  // walk to next node
            current = current->next;
      }
      // insert new node, link it in
      ptr->next = current; 
      previous->next = ptr; 
   }
   return true;
}
//----------------------------------------------------------------------------
// buildList 
// continually insert new items from a file into the list
void List::buildList(ifstream& infile) {
   NodeData* ptr;
   bool successfulRead;                            // read good data
   bool success;                                   // successfully insert
   for (;;) {
      ptr = new NodeData;
      successfulRead = ptr->setData(infile);       // fill the NodeData object
      if (infile.eof()) {
         delete ptr;
         ptr = NULL;
         break;
      }
      // insert good data into the list, otherwise ignore it
      if (successfulRead) {
         success = insert(ptr);
      }
      else {
         delete ptr;
         ptr = NULL;
      }
      // if item was not inserted, throw it away
      if (!success) {
         delete ptr;
         ptr = NULL;
      }
   }
}
//----------------------------------------------------------------------------
// operator<<  
// output operator for class List, print data, 
// responsibility for output is left to object stored in the list
ostream& operator<<(ostream& output, const List& thelist) {
   List::Node* current = thelist.head;
   while (current != NULL) { 
      output << *current->data;
      current = current->next;
   }
   return output;                      // enables output << x << y;
}
//----------------------------------------------------------------------------
// destructor
List::~List()
{
    //makeEmpty();
}
//----------------------------------------------------------------------------
// copy contructor
List::List(const List& oldlist)
{
    head = oldlist.head;
}
//----------------------------------------------------------------------------
// remove
// remove first parameter, the object, from the list and set the second 
// (pass by reference) parameter to the Object* from the list if it is found
bool List::remove(const NodeData& first, NodeData*& second)
{
    bool success = false;
    if (isEmpty())
        return false;

    Node* ptr = head;
    while (ptr)
    {
        if (ptr->data->getNum() == first.getNum() 
            && ptr->data->getCh() == first.getCh())
            success = true;
        ptr = ptr->next;
    }
    if (success)
    {
        *second = first;
        delete& first;
        return true;
    }
    else
    {
        second = nullptr;
        return false;
    }
}
//----------------------------------------------------------------------------
// retrieve
bool List::retrieve(const NodeData& first)
{
    bool success = false;
    if (isEmpty())
        return false;
    Node* ptr = head;
    while (ptr)
    {
        if (ptr->data->getNum() == first.getNum() 
            && ptr->data->getCh() == first.getCh())
        {
            success = true;
            break;
        }
        ptr = ptr->next;
    }
    if (success)
    {
        Node* ptr1 = ptr->next;
        while (ptr1)
        {
            if (ptr1->data->getNum() == first.getNum() 
                && ptr1->data->getCh() == first.getCh())
            {
                delete& first;
            }
        }
        return true;
    }
    return false;
}
//----------------------------------------------------------------------------
// merge
void List::merge(List* list1, List* list2)
{
    // insert first list
    head = list1->head;
    Node* ptr = head;
    while (ptr)
        ptr = ptr->next;
    // insert second list
    ptr = list2->head;
}
//----------------------------------------------------------------------------
// intersect
void List::intersect(List* list1, List* list2)
{
    Node* ptr = head;
    Node* ptr1 = list1->head;
    Node* ptr2 = list2->head;
    while (ptr1)
    {
        while (ptr2)
        {
            if (ptr1->data->getNum() == ptr2->data->getNum() 
                && ptr1->...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here