Question: use the provided C++ code to do the following: Create a new class for the linked list. The new class should have the following functions:

use the provided C++ code to do the following:

Create a new class for the linked list. The new class should have the following functions:

- Delete, Insert and Output

1. Create a list of the following elements [11,10,9,8,7,6,5,4,3,2,1]

2. Delete element #5 (counting from 0)

3. Insert element 100 at position 8 (counting from 0)

4. Modify the Output function to save the list to a file. The name of the file is provided as an argument.

- obj.Output('test.txt');

5 Using object from the linked list For step 4 the name of the file should be FirstName.LastName.lList.txt

code:

#include
#include
class Node
{
public:
Node* next;
int data;
};
using namespace std;
class LinkedList
{
public:
int length;
Node* head;
LinkedList();
~LinkedList();
Node * add(int index, int data);
void print();
};
LinkedList::LinkedList(){
this->length = 0;
this->head = NULL;
}
LinkedList::~LinkedList(){
std::cout << "LIST DELETED";
}
Node* LinkedList::add(int index, int data){
if (index < 0) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = new Node;
newNode->data = data;
if (index == 0) {
newNode->next = head;
head = newNode;
this->length++;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
this->length++;
}
return newNode;
}
void LinkedList::print(){
Node* head = this->head;
int i = 1;
while(head){
std::cout << i << ": " << head->data<
head = head->next;
i++;
}
}
int main(int argc, char const *argv[])
{
LinkedList* list = new LinkedList();
//list->add(0,rand() % 10);
list->add(0,2);
list->add(1,20);
list->print();
std::cout << "List Length: " << list->length << std::endl;
//delete list;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!