So I found the answer to my question here on chegg and is rated 100% can someone
ID: 3731291 • Letter: S
Question
So I found the answer to my question here on chegg and is rated 100% can someone with C++ knowledge explain to me where in the program I insert the correct answer. I tried to replace the main with this answer and I get the errors.
Appreciate help.
ASSIGMENT DESCRIPTION AND PROVIDED PROGRAM
We will define a ring to be sorted bi-directional circular list that is implemented using links. Each node in the ring will have a backward and forward link and a data entry. The ring will be sorted in ascending order following the forward links. Of course, since the ring is circular, there is a discontinuity in the sort at the head (the start/end point) of the ring. Create a templated ring class along with functions for inserting and deleting data, and a function that displays the contents of the ring. You must use the LinkedSortedList ADT from our textbook as the base code for your solution. You will need to update a few portions of the code so that it supports bi-directional linked lists. Write a driver that inserts 50 unique random int’s into a ring, displays the ring in both forward and backward directions by following the links, and then deletes all of the odd values that were inserted. Perform the deletions by saving a list of the odd values (say, in a vector or array) and then using your delete function – don’t write a special function that traverses the ring, deleting odd values as it goes. Should your delete function follow the forward or backward links when deleting? Display the ring again in both forward and backwards directions after performing the deletions.
Code provided:
Main.cpp
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
#include <iostream>
#include <string>
#include "LinkedSortedList.h" // ADT list operations
using namespace std;
void displayList(SortedListInterface<string>* listPtr)
{
cout << "The sorted list contains " << endl;
for (int pos = 1; pos <= listPtr->getLength(); pos++)
{
cout << listPtr->getEntry(pos) << " ";
} // end for
cout << endl << endl;
} // end displayList
void copyConstructorTester()
{
LinkedSortedList<string> list;
string items[] = { "zero", "one", "two", "three", "four", "five" };
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
list.insertSorted(items[i]);
}
displayList(&list);
LinkedSortedList<string> copyOfList(list);
cout << "Copy of list: ";
displayList(©OfList);
cout << "The copied list: ";
displayList(&list);
} // end copyConstructorTester
void sortedListTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
cout << " Test isEmpty with an empty list:" << endl;
if (listPtr->isEmpty())
cout << "OK" << endl;
else
cout << "isEmpty() error" << endl;
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
// display the list
cout << "List should contain Brent, Donna, " <<
"Jerry, Luke, Sue, Tom" << endl;
cout << " List actually contains:" << endl;
displayList(listPtr);
cout << endl;
// test getPosition
cout << " Test getPosition: " << endl;
// These names are in the list
cout << "Brent is at position " << listPtr->getPosition(brent) << endl;
cout << "Donna is at position " << listPtr->getPosition(donna) << endl;
cout << "Jerry is at position " << listPtr->getPosition(jerry) << endl;
cout << "Luke is at position " << listPtr->getPosition(luke) << endl;
cout << "Sue is at position " << listPtr->getPosition(sue) << endl;
cout << "Tom is at position " << listPtr->getPosition(tom) << endl;
// These names are not in the list
string missy = "Missy";
cout << "Missy belongs at position " << listPtr->getPosition(missy) << endl;
string zeke = "Zeke";
cout << "Zeke belongs at position " << listPtr->getPosition(zeke) << endl;
string aaron = "Aaron";
cout << "Aaron belongs at position " << listPtr->getPosition(aaron) << endl;
// test getLength and getEntry
cout << " Test getLength and getEntry: " << endl;
cout << " List has " << listPtr->getLength() << " entries, as follows: " << endl;
for (int i = 1; i <= listPtr->getLength(); i++)
cout << i << ": " << listPtr->getEntry(i) << endl;
// test remove
cout << " Test remove: " << endl;
// remove first entry
cout << " Removing first item (Brent): " << listPtr->removeSorted(brent) << "; should be 1 (true)" << endl;
cout << " After removing Brent, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// remove interior
cout << " Removing interior item (Luke): " << listPtr->removeSorted(luke) << "; should be 1 (true)" << endl;
cout << " After removing Luke, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// remove last
cout << &" Removing last item (Tom): "[listPtr->removeSorted(tom)] << "; should be 1 (true)" << endl;
cout << " After removing last item, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
cout << " Removing a missing item (Brent): " << listPtr->removeSorted(brent) << "; should be 0 (false)" << endl;
cout << " Removing a missing item (Luke): " << listPtr->removeSorted(luke) << "; should be 0 (false)" << endl;
cout << " Removing a missing item (Tom): " << listPtr->removeSorted(tom) << "; should be 0 (false)" << endl;
cout << " The list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// test clear()
cout << " Test clear(): " << endl;
listPtr->clear();
if (listPtr->isEmpty())
cout << " The list is empty after invoking clear()." << endl;
else
cout << " clear() error" << endl;
} // end sortedListTester
void listOpsTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
displayList(listPtr);
cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 0 (false)" << endl;
cout << "getLength returns : " << listPtr->getLength() << "; should be 6" << endl;
cout << "remove(2): returns " << listPtr->remove(2) << "; should be 1 (true)" << endl;
cout << "remove(1): returns " << listPtr->remove(1) << "; should be 1 (true)" << endl;
cout << "remove(6): returns " << listPtr->remove(6) << "; should be 0 (false)" << endl;
displayList(listPtr);
cout << "getLength returns : " << listPtr->getLength() << "; should be 4" << endl;
cout << "clear: " << endl;
listPtr->clear();
cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 1 (true)" << endl;
try
{
cout << "Attempt an illegal retrieval from position 6: " << endl;
listPtr->getEntry(6);
}
catch (PrecondViolatedExcep e)
{
cout << e.what() << endl;
} // end try/catch
} // end listOpsTester
int main()
{
cout << " Linked Sorted List ";
copyConstructorTester();
SortedListInterface<string>* listPtr = new LinkedSortedList<string>();
cout << "Testing the Link-Based Sorted List:" << endl;
sortedListTester(listPtr);
cout << "=========================================" << endl;
cout << " Testing the List Operations:" << endl;
listOpsTester(listPtr);
cout << " Fin ";
return 0;
} // end main
/*
Linked Sorted List
Adding zero
Adding one
Adding two
Adding three
Adding four
Adding five
The sorted list contains
five four one three two zero
Copy of list: The sorted list contains
five four one three two zero
The copied list: The sorted list contains
five four one three two zero
Testing the Link-Based Sorted List:
Test isEmpty with an empty list:
OK
List should contain
Brent, Donna, Jerry, Luke, Sue, Tom
List actually contains:
The sorted list contains
Brent Donna Jerry Luke Sue Tom
Test getPosition:
Brent is at position 1
Donna is at position 2
Jerry is at position 3
Luke is at position 4
Sue is at position 5
Tom is at position 6
Missy belongs at position -5
Zeke belongs at position -7
Aaron belongs at position -1
Test getLength and getEntry:
List has 6 entries, as follows:
1: Brent
2: Donna
3: Jerry
4: Luke
5: Sue
6: Tom
Test remove:
Removing first item (Brent): 1; should be 1 (true)
After removing Brent, list contains 5 items, as follows:
The sorted list contains
Donna Jerry Luke Sue Tom
Removing interior item (Luke): 1; should be 1 (true)
After removing Luke, list contains 4 items, as follows:
The sorted list contains
Donna Jerry Sue Tom
Removing last item (Tom): ; should be 1 (true)
After removing last item, list contains 3 items, as follows:
The sorted list contains
Donna Jerry Sue
Removing a missing item (Brent): 0; should be 0 (false)
Removing a missing item (Luke): 0; should be 0 (false)
Removing a missing item (Tom): 0; should be 0 (false)
The list contains 3 items, as follows:
The sorted list contains
Donna Jerry Sue
Test clear():
The list is empty after invoking clear().
=========================================
Testing the List Operations:
The sorted list contains
Brent Donna Jerry Luke Sue Tom
isEmpty: returns 0; should be 0 (false)
getLength returns : 6; should be 6
remove(2): returns 1; should be 1 (true)
remove(1): returns 1; should be 1 (true)
remove(6): returns 0; should be 0 (false)
The sorted list contains
Jerry Luke Sue Tom
getLength returns : 4; should be 4
clear:
isEmpty: returns 1; should be 1 (true)
Attempt an illegal retrieval from position 6:
Precondition Violated Exception: getEntry() called with an empty list or invalid
position.
Fin
*/
PrecondViolatedExcep.cpp
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 7-6.
@file PrecondViolatedExcep.cpp */
#include "PrecondViolatedExcep.h"
PrecondViolatedExcep::PrecondViolatedExcep(const string& message) : logic_error("Precondition Violated Exception: " + message)
{
} // end constructor
// End of implementation file.
ANSWER I FOUND / WHERE SHOULD THIS BE IN THE PROGRAM??
#include "SortedListHasA.h" // ADT sorted list operations
#include <iostream>
#include <string>
#include <limits>
using namespace std;
string getInput(string message)
{
cout << message;
string temp;
getline(cin, temp);
return temp;
}
// Gets input for a score
int getScore()
{
string temp = getInput("Enter a score (negative to quit): ");
int value;
try
{
value = stoi(temp);
}
catch(...) {
// std::stoi failed, so clear up the stream and try again
cout << "You did not enter a valid integer ";
return getScore();
}
return value;
}
template<typename T>
void printDescending(const SortedListHasA<T>& L, string label = "")
{
cout << label;
for(int i = L.getLength(); i >= 1; --i)
cout << L.getEntry(i) << ' ';
cout << endl;
}
int max(const SortedListHasA<int>& L)
{
if(L.getLength() == 0)
return -1;
int max = L.getEntry(1);
for(int i = 2; i <= L.getLength(); ++i)
{
auto entry = L.getEntry(i);
if(entry > max)
max = entry;
}
return max;
}
// Main execution point
int main(int argc, char *argv[])
{
SortedListHasA<int> list;
int newScore = getScore();
while(newScore >= 0)
{
list.insertSorted(newScore);
printDescending(list, "High Scores: ");
newScore = getScore();
}
cout << "Good bye! The highest score was " << max(list) << endl;
return 0;
}
/*
void displayList(SortedListInterface<string>* listPtr)
{
cout << "The sorted list contains " << endl;
for (int pos = 1; pos <= listPtr->getLength(); pos++)
{
cout << listPtr->getEntry(pos) << " ";
} // end for
cout << endl << endl;
} // end displayList
void copyConstructorTester()
{
SortedListHasA<string> list;
string items[] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
list.insertSorted(items[i]);
}
displayList(&list);
SortedListHasA<string> copyOfList(list);
cout << "Copy of list: ";
displayList(©OfList);
cout << "The copied list: ";
displayList(&list);
} // end copyConstructorTester
void sortedListTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
cout << " Test isEmpty with an empty list:" << endl;
if (listPtr->isEmpty())
cout << "OK" << endl;
else
cout << "isEmpty() error" << endl;
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
// display the list
cout << "List should contain Brent, Donna, " <<
"Jerry, Luke, Sue, Tom" << endl;
cout << " List actually contains:" << endl;
displayList(listPtr);
cout << endl;
// test getPosition
cout << " Test getPosition: " << endl;
// These names are in the list
cout << "Brent is at position " << listPtr->getPosition(brent) << endl;
cout << "Donna is at position " << listPtr->getPosition(donna) << endl;
cout << "Jerry is at position " << listPtr->getPosition(jerry) << endl;
cout << "Luke is at position " << listPtr->getPosition(luke) << endl;
cout << "Sue is at position " << listPtr->getPosition(sue) << endl;
cout << "Tom is at position " << listPtr->getPosition(tom) << endl;
// These names are not in the list
string missy = "Missy";
cout << "Missy belongs at position " << listPtr->getPosition(missy) << endl;
string zeke = "Zeke";
cout << "Zeke belongs at position " << listPtr->getPosition(zeke) << endl;
string aaron = "Aaron";
cout << "Aaron belongs at position " << listPtr->getPosition(aaron) << endl;
// test getLength and getEntry
cout << " Test getLength and getEntry: " << endl;
cout << " List has " << listPtr->getLength() << " entries, as follows: " << endl;
for (int i = 1; i <= listPtr->getLength(); i++)
cout << i << ": " << listPtr->getEntry(i) << endl;
// test remove
cout << " Test remove: " << endl;
// remove first entry
cout << " Removing first item (Brent): " << listPtr->removeSorted(brent) << "; should be 1 (true)" << endl;
cout << " After removing Brent, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// remove interior
cout << " Removing interior item (Luke): " << listPtr->removeSorted(luke) << "; should be 1 (true)" << endl;
cout << " After removing Luke, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// remove last
cout << &" Removing last item (Tom): " [ listPtr->removeSorted(tom)] << "; should be 1 (true)" << endl;
cout << " After removing last item, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
cout << " Removing a missing item (Brent): " << listPtr->removeSorted(brent) << "; should be 0 (false)" << endl;
cout << " Removing a missing item (Luke): " << listPtr->removeSorted(luke) << "; should be 0 (false)" << endl;
cout << " Removing a missing item (Tom): " << listPtr->removeSorted(tom) << "; should be 0 (false)" << endl;
cout << " The list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// test clear()
cout << " Test clear(): " << endl;
listPtr->clear();
if (listPtr->isEmpty())
cout << " The list is empty after invoking clear()." << endl;
else
cout << " clear() error" << endl;
} // end sortedListTester
void listOpsTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
displayList(listPtr);
cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 0 (false)" << endl;
cout << "getLength returns : " << listPtr->getLength() << "; should be 6" << endl;
cout << "remove(2): returns " << listPtr->remove(2) << "; should be 1 (true)" << endl;
cout << "remove(1): returns " << listPtr->remove(1) << "; should be 1 (true)" << endl;
cout << "remove(6): returns " << listPtr->remove(6) << "; should be 0 (false)" << endl;
displayList(listPtr);
cout << "getLength returns : " << listPtr->getLength() << "; should be 4" << endl;
cout << "clear: " << endl;
listPtr->clear();
cout << "isEmpty: returns " << listPtr->isEmpty() << "; should be 1 (true)" << endl;
try
{
cout << "Attempt an illegal retrieval from position 6: " << endl;
listPtr->getEntry(6);
}
catch(PrecondViolatedExcep e)
{
cout << e.what() << endl;
} // end try/catch
} // end listOpsTester
int main()
{
copyConstructorTester();
SortedListInterface<string>* listPtr = new SortedListHasA<string>();
cout << "Testing the Link-Based Sorted List:" << endl;
sortedListTester(listPtr);
cout << "======================================" << endl;
cout << " Testing the List Operations:" << endl;
listOpsTester(listPtr);
return 0;
} // end main
*/
/*
Adding zero
Adding one
Adding two
Adding three
Adding four
Adding five
The sorted list contains
five four one three two zero
Copy of list: The sorted list contains
five four one three two zero
The copied list: The sorted list contains
five four one three two zero
Testing the Link-Based Sorted List:
Test isEmpty with an empty list:
OK
List should contain
Brent, Donna, Jerry, Luke, Sue, Tom
List actually contains:
The sorted list contains
Brent Donna Jerry Luke Sue Tom
Test getPosition:
Brent is at position 1
Donna is at position 2
Jerry is at position 3
Luke is at position 4
Sue is at position 5
Tom is at position 6
Missy belongs at position -5
Zeke belongs at position -7
Aaron belongs at position -1
Test getLength and getEntry:
List has 6 entries, as follows:
1: Brent
2: Donna
3: Jerry
4: Luke
5: Sue
6: Tom
Test remove:
Removing first item (Brent): 1; should be 1 (true)
After removing Brent, list contains 5 items, as follows:
The sorted list contains
Donna Jerry Luke Sue Tom
Removing interior item (Luke): 1; should be 1 (true)
After removing Luke, list contains 4 items, as follows:
The sorted list contains
Donna Jerry Sue Tom
Removing last item (Tom): ; should be 1 (true)
After removing last item, list contains 3 items, as follows:
The sorted list contains
Donna Jerry Sue
Removing a missing item (Brent): 0; should be 0 (false)
Removing a missing item (Luke): 0; should be 0 (false)
Removing a missing item (Tom): 0; should be 0 (false)
The list contains 3 items, as follows:
The sorted list contains
Donna Jerry Sue
Test clear():
The list is empty after invoking clear().
======================================
Testing the List Operations:
The sorted list contains
Brent Donna Jerry Luke Sue Tom
isEmpty: returns 0; should be 0 (false)
getLength returns : 6; should be 6
remove(2): returns 1; should be 1 (true)
remove(1): returns 1; should be 1 (true)
remove(6): returns 0; should be 0 (false)
The sorted list contains
Jerry Luke Sue Tom
getLength returns : 4; should be 4
clear:
isEmpty: returns 1; should be 1 (true)
Attempt an illegal retrieval from position 6:
Precondition Violated Exception: getEntry() called with an empty list or invalid position.
*/
Explanation / Answer
//main.cpp
#include <iostream>
#include <string>
#include "LinkedSortedList.h" // ADT list operations
using namespace std;
void displayList(SortedListInterface<string>* listPtr)
{
cout << "The sorted list contains " << endl;
for (int pos = 1; pos <= listPtr->getLength(); pos++)
{
cout << listPtr->getEntry(pos) << " ";
} // end for
cout << endl << endl;
} // end displayList
void copyConstructorTester()
{
LinkedSortedList<string> list;
string items[] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
list.insertSorted(items[i]);
}
displayList(&list);
LinkedSortedList<string> copyOfList(list);
cout << "Copy of list: ";
displayList(©OfList);
cout << "The copied list: ";
displayList(&list);
cout << "calling destructor" << endl;
} // end copyConstructorTester
void sortedListTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
cout << " Test isEmpty with an empty list:" << endl;
if (listPtr->isEmpty())
cout << "OK" << endl;
else
cout << "isEmpty() error" << endl;
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
// display the list
cout << "List should contain Brent, Donna, " <<
"Jerry, Luke, Sue, Tom" << endl;
cout << " List actually contains:" << endl;
displayList(listPtr);
cout << endl;
// test getPosition
cout << " Test getPosition: " << endl;
// These names are in the list
cout << "Brent is at position " << listPtr->getPosition(brent) << endl;
cout << "Donna is at position " << listPtr->getPosition(donna) << endl;
cout << "Jerry is at position " << listPtr->getPosition(jerry) << endl;
cout << "Luke is at position " << listPtr->getPosition(luke) << endl;
cout << "Sue is at position " << listPtr->getPosition(sue) << endl;
cout << "Tom is at position " << listPtr->getPosition(tom) << endl;
// These names are not in the list
string missy = "Missy";
cout << "Missy belongs at position "
<< listPtr->getPosition(missy) << endl;
string zeke = "Zeke";
cout << "Zeke belongs at position "
<< listPtr->getPosition(zeke) << endl;
string aaron = "Aaron";
cout << "Aaron belongs at position "
<< listPtr->getPosition(aaron) << endl;
// test getLength and getEntry
cout << " Test getLength and getEntry: " << endl;
cout << " List has " << listPtr->getLength()
<< " entries, as follows: " << endl;
for (int i = 1; i <= listPtr->getLength(); i++)
cout << i << ": " << listPtr->getEntry(i) << endl;
// test remove
cout << " Test remove: " << endl;
// remove first entry
cout << " Removing first item (Brent): " << listPtr->removeSorted(brent)
<< "; should be 1 (true)" << endl;
cout << " After removing Brent, list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// remove interior
cout << " Removing interior item (Luke): " << listPtr->removeSorted(luke)
<< "; should be 1 (true)" << endl;
cout << " After removing Luke, list contains "
<< listPtr->getLength() << " items, as follows:" << endl;
displayList(listPtr);
// remove last
cout << &" Removing last item (Tom): " [ listPtr->removeSorted(tom)]
<< "; should be 1 (true)" << endl;
cout << " After removing last item, list contains "
<< listPtr->getLength() << " items, as follows:" << endl;
displayList(listPtr);
cout << " Removing a missing item (Brent): "
<< listPtr->removeSorted(brent) << "; should be 0 (false)" << endl;
cout << " Removing a missing item (Luke): " << listPtr->removeSorted(luke)
<< "; should be 0 (false)" << endl;
cout << " Removing a missing item (Tom): " << listPtr->removeSorted(tom)
<< "; should be 0 (false)" << endl;
cout << " The list contains " << listPtr->getLength()
<< " items, as follows:" << endl;
displayList(listPtr);
// test clear()
cout << " Test clear(): " << endl;
listPtr->clear();
if (listPtr->isEmpty())
cout << " The list is empty after invoking clear()." << endl;
else
cout << " clear() error" << endl;
} // end sortedListTester
void listOpsTester(SortedListInterface<string>* listPtr)
{
string luke = "Luke";
string brent = "Brent";
string donna = "Donna";
string tom = "Tom";
string sue = "Sue";
string jerry = "Jerry";
listPtr->insertSorted(luke);
listPtr->insertSorted(brent);
listPtr->insertSorted(donna);
listPtr->insertSorted(tom);
listPtr->insertSorted(sue);
listPtr->insertSorted(jerry);
displayList(listPtr);
cout << "isEmpty: returns " << listPtr->isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getLength returns : " << listPtr->getLength()
<< "; should be 6" << endl;
cout << "remove(2): returns " << listPtr->remove(2)
<< "; should be 1 (true)" << endl;
cout << "remove(1): returns " << listPtr->remove(1)
<< "; should be 1 (true)" << endl;
cout << "remove(6): returns " << listPtr->remove(6)
<< "; should be 0 (false)" << endl;
displayList(listPtr);
cout << "getLength returns : " << listPtr->getLength()
<< "; should be 4" << endl;
cout << "clear: " << endl;
listPtr->clear();
cout << "isEmpty: returns " << listPtr->isEmpty()
<< "; should be 1 (true)" << endl;
try
{
cout << "Attempt an illegal retrieval from position 6: " << endl;
listPtr->getEntry(6);
}
catch(PrecondViolatedExcep e)
{
cout << e.what() << endl;
} // end try/catch
//Test newly added methods
cout << " Test display forward and display backward ";
LinkedSortedList<string> testList;
testList.insertSorted(luke);
testList.insertSorted(brent);
testList.insertSorted(donna);
testList.insertSorted(tom);
testList.insertSorted(sue);
testList.insertSorted(jerry);
testList.displayForward();
testList.displayBackward();
testList.clear();
cout << " Test that display forward and display backward "
<< "display only a new line for an empty list. ";
testList.displayForward();
testList.displayBackward();
cout << "End of listOpsTester" << endl;
} // end listOpsTester
int main()
{
copyConstructorTester();
cout << "end copyConstructorTester" << endl;
SortedListInterface<string>* listPtr = new LinkedSortedList<string>();
cout << "created listPtr" << endl;
cout << "Testing the Link-Based Sorted List:" << endl;
sortedListTester(listPtr);
cout << "======================================" << endl;
cout << " Testing the List Operations:" << endl;
listOpsTester(listPtr);
return 0;
} // end main
-------------------------------------------------------------------
//LinkedSortedList.cpp
#include "LinkedSortedList.h" // Header file
#include <cassert>
template<class ItemType>
LinkedSortedList<ItemType>::LinkedSortedList()
: headPtr(nullptr), itemCount(0)
{
} // end default constructor
template<class ItemType>
LinkedSortedList<ItemType>::LinkedSortedList(
const LinkedSortedList<ItemType>& aList)
{
headPtr = copyChain(aList.headPtr);
itemCount = aList.itemCount;
} // end copy constructor
/*Modified from original to include a pointer to the previous link on the chain
to link the next one to */
template<class ItemType>
Node<ItemType>* LinkedSortedList<ItemType>::copyChain(
const Node<ItemType>* origChainPtr,
Node<ItemType>* prevPtr)
{
Node<ItemType>* copiedChainPtr = nullptr;
if (origChainPtr != nullptr)
{
// Build new chain from given one
copiedChainPtr = new Node<ItemType>(origChainPtr->getItem());
copiedChainPtr->setPrev(prevPtr);
copiedChainPtr->setNext(copyChain(origChainPtr->getNext(), copiedChainPtr));
} // end if
return copiedChainPtr;
} // end copyChain
template<class ItemType>
LinkedSortedList<ItemType>::~LinkedSortedList()
{
clear();
} // end destructor
template<class ItemType>
void LinkedSortedList<ItemType>::insertSorted(const ItemType& newEntry)
{
Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry);
Node<ItemType>* prevPtr = getNodeBefore(newEntry);
if (isEmpty() || (prevPtr == nullptr)) // Add at beginning
{
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
}
else // Add after node before
{
Node<ItemType>* aftPtr = prevPtr->getNext();
newNodePtr->setNext(aftPtr);
newNodePtr->setPrev(prevPtr);
if (aftPtr)
aftPtr->setPrev(newNodePtr);
prevPtr->setNext(newNodePtr);
} // end if
itemCount++;
} // end insertSorted
template<class ItemType>
bool LinkedSortedList<ItemType>::removeSorted(const ItemType& anEntry)
{
bool ableToDelete = false;
if (!isEmpty())
{
Node<ItemType>* nodeToRemovePtr = headPtr;
Node<ItemType>* prevPtr = getNodeBefore(anEntry);
if (prevPtr != nullptr)
nodeToRemovePtr = prevPtr->getNext();
ableToDelete = (nodeToRemovePtr != nullptr) &&
(anEntry == nodeToRemovePtr->getItem());
if (ableToDelete)
{
Node<ItemType>* aftPtr = nodeToRemovePtr->getNext();
if (nodeToRemovePtr == headPtr)
{
// Delete the first node in the chain
aftPtr->setPrev(nullptr);
headPtr = aftPtr;
}
else
{
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(aftPtr);
// Connect the aftptr, if it exists
if (aftPtr)
aftPtr->setPrev(prevPtr);
} // end if
// Return deleted node to system
nodeToRemovePtr->setNext(nullptr);
delete nodeToRemovePtr;
nodeToRemovePtr = nullptr;
itemCount--; // Decrease count of entries
} // end if
} // end if
return ableToDelete;
} // end removeSorted
template<class ItemType>
int LinkedSortedList<ItemType>::getPosition(const ItemType& anEntry) const
{
int position = 1;
Node<ItemType>* curPtr = headPtr;
while ( (curPtr != nullptr) && (anEntry > curPtr->getItem()) )
{
curPtr = curPtr->getNext();
position++;
} // end while
if ( (curPtr == nullptr) || (anEntry != curPtr->getItem()) )
position = -position;
return position;
} // end getPosition
// List operations:
template<class ItemType>
bool LinkedSortedList<ItemType>::remove(int position)
{
bool ableToDelete = (position >= 1) && (position <= itemCount);
if (ableToDelete)
{
Node<ItemType>* curPtr = nullptr;
if (position == 1)
{
// Delete the first node in the chain
curPtr = headPtr; // save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<ItemType>* prevPtr = getNodeAt(position - 1);
// Point to node to delete
curPtr = prevPtr->getNext();
/* Disconnect indicated node from chain by connecting the
prior node with the one after and the after node with the one prior,
if it exists */
Node<ItemType>* aftPtr = curPtr->getNext();
prevPtr->setNext(aftPtr);
if (aftPtr)
aftPtr->setPrev(prevPtr);
} // end if
// Return deleted node to system
curPtr->setNext(nullptr);
delete curPtr;
curPtr = nullptr;
itemCount--; // Decrease count of entries
} // end if
return ableToDelete;
} // end remove
template<class ItemType>
void LinkedSortedList<ItemType>::clear()
{
while (!isEmpty())
remove(1);
} // end clear
template<class ItemType>
ItemType LinkedSortedList<ItemType>::getEntry(int position) const
// throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
Node<ItemType>* nodePtr = getNodeAt(position);
return nodePtr->getItem();
}
else
{
string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
} // end if
} // end getEntry
template<class ItemType>
bool LinkedSortedList<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
int LinkedSortedList<ItemType>::getLength() const
{
return itemCount;
} // end getLength
template<class ItemType>
Node<ItemType>* LinkedSortedList<ItemType>::getNodeBefore(
const ItemType& anEntry) const
{
Node<ItemType>* curPtr = headPtr;
Node<ItemType>* prevPtr = nullptr;
while ( (curPtr != nullptr) && (anEntry > curPtr->getItem()) )
{
prevPtr = curPtr;
curPtr = curPtr->getNext();
} // end while
return prevPtr;
} // end getNodeBefore
template<class ItemType>
Node<ItemType>* LinkedSortedList<ItemType>::getNodeAt(int position) const
{
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<ItemType>* curPtr = headPtr;
for (int skip = 1; skip < position; skip++)
curPtr = curPtr->getNext();
return curPtr;
} // end getNodeAt
template<class ItemType>
void LinkedSortedList<ItemType>::displayForward(ostream& w) //w default=cout
{
if (itemCount > 0)
{
Node<ItemType>* traveller = headPtr;
while (traveller)
{
w << traveller->getItem() << " ";
traveller = traveller->getNext();
}
}
w << endl;
}
template<class ItemType>
void LinkedSortedList<ItemType>::displayBackward(ostream& w) //w default=cout
{
if (itemCount > 0)
{
//get the last node in the chain
Node<ItemType>* traveller = getNodeAt(itemCount);
while (traveller)
{
w << traveller->getItem() << " ";
traveller = traveller->getPrev();
}
}
w << endl;
}
--------------------------------------------------------
//LinkedSortedList.h
#ifndef _LINKED_SORTED_LIST
#define _LINKED_SORTED_LIST
#include "SortedListInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class LinkedSortedList : public SortedListInterface<ItemType>
{
private:
Node<ItemType>* headPtr; // Pointer to first node in the chain
int itemCount; // Current count of list items
Node<ItemType>* getNodeBefore(const ItemType& anEntry) const;
Node<ItemType>* getNodeAt(int position) const;
Node<ItemType>* copyChain(const Node<ItemType>* origChainPtr,
Node<ItemType>* prevPtr=nullptr);
public:
LinkedSortedList();
LinkedSortedList(const LinkedSortedList<ItemType>& aList);
virtual ~LinkedSortedList();
void insertSorted(const ItemType& newEntry);
bool removeSorted(const ItemType& anEntry);
int getPosition(const ItemType& newEntry) const;
// The following methods are the same as given in ListInterface:
bool isEmpty() const;
int getLength() const;
bool remove(int position);
void clear();
ItemType getEntry(int position) const; // throw(PrecondViolatedExcep)
void displayForward(ostream& w=cout);
void displayBackward(ostream& w=cout);
}; // end LinkedSortedList
#include "LinkedSortedList.cpp"
#endif
----------------------------------------------------------
//Node.cpp
#include "Node.h"
#include <cstddef>
template<class ItemType>
Node<ItemType>::Node() : next(nullptr), prev(nullptr)
{
} // end default constructor
template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr),
prev(nullptr)
{
} // end constructor
template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr,
Node<ItemType>* prevNodePtr) : item(anItem), next(nextNodePtr), prev(prevNodePtr)
{
} // end constructor
template<class ItemType>
void Node<ItemType>::setItem(const ItemType& anItem)
{
item = anItem;
} // end setItem
template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)
{
next = nextNodePtr;
} // end setNext
template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
return item;
} // end getItem
template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
return next;
} // end getNext
template<class ItemType>
void Node<ItemType>::setPrev(Node<ItemType>* prevNodePtr)
{
this->prev = prevNodePtr;
}
template<class ItemType>
Node<ItemType>* Node<ItemType>::getPrev() const
{
return prev;
}
----------------------------------------------------------------------
//Node.h
#ifndef _NODE
#define _NODE
template<class ItemType>
class Node
{
private:
ItemType item; // A data item
Node<ItemType>* next; // Pointer to next node
Node<ItemType>* prev; // Pointer to previous node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr,
Node<ItemType>* prevNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node<ItemType>* nextNodePtr);
ItemType getItem() const ;
Node<ItemType>* getNext() const ;
void setPrev(Node<ItemType>* prevNodePtr);
Node<ItemType>* getPrev() const ;
}; // end Node
#include "Node.cpp"
#endif
---------------------------------------------------------------
//PrecondViolatedExcep.cpp
#include "PrecondViolatedExcep.h"
PrecondViolatedExcep::PrecondViolatedExcep(const string& message):
logic_error("Precondition Violated Exception: " + message)
{
}
--------------------------------------------------------------
//PrecondViolatedExcep.h
#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP
#include <stdexcept>
#include <string>
using namespace std;
class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif
--------------------------------------------------------------
//SortedListInterface.h
#ifndef SORTED_LIST_INTERFACE
#define SORTED_LIST_INTERFACE
template<class ItemType>
class SortedListInterface
{
public:
virtual void insertSorted(const ItemType& newEntry) = 0;
virtual bool removeSorted(const ItemType& anEntry) = 0;
virtual int getPosition(const ItemType& anEntry) const = 0;
//Sees whether this list is empty.
virtual bool isEmpty() const = 0;
//Gets the current number of entries in this list.
virtual int getLength() const = 0;
//Removes the entry at a given position from this list.
virtual bool remove(int position) = 0;
// Removes all entries from this list.
virtual void clear() = 0;
//Gets the entry at the given position in this list.
virtual ItemType getEntry(int position) const = 0;
}; // end SortedListInterface
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.