Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

See this zip file for source: https://files.fm/u/uurrmm77 See problem below: (co

ID: 3603550 • Letter: S

Question

See this zip file for source: https://files.fm/u/uurrmm77

See problem below: (confusing a bit I know)

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. (See attached ZIP file.) 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.

Explanation / Answer

#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(&copyOfList);

   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