Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include using namespace std; // implementing the dynamic List ADT using Linked List class Node{ private : int data; Node*

image text in transcribed

#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){

if (currentNodePtr->getData() == data)

return;

prevNodePtr = currentNodePtr;

currentNodePtr = currentNodePtr->getNextNodePtr();

}

Node* newNodePtr = new Node();

newNodePtr->setData(data);

newNodePtr->setNextNodePtr(0);

prevNodePtr->setNextNodePtr(newNodePtr);

}

void insertSortedOrder(int data){

// Implement the function

// the data should get inserted at an appropriate index/location in the list

// such that the list stays sorted after the insertion

}

void insertAtIndex(int insertIndex, int data){

Node* currentNodePtr = headPtr->getNextNodePtr();

Node* prevNodePtr = headPtr;

int index = 0;

while (currentNodePtr != 0){

if (currentNodePtr->getData() == data)

return;

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 getData()

currentNodePtr = currentNodePtr->getNextNodePtr();

}

cout

}

};

int main(){

int maxValue;

cout

cin >> maxValue;

int listSize;

cout

cin >> listSize;

//srand(time(NULL));

srand( static_castunsigned int>(time(nullptr)));

using namespace std::chrono;

List IntegerList;

high_resolution_clock::time_point t1 = high_resolution_clock::now();

for (int i = 0; i

int value = 1 + rand() % maxValue;

IntegerList.insertSortedOrder(value);

}

high_resolution_clock::time_point t2 = high_resolution_clock::now();

durationdouble, std::milli> insertionTime_milli = t2 - t1;

double insertionTime = insertionTime_milli.count();

//IntegerList.IterativePrint();

cout

system("pause");

return 0;

}

In this programming assignment, you will implement a type of List ADT called the Sorted List. A Sorted List will store its elements in the sorted order. That means, every element that is inserted into the list should be inserted at an appropriate location such that the list (which was sorted before the insertion) will also stay sorted after the insertion. Each insertion operation should take at most linear time (i.e., O(n) time, where n is the number of elements already in the list). You are given the code for implementing a Sorted List using a singly linked list. The main function in the code is already written for you to create a list, generate random numbers and insert them to the list as well as the timers are setup to measure the time to insert. As you can notice in the 'for' loop of the main function, the insertSortedOrder(...) function is called on to insert every random integer in the sorted order in the list (i.e., the list should remain sorted after each insertion). Your main task in this assignment is to implement the insertSortedOrder(...) function to keep the singly linked list stay sorted after each insertion. Following is an example illustration: Initialization Insert 10 Insert 3 Insert 5 Insert 12 Insert 7 Insert 2 Insert 8 Contents of the SortedList after the Insertion { } // empty 10 3 10 3 5 10 3 5 10 12 3 5 7 10 12 2 3 5 7 10 12 2 3 5 7 8 10 12 You would run your code with list size values of 1000, 10000 and 100000, with a maximum value of 50000 for any integer in the list for each case. The code will print the average time per insertion in milli seconds. You would then plot the results in Excel, fit the data points to a polynomial (power function) and determine the empirical relationship between run time and list size: i.e., the run time as a polynomial function of the list size. Submission: Items 1-4 in a single PDF document Item 5 - submitted as a .cpp file 1 - 15 pts) Pseudo code of your algorithm to insert an element to a singly linked list-based SortedList. 2 - 5 pts) Analysis/Explanation of the time complexity of your algorithm (1). 3 - 5 pts) Screenshots of the outputs showing the average time per insertion for the implementation for the three different values of the list size. 4 - 15 pts) The Excel plot (as a screenshot) showing the polynomial (power function) fit along with the R^2 value, which is a measure of the closeness (accuracy) of the fit. 5-60 pts) The entire .cpp file for the singly linked list-based SortedList implementation. In this programming assignment, you will implement a type of List ADT called the Sorted List. A Sorted List will store its elements in the sorted order. That means, every element that is inserted into the list should be inserted at an appropriate location such that the list (which was sorted before the insertion) will also stay sorted after the insertion. Each insertion operation should take at most linear time (i.e., O(n) time, where n is the number of elements already in the list). You are given the code for implementing a Sorted List using a singly linked list. The main function in the code is already written for you to create a list, generate random numbers and insert them to the list as well as the timers are setup to measure the time to insert. As you can notice in the 'for' loop of the main function, the insertSortedOrder(...) function is called on to insert every random integer in the sorted order in the list (i.e., the list should remain sorted after each insertion). Your main task in this assignment is to implement the insertSortedOrder(...) function to keep the singly linked list stay sorted after each insertion. Following is an example illustration: Initialization Insert 10 Insert 3 Insert 5 Insert 12 Insert 7 Insert 2 Insert 8 Contents of the SortedList after the Insertion { } // empty 10 3 10 3 5 10 3 5 10 12 3 5 7 10 12 2 3 5 7 10 12 2 3 5 7 8 10 12 You would run your code with list size values of 1000, 10000 and 100000, with a maximum value of 50000 for any integer in the list for each case. The code will print the average time per insertion in milli seconds. You would then plot the results in Excel, fit the data points to a polynomial (power function) and determine the empirical relationship between run time and list size: i.e., the run time as a polynomial function of the list size. Submission: Items 1-4 in a single PDF document Item 5 - submitted as a .cpp file 1 - 15 pts) Pseudo code of your algorithm to insert an element to a singly linked list-based SortedList. 2 - 5 pts) Analysis/Explanation of the time complexity of your algorithm (1). 3 - 5 pts) Screenshots of the outputs showing the average time per insertion for the implementation for the three different values of the list size. 4 - 15 pts) The Excel plot (as a screenshot) showing the polynomial (power function) fit along with the R^2 value, which is a measure of the closeness (accuracy) of the fit. 5-60 pts) The entire .cpp file for the singly linked list-based SortedList implementation

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Students also viewed these Databases questions

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago