Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ #include using namespace std; struct Node { int data; Node* next; Node(){ data = 0; next = NULL; } Node(int x){ data = x;

C++

#include

using namespace std;

struct Node { int data; Node* next; Node(){ data = 0; next = NULL; } Node(int x){ data = x; next = NULL; } };

struct LinkedList { Node* head; LinkedList(){ head = NULL; } void append(int value){ if (head == NULL){ head = new Node(value); } else{ Node* newNode = new Node(value); Node* temp = head; while(temp->next != NULL){ temp = temp->next; }

temp->next = newNode; } } void insertAt(int index, int value) { // Provide your code here } int getValue(int index){ // Provide your code here } void setValue(int index, int value){ // Provide your code here } void print (){ Node* temp = head; while (temp != NULL) { cout << temp->data << endl; temp = temp->next; } } ~LinkedList(){ Node* temp = head; while(temp != NULL){ temp = temp->next; delete head; head = temp; } } };

int main(int argc, const char * argv[]) { LinkedList myList; for (int i = 0; i < 6; i++) { myList.append(i); } myList.insertAt(2, 77); myList.insertAt(10, 89); myList.append(101); myList.setValue(0, 11); cout << myList.getValue(2) << endl << endl; myList.print(); // Expected output: // 77 // // 11 // 1 // 77 // 2 // 3 // 4 // 5 // 0 // 0 // 0 // 89 // 101 return 0; }

The first function you are being asked to implement is int getValueAt(int index) . This function simply returns the value that appears in the array position specified by index .

The second function is void setValueAt(int index, int value) . Its job is to store value in the array position corresponding to index .

The last function to implement is void insertAt(int index, int value) . As the name suggests, it needs to insert the value at the index. It should not overwrite anything. If there is already a something stored at index , it should be shifted to the right. If index is larger than the current size of the list, then it needs to be resized in order to accomodate. If there is a gap between the old size of the list, and the newly inserted value, that gap should be filled with 0s

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

Students also viewed these Databases questions