Change the driver file the main cpp so that it asks the user

Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revprint2 function that is in the code!

LinkListImp.cpp

//----- List.cpp -----
#include <iostream>
using namespace std;
#include \"linkedlist.h\"
//-- Definition of the class constructor
List::List()
: first(0), mySize(0)
{ }

//-- Definition of the copy constructor
List::List(const List & origList)
{
mySize = origList.mySize;
first = 0;
if (mySize == 0) return;
List::NodePointer origPtr, lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != 0)
{
  lastPtr->next = new Node(origPtr->data);
  origPtr = origPtr->next;
  lastPtr = lastPtr->next;
}
}
//-- Definition of the destructor
inline List::~List()
{
List::NodePointer prev = first,
  ptr;
while (prev != 0)
{
  ptr = prev->next;
  delete prev;
  prev = ptr;
}
}

// Definition of empty()
bool List::empty()
{
return mySize == 0;
}
//-- Definition of the assignment operator
const List & List::operator=(const List & rightSide)
{
mySize = rightSide.mySize;
first = 0;
if (mySize == 0) return *this;
if (this != &rightSide)
{
  this->~List();
  List::NodePointer origPtr, lastPtr;
  first = new Node(rightSide.first->data); // copy first node
  lastPtr = first;
  origPtr = rightSide.first->next;
  while (origPtr != 0)
  {
   lastPtr->next = new Node(origPtr->data);
   origPtr = origPtr->next;
   lastPtr = lastPtr->next;
  }
}
return *this;
}

//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
  cerr << \"Illegal location to insert -- \" << index << endl;
  return;
}
mySize++;
List::NodePointer newPtr = new Node(dataVal),
  predPtr = first;
if (index == 0)
{
  newPtr->next = first;
  first = newPtr;
}
else
{
  for (int i = 1; i < index; i++)
   predPtr = predPtr->next;
  newPtr->next = predPtr->next;
  predPtr->next = newPtr;
}
}
//-- Definition of erase()
void List::erase(int index)
{
if (index < 0 || index >= mySize)
{
  cerr << \"Illegal location to delete -- \" << index << endl;
  return;
}
mySize--;
List::NodePointer ptr,
  predPtr = first;
if (index == 0)
{
  ptr = first;
  first = ptr->next;
  delete ptr;
}
else
{
  for (int i = 1; i < index; i++)
   predPtr = predPtr->next;
  ptr = predPtr->next;
  predPtr->next = ptr->next;
  delete ptr;
}
}
//-- Definition of search()
int List::search(ElementType dataVal)
{
int loc;
List::NodePointer tempP = first;
for (loc = 0; loc < mySize; loc++)
  if (tempP->data == dataVal)
   return loc;
  else
   tempP = tempP->next;
return -1;
}
//-- Definition of display()
void List::display(ostream & out) const
{
NodePointer ptr = first;
while (ptr != 0)
{
  out << ptr->data << \" \";
  ptr = ptr->next;
}
}
void List::revPrint(ostream & out)
{

reverse();
display(out);
reverse();
}
void List::revPrint2(ostream &out, NodePointer ptr)
{
out << \"Inside revPrint2\ \";
static List::NodePointer point = first;

out << point->data << endl;
if (point != NULL)
{
  out << \"Inside if statement\ \";
  revPrint2(out, point->next);
  out << point->data;
}
}

//-- Definition of the output operator
ostream & operator<<(ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//-- Definition of nodeCount()
int List::nodeCount()
{ // or simply, { return mySize; }
int count = 0;
List::NodePointer ptr = first;
while (ptr != 0)
{
  count++;
  ptr = ptr->next;
}
return count;
}

//-- Definition of reverse()
void List::reverse()
{

}
//-- Definition of ascendingOrder()
bool List::ascendingOrder()
{
if (mySize <= 1)
  //empty or one element list
  return true;
//else
NodePointer prevP = first,
  tempP = first->next;
while (tempP != 0 && prevP->data <= tempP->data)
{
  prevP = tempP;
  tempP = tempP->next;
}
if (tempP != 0)
  return false;
// else
return true;
}

linkedlist.cpp

#include<iostream>

using namespace std;

#include \"linkedlist.h\"

int main()
{
List mylist;
for (int i = 0; i < 6; i++)
  mylist.insert(i * 2, i);

mylist.display(cout);
cout << endl;
mylist.revPrint(cout);
cout << endl;
cout << mylist;

}

linkedlist.h

//----- List.h -----
#ifndef LINKEDLIST
#define LINKEDLIST
#include <iostream>
using namespace std;
typedef int ElementType;
class List
{
private:
class Node
{
public:
  ElementType data;
  Node * next;
  
  Node()
   : next(0)
   { }
  
  Node(ElementType dataValue)
   : data(dataValue), next(0)
   { }
}; //--- end of Node class

typedef Node * NodePointer;

public:
//------ List OPERATIONS
List();
List(const List & origList);
~List();
const List & operator=(const List & rightSide);
bool List::empty();
void insert(ElementType dataVal, int index);
void erase(int index);
int search(ElementType dataVal);
void display(ostream & out) const;
void revPrint(ostream & out);
void revPrint2(ostream &out, NodePointer ptr = NULL);
int nodeCount();
void reverse();
bool ascendingOrder();
private:
//------ DATA MEMBERS
NodePointer first ;
int mySize;
}; //--- end of List class
ostream & operator<<(ostream & out, const List & aList);
//istream & operator>>(istream & in, List & aList);
#endif

Solution

////----- List.cpp -----

//added reverse functionality

#include <iostream>
using namespace std;
#include \"linkedlist.h\"
//-- Definition of the class constructor
List::List()
   : first(0), mySize(0)
{ }
//-- Definition of the copy constructor
List::List(const List & origList)
{
   mySize = origList.mySize;
   first = 0;
   if (mySize == 0) return;
   List::NodePointer origPtr, lastPtr;
   first = new Node(origList.first->data); // copy first node
   lastPtr = first;
   origPtr = origList.first->next;
   while (origPtr != 0)
   {
       lastPtr->next = new Node(origPtr->data);
       origPtr = origPtr->next;
       lastPtr = lastPtr->next;
   }
}
//-- Definition of the destructor
inline List::~List()
{
   List::NodePointer prev = first,
       ptr;
   while (prev != 0)
   {
       ptr = prev->next;
       delete prev;
       prev = ptr;
   }
}
// Definition of empty()
bool List::empty()
{
   return mySize == 0;
}
//-- Definition of the assignment operator
const List & List::operator=(const List & rightSide)
{
   mySize = rightSide.mySize;
   first = 0;
   if (mySize == 0) return *this;
   if (this != &rightSide)
   {
       this->~List();
       List::NodePointer origPtr, lastPtr;
       first = new Node(rightSide.first->data); // copy first node
       lastPtr = first;
       origPtr = rightSide.first->next;
       while (origPtr != 0)
       {
           lastPtr->next = new Node(origPtr->data);
           origPtr = origPtr->next;
           lastPtr = lastPtr->next;
       }
   }
   return *this;
}
//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
   if (index < 0 || index > mySize)
   {
       cerr << \"Illegal location to insert -- \" << index << endl;
       return;
   }
   mySize++;
   List::NodePointer newPtr = new Node(dataVal),
       predPtr = first;
   if (index == 0)
   {
       newPtr->next = first;
       first = newPtr;
   }
   else
   {
       for (int i = 1; i < index; i++)
           predPtr = predPtr->next;
       newPtr->next = predPtr->next;
       predPtr->next = newPtr;
   }
}
//-- Definition of erase()
void List::erase(int index)
{
   if (index < 0 || index >= mySize)
   {
       cerr << \"Illegal location to delete -- \" << index << endl;
       return;
   }
   mySize--;
   List::NodePointer ptr,
       predPtr = first;
   if (index == 0)
   {
       ptr = first;
       first = ptr->next;
       delete ptr;
   }
   else
   {
       for (int i = 1; i < index; i++)
           predPtr = predPtr->next;
       ptr = predPtr->next;
       predPtr->next = ptr->next;
       delete ptr;
   }
}
//-- Definition of search()
int List::search(ElementType dataVal)
{
   int loc;
   List::NodePointer tempP = first;
   for (loc = 0; loc < mySize; loc++)
       if (tempP->data == dataVal)
           return loc;
       else
           tempP = tempP->next;
   return -1;
}
//-- Definition of display()
void List::display(ostream & out) const
{
   NodePointer ptr = first;
   while (ptr != 0)
   {
       out << ptr->data << \" \";
       ptr = ptr->next;
   }
}
void List::revPrint(ostream & out)
{

   reverse();
   display(out);
   reverse();
}
void List::revPrint2(ostream &out, NodePointer ptr)
{
   out << \"Inside revPrint2\ \";
   static List::NodePointer point = first;
   out << point->data << endl;
   if (point != NULL)
   {
       out << \"Inside if statement\ \";
       revPrint2(out, point->next);
       out << point->data;
   }
}
//-- Definition of the output operator
ostream & operator<<(ostream & out, const List & aList)
{
   aList.display(out);
   return out;
}
//-- Definition of nodeCount()
int List::nodeCount()
{ // or simply, { return mySize; }
   int count = 0;
   List::NodePointer ptr = first;
   while (ptr != 0)
   {
       count++;
       ptr = ptr->next;
   }
   return count;
}
//-- Definition of reverse()
//added by chegg EA
void List::reverse()
{
   static List::NodePointer current,next,prev;
   prev = NULL;
   current = first;
  
   while (current != NULL)
   {
       next = current->next;
       current->next = prev;
       prev = current;
       current = next;
   }
   first= prev;
}
//-- Definition of ascendingOrder()
bool List::ascendingOrder()
{
   if (mySize <= 1)
       //empty or one element list
       return true;
   //else
   NodePointer prevP = first,
       tempP = first->next;
   while (tempP != 0 && prevP->data <= tempP->data)
   {
       prevP = tempP;
       tempP = tempP->next;
   }
   if (tempP != 0)
       return false;
   // else
   return true;
}

------------------------------------------------------------------------

// List.h

this file unchanged

---------------------------------------------------------------

//main.cpp

#include<iostream>
using namespace std;
#include \"linkedlist.h\"
int main()
{
   List mylist;
   int num;
   for (int i = 0; i < 6; i++)
   {
       //mylist.insert(i * 2, i);
       //added by chegg EA,ask for user input
       cin >> num;
       mylist.insert(num, i);
   }
   cout << \"mylist: \";
   mylist.display(cout);
   cout << endl;
   //mylist.revPrint(cout);
   //added by chegg EA,reverse the list
   mylist.reverse();
   //added by chegg EA, print the reversed list
  
   cout << endl;
   cout << \"After reversing the list: \"<<mylist;
}

-----------------------------------------------------

//output

6 5 9 10 3 12
mylist: 6 5 9 10 3 12

After reversing the list: 12 3 10 9 5 6

Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site