In: Computer Science
C++
#include <iostream>
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
#include <iostream>
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) {
Node *x = new Node;
x->data = value;
x->next = NULL;
if(index == 0) {
x->next = head;
head = x;
} else {
index--;
Node *y = head;
while(y->next && index != 0) {
y = y ->next;
index--;
}
if(index == 0) {
x->next = y->next;
y->next = x;
} else {
// add new nodes.
while(index != 0) {
y->next = new Node(0);
y = y->next;
index--;
}
y->next = new Node(value);
}
}
}
int getValue(int index){
Node *y = head;
while(y && index != 0) {
y = y ->next;
index--;
}
if(y == NULL) {
// invalid index
return -1;
} else {
return y->data;
}
}
void setValue(int index, int value){
Node *y = head;
while(y && index != 0) {
y = y ->next;
index--;
}
if(y == NULL) {
// invalid index
return;
} else {
y->data =value;
}
}
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;
}

Please upvote, as i have given the exact answer as asked in
question. Still in case of any concerns in code, let me know in
comments. Thanks!