Question: Consider the implementation of the List ADT using Singly Linked List. Add a member function (to the List class) called recursivePrintForwardReverseOrders that prints the contents

Consider the implementation of the List ADT using Singly Linked List. Add a member

function (to the List class) called recursivePrintForwardReverseOrders that prints the contents of the list

in a recursive fashion in both the forward order and reverse order.

For example, if the contents of the List are: 10 --> 4 --> 8 --> 12 --> 9, the recursive member function

should print the List as follows:

10 4 8 12 9

9 12 8 4 10

Note that both the forward and reverse orders should be printed through an invocation of the

recursivePrintForwardReverseOrders member function on the List object called from the main function.

You are free to choose the parameter(s) that need to be passed to the

recursivePrintForwardReverseOrders function. But, you are not supposed to pass more than three

parameter(s). A suggestion for the parameter to pass is given in the main function of the code posted for

Question 1.

To test your code (and take screenshot), create a List of at least 5 elements and then call the

recursivePrintForwardReverseOrders function on this List object by passing a pointer to the first node in

the Linked List as an argument, as shown in the main function of the Singly Linked List code for

Question 1.

#include #include //srand, rand #include using namespace std;

// implementing the dynamic List ADT using Linked List // operations to be implemented: read, Modify, delete, isEmpty, insert, countElements

class Node{ private: int data; Node* nextNodePtr; public: Node(){} void setData(int d){ data = d; } int getData(){ return data; } void setNextNodePtr(Node* nodePtr){ nextNodePtr = nodePtr; } Node* getNextNodePtr(){ return nextNodePtr; } };

class List{

private: Node *headPtr; public: List(){ headPtr = new Node(); headPtr->setNextNodePtr(0); } bool isEmpty(){ if (headPtr->getNextNodePtr() == 0) return true; return false; } Node* getHeadNodePtr(){ return headPtr; } void insert(int data){ Node* currentNode = headPtr->getNextNodePtr(); Node* prevNode = headPtr; while (currentNode != 0){ prevNode = currentNode; currentNode = currentNode->getNextNodePtr(); } Node* newNode = new Node(); newNode->setData(data); newNode->setNextNodePtr(0); prevNode->setNextNodePtr(newNode); } void insertAtIndex(int insertIndex, int data){ Node* currentNode = headPtr->getNextNodePtr(); Node* prevNode = headPtr; int index = 0; while (currentNode != 0){ if (index == insertIndex) break; prevNode = currentNode; currentNode = currentNode->getNextNodePtr(); index++; } Node* newNode = new Node(); newNode->setData(data); newNode->setNextNodePtr(currentNode); prevNode->setNextNodePtr(newNode); } int read(int readIndex){ Node* currentNode = headPtr->getNextNodePtr(); Node* prevNode = headPtr; int index = 0; while (currentNode != 0){ if (index == readIndex) return currentNode->getData(); prevNode = currentNode; currentNode = currentNode->getNextNodePtr(); index++; } return -1; // an invalid value indicating // index is out of range } void modifyElement(int modifyIndex, int data){ Node* currentNode = headPtr->getNextNodePtr(); Node* prevNode = headPtr; int index = 0; while (currentNode != 0){ if (index == modifyIndex){ currentNode->setData(data); return; } prevNode = currentNode; currentNode = currentNode->getNextNodePtr(); index++; } } void deleteElement(int deleteIndex){ Node* currentNode = headPtr->getNextNodePtr(); Node* prevNode = headPtr; Node* nextNode = headPtr; int index = 0; while (currentNode != 0){ if (index == deleteIndex){ nextNode = currentNode->getNextNodePtr(); break; } prevNode = currentNode; currentNode = currentNode->getNextNodePtr(); index++; } prevNode->setNextNodePtr(nextNode); } int countList(){ Node* currentNode = headPtr->getNextNodePtr(); int numElements = 0; while (currentNode != 0){ numElements++; currentNode = currentNode->getNextNodePtr(); } return numElements; }

void IterativePrint(){ Node* currentNode = headPtr->getNextNodePtr(); while (currentNode != 0){ cout << currentNode->getData() << " "; currentNode = currentNode->getNextNodePtr(); } cout << endl; } // add the code to the member function // RecursivePrintForwardReverseOrders(Node*) };

int main(){

int listSize; cout << "Enter the number of elements you want to insert: "; cin >> listSize; List integerList; // Create an empty list srand(time(NULL)); int maxValue; cout << "Enter the maximum value for an element: "; cin >> maxValue;

for (int i = 0; i < listSize; i++){ int value = rand() % maxValue; integerList.insertAtIndex(i, value); } cout << "Contents of the List (IterativePrint): "; integerList.IterativePrint(); cout << endl;

cout << "Contents of the List (Forward and Reverse Orders) " << endl; Node* firstNodePtr = integerList.getHeadNodePtr()->getNextNodePtr(); integerList.recursivePrintForwardReverseOrders(firstNodePtr); return 0; }

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!