Question: 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

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 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

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 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

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!