Question 1

Using the code for a singly linked list-based implementation of the List ADT, you will add a function called isUnique( ) to the List class such that the function will return true if all the integer elements in the List are unique and return false otherwise.

The main function is written in such a way that it will create a list (each of size listSize) of random integers in a range [1...maxValue] and will call the isUnique function to determine if the list is unique.

You will test your code with the following values for listSize and maxValue.

listSize: the last two digits of your J#. For example, if your J# is J00123456, then your listSize is 56. In case, the last two digits of your J# is a value less than 10, you will add 10 to that value and use the sum as the listSize. For example, if your J# is J00120005, then your listSize will be 10 + 05 = 15.

maxValue: the last four digits of your J#. For example, if your J# is J00123456, then your maxValue is 3456. In case, the last four digits of your J# is a value less than 1000, you will add 1000 to that value and use the sum as the maxValue. For example, if your J# is J00120005, then your maxValue will be 1000 + 0005 = 1005.

SinglyLinkedList

#include < iostream >
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 deleteIndex){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
Node* nextNodePtr = headPtr;

int index = 0;

while (currentNodePtr != 0){
if (index == deleteIndex){
nextNodePtr = currentNodePtr->getNextNodePtr();
break;
}

prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}

prevNodePtr->setNextNodePtr(nextNodePtr);
}

void IterativePrint(){
Node* currentNodePtr = headPtr->getNextNodePtr();
while (currentNodePtr != 0){
cout << currentNodePtr->getData() << " ";
currentNodePtr = currentNodePtr->getNextNodePtr();
}

cout << endl;
}
};

int main(){
int listSize;
cout << "Enter the number of elements you want to insert: ";
cin >> listSize;
List integerList; // Create an empty list

for (int i = 0; i < listSize; i++){
int value;
cout << "Enter element # " << i << " : ";
cin >> value;
integerList.insertAtIndex(i, value);
}

cout << "Contents of the List: ";
integerList.IterativePrint();
// to read an element at a particular index (before delete)

int readIndex;
cout << "Enter an index to read (before delete): ";
cin >> readIndex;
cout << "Value at " << readIndex << " is: " << integerList.read(readIndex) << endl;

// to delete an element at a particular index
int deleteIndex;
cout << "Enter an index to delete: ";
cin >> deleteIndex;
integerList.deleteElement(deleteIndex);
cout << "Contents of the List: ";
integerList.IterativePrint();

// to read an element at a particular index (after delete)
cout << "Enter an index to read (after delete): ";
cin >> readIndex;
cout << "Value at " << readIndex << " is: " << integerList.read(readIndex) << endl;

// to insert an element at a particular index
int insertIndex, insertValue;
cout << "Enter an index to insert: ";
cin >> insertIndex;
cout << "Enter a value to insert: ";
cin >> insertValue;
integerList.insertAtIndex(insertIndex, insertValue);

cout << "Contents of the List: ";
integerList.IterativePrint();

// to read an element at a particular index (after insert)
cout << "Enter an index to read (after insert): ";
cin >> readIndex;
cout << "Value at " << readIndex << " is: " << integerList.read(readIndex) << endl;

// to insert at the end of the list
cout << "Enter the element you want to insert at the end of the list: ";
cin >> insertValue;
integerList.insert(insertValue);
cout << "Contents of the List: ";
integerList.IterativePrint();
return 0;
}

Question 2

Consider the implementation of the List ADT using the code for Singly Linked List given provided to you. 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.

Question 3

For this question, you will develop the code to determine the nodes that have the maximum data and minimum data values in a singly linked list-based implementation of a List.

You will add a function to determine the addresses of the nodes with the maximum data and minimum data in the List and then print out the data of the corresponding nodes.

You should not make any changes in the code provided for the List class.

Test run your code with inputs for the list size as 10 and the range of values for the elements is [1...50] and capture the screenshot. Submission: .cpp files and screenshots of output.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.