Question: I need help with this C++ Object Oriented Programming (Data Structures) assignment! You basically have to add two functions to this file. Here are the

I need help with this C++ Object Oriented Programming (Data Structures) assignment! You basically have to add two functions to this file. Here are the instructions:

I need help with this C++ Object Oriented Programming (Data Structures) assignment!

#include  using std::cin; using std::endl; #include  using std::string; #include "list.h" // List class definition void instructions(); // function to test a List template void testList( List &listObject, const string &typeName ) { cout > choice; switch ( choice ) { case 1: cout > value; listObject.insertAtFront( value ); listObject.print(); break; case 2: cout > value; listObject.insertAtBack( value ); listObject.print(); break; case 3: cout > value; cout > pos; listObject.insertMiddle( value , pos ); listObject.print(); break; case 4: if ( listObject.removeFromFront( value ) ) cout > pos; if ( listObject.removeMiddle( value, pos ) ) cout  integerList; testList( integerList, "integer" ); // test List of double values List doubleList; testList( doubleList, "double" ); return 0; } // end main 

Here is list.h, the file you will need to add the two functions to:

#ifndef LIST_H #define LIST_H #include  using std::cout; #include  #include "listnode.h" // ListNode class definition template class List { public: List(); // constructor ~List(); // destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; private: ListNode *firstPtr; // pointer to first node ListNode *lastPtr; // pointer to last node // utility function to allocate new node ListNode *getNewNode( const NODETYPE & ); }; // end class List // default constructor template List::List() : firstPtr( 0 ), lastPtr( 0 ) { // empty body } // end List constructor // destructor template List::~List() { if ( !isEmpty() ) { // List is not empty // cout  *currentPtr = firstPtr; ListNode *tempPtr; while ( currentPtr != 0 ) // delete remaining nodes { tempPtr = currentPtr; // commented out the output -- no need to print what we are deallocating // cout data nextPtr; delete tempPtr; } } // cout  void List::insertAtFront( const NODETYPE &value ) { ListNode *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; } // end else } // end function insertAtFront // insert node at back of list template void List::insertAtBack( const NODETYPE &value ) { ListNode *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; } // end else } // end function insertAtBack // delete node from front of list template bool List::removeFromFront( NODETYPE &value ) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { ListNode *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; // data being removed delete tempPtr; return true; // delete successful } // end else } // end function removeFromFront // delete node from back of list template bool List::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return false; // delete unsuccessful else { ListNode *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { ListNode *currentPtr = firstPtr; // locate second-to-last element while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } // end else value = tempPtr->data; delete tempPtr; return true; // delete successful } // end else } // end function removeFromBack // is List empty? template bool List::isEmpty() const { return firstPtr == 0; } // end function isEmpty // return pointer to newly allocated node template ListNode *List::getNewNode( const NODETYPE &value ) { return new ListNode( value ); } // end function getNewNode // display contents of List template void List::print() const { if ( isEmpty() ) { cout  *currentPtr = firstPtr; cout data nextPtr; } // end while cout  

Here is listnode.h:

#ifndef LISTNODE_H #define LISTNODE_H // forward declaration of class List template class List; template class ListNode { friend class List; // make List a friend public: ListNode( const NODETYPE & ); // constructor NODETYPE getData() const; // return data in node private: NODETYPE data; // data ListNode *nextPtr; // next node in list }; // end class ListNode // constructor template ListNode::ListNode( const NODETYPE &info ) : data( info ), nextPtr( 0 ) { // empty body } // end ListNode constructor // return copy of data in node template NODETYPE ListNode::getData() const { return data; } // end function getData #endif 

Thanks a lot for your help!!

Modify the List class (file list.h so that it has two more functions, which will allow inserts and removes from anywhere in the linked list. Your functions should be called: *insertMiddle * removeMiddle Your functions should have all the same features as the given insert and remove functions, except that yours each have one extra parameter. The second parameter on each of your functions should be of type int, representing the position at which to insert (or delete). Sample calls for a list of integers: .insertMiddle (345, 5); // attempts to insert the value 345 /as the 5th item in the list // attempts to delete the 10th item in the //list and captures its value into x L.removeMiddle (x, 10); For insertMiddle, if the position number is larger than the number of items in the list, just insert the item at the back. If it's too small (i.e. 0 or less), insert at the front. For removeMiddle, return false if the position is invalid (without removing anything). You can use this program to test your class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!