Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Q1 - 20 pts) Consider the implementation of the List ADT using Singly Linked List given to you for this question. Add a member function

Q1 - 20 pts) Consider the implementation of the List ADT using Singly Linked List given to you for this question. 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 10 elements (with a maximum value of 100) 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. You need to submit the following as part of your answer for this question: (i) the complete code for the Node class, List class (including the recursivePrintForwardReverseOrders( ) function) and the main function. (ii) Snapshot of the execution of the code with at least 10 elements and maximum value of 100.

Code 1

#include #include #include #include #include #include

using namespace std;

// implementing the dynamic List ADT using Linked List

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); } Node* getHeadPtr(){ return headPtr; } bool isEmpty(){ if (headPtr->getNextNodePtr() == 0) return true; return false; } void insert(int data){ Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; while (currentNodePtr != 0){ prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr(); } Node* newNodePtr = new Node(); newNodePtr->setData(data); newNodePtr->setNextNodePtr(0); prevNodePtr->setNextNodePtr(newNodePtr); } void insertAtIndex(int insertIndex, int data){ Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; int index = 0; while (currentNodePtr != 0){ if (index == insertIndex) break; prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr(); index++; } Node* newNodePtr = new Node(); newNodePtr->setData(data); newNodePtr->setNextNodePtr(currentNodePtr); prevNodePtr->setNextNodePtr(newNodePtr); } int read(int readIndex){ Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; int index = 0; while (currentNodePtr != 0){ if (index == readIndex) return currentNodePtr->getData(); prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr(); index++; } return -1; // an invalid value indicating // index is out of range } void modifyElement(int modifyIndex, int data){ Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; int index = 0; while (currentNodePtr != 0){ if (index == modifyIndex){ currentNodePtr->setData(data); return; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr(); index++; } }

void deleteElement(int deleteData){ Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; Node* nextNodePtr = headPtr; while (currentNodePtr != 0){ if (currentNodePtr->getData() == deleteData){ nextNodePtr = currentNodePtr->getNextNodePtr(); break; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr(); } prevNodePtr->setNextNodePtr(nextNodePtr); } void IterativePrint(){ Node* currentNodePtr = headPtr->getNextNodePtr(); while (currentNodePtr != 0){ cout << currentNodePtr->getData() << " "; currentNodePtr = currentNodePtr->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.getHeadPtr()->getNextNodePtr(); integerList.recursivePrintForwardReverseOrders(firstNodePtr); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started