/*My instructor says: My driver does not test all the class functions. My test d
ID: 3568935 • Letter: #
Question
/*My instructor says: My driver does not test all the class functions. My test data should be input from the keyboard.*/ //------------------------------------------------------------------------ //driver.cpp (for some tests) // ListDemo.cpp : Defines the entry point for the console application. //driver.cpp //#include "stdafx.h" #include "List.h" #include <iostream> using namespace std; int main(int argc, char const *argv[]) { List Test; Test.insert("AAA", 0); Test.insert("BBB", 1); Test.insert("CCC", 2); cout<<"The first list is: "<<endl; Test.display(cout); cout << Test.getSize() << endl; List Test_Second; Test_Second = Test; cout<<"The Second list is: "<<endl; Test_Second.display(cout); cout << Test_Second.getSize() << endl; Test_Second.insert("DDD", 3); List Test_Third(Test_Second); cout<<"The Third list is: "<<endl; Test_Third.display(cout); cout<<" " << Test_Third.getSize() << endl; Test_Third.insert("EEE",4); cout<<"The Third list after inserting another element is: "<<endl; Test_Third.display(cout); cout << Test_Third.getSize() << endl; cout<<"Deleted an element whose index value is 2: "<<endl; Test_Third.erase(2); cout<<endl; cout<<"Therefore, the elements present in the Third list are: "<<endl; Test_Third.display(cout); cout<<" "<<"The value AAA is found at: "<<Test_Third.find("AAA"); cout<<endl; cout <<"The size of the Third list is :" <<Test_Third.getSize() << endl; system ("pause"); return 0; } //------------------------------------------------------------------------------ //List.cpp //#include "stdafx.h" #include "List.h" //Construct empty List List::List() { first = NULL; mySize = 0; } //Construct copy of source List List::List( const List &source ) { first = NULL; mySize = 0; //check the code, the value of mySize is //changed int index = 0; NodePointer current = source.first; while(current != NULL) { insert(current->data, index++); current = current->next; } } List::~List( ) { for(int i = 0; i <= mySize; i++) erase(i); } const List & List::operator=( const List &rightSide ) { int index = 0; NodePointer current = rightSide.first; while(current != NULL) { insert(current->data, index++); current = current->next; } return *this; } int List::getSize( ) const { return mySize; } bool List::empty( ) const { if(mySize == -1) return true; return false; } void List::insert( ElementType dataVal, int index ) { if(index == 0) { NodePointer NewNode = new Node; NewNode->data = dataVal; NewNode->next = first; first = NewNode; mySize++; } else if(index > 0 && index <= (mySize + 1)) { NodePointer current = first; for(int i = 0; i < index - 1; i++) current = current->next; NodePointer NewNode = new Node; NewNode->data = dataVal; NewNode->next = current->next; current->next = NewNode; mySize++; } } void List::erase( int index ) { if(first != NULL) { if(index == 0) { NodePointer temp = first; first = temp->next; delete temp; mySize--; } else if(index > 0 && index <= (mySize + 1)) { NodePointer current = first; for(int i = 0; i < index -1; i++) current = current->next; NodePointer temp = current->next; if(temp!=NULL) //check the condition that has been modified current->next = temp->next; delete temp; mySize--; } } } void List::display( ostream &out ) const { NodePointer current = first; while(current != NULL) { out << current->data << endl; current = current->next; } } int List::find( ElementType value) const { NodePointer current = first; int index = 0; while(current != NULL) { if(current->data == value) return index; else { index++; current = current->next; } } return -1; } //-------------------------------------------------------------------------------------------- // List.h #ifndef LIST #define LIST #include <iostream> #include <cstdlib> #include <string> using namespace std; typedef string ElementType; class List { private: class Node { public: ElementType data; Node * next; Node( ): data( ElementType( ) ), next( NULL ) { } Node( ElementType initData ): data( initData ), next( NULL ) { } }; // end of Node class typedef Node * NodePointer; public: List( ); /* Construct a List object Precondition: none. Postcondition: An empty List object has been constructed. */ List( const List &source ); /* Construct a copy of a List object. Precondition: None. Postcondition: A copy of source has been constructed. */ ~List( ); /* Destroys a List object. Precondition: None. Postcondition: Any memory allocated to the List object has been freed. */ const List & operator=( const List &rightSide ); /* Assign a copy of a List object to the current object. Precondition: none Postcondition: A copy of rightside has been assigned to this object. A constant reference to this list is returned. */ int getSize( ) const; /* Returns the size of the list (number of items in the list) Precondition: none Postcondition: The return value is the number of items in the list. */ bool empty( ) const; /* Check if this List is empty Precondition: none Postcondition: The return value is true if this List object is empty; otherwise the return value is false. */ void insert( ElementType dataVal, int index ); /* Insert a value into this List at a given index Precondition: The index is valid (0 <= index <= the list size). The first position is index 0, the second position is index 1, etc. Postcondition: dataval has been inserted into the list at the position determined by index (provided there is room and index is a legal position). */ void erase( int index ); /* Remove the value from this List at a given index. Precondition: The list is not empty and index is valid (0 <= index < the list size). Postcondition: the element at position index has been removed (provided index is a legal position). */ void display( ostream &out ) const; /* Display the contents of this List Precondition: ostream out is open Postcondition: the items in this List have been output to stream out */ int find( ElementType value) const; /* Find the first occurrence of a value in this List Preconditions: None Postconditions: The return value is the index of the first List item that matches value. The first list item has index 0, the second has index 1, etc. The return value is -1 if value is not found in the list. */ private: NodePointer first; int mySize; }; // end of List class #endif
Explanation / Answer
Your driver doesn't have any calls to the empty() function. Maybe you can add some empty() calls after getSize() calls.
An example would be:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.