Question: C++ * Declare in simpleLinkedList.cpp a structure 'node' which defines a single node of linkedList containing integers only. Your structure should have at least one

C++

* Declare in simpleLinkedList.cpp a structure 'node' which defines a single node of linkedList containing integers only. Your structure should have at least one variable for data section and a pointer for the next node.

* Modify exercise1.cpp - Add a new node. - Print all the items. - Display the last item (the one pointing to null).

* Implement the following in simpleLinkedList.cpp: - void insertAt(int newItem, int location) : inserts a node at a specific position in the list. - void remove(int item) : deletes a node containing specific item.

Notes: - The list does not have to be sorted. - insertAt() should check for invalid locations. - remove() should output an error message if the item is not found.

Exercise1.cpp :

#include "simpleLinkedList.cpp"

int main(int argc, char const *argv[])

{

simpleLinkedList list;

int data[5] = {12,5,14,1,9};

cout << "Generating list" << endl;

for (int i = 0; i < 5; ++i)

{

list.add(data[i]);

}

list.print();

cout << " List Length: " << list.length() << endl<< endl;

// add an item to the list & print the list

cout << "Adding 3 to the list" << endl;

/* CODE HERE */

// get the last item from the list and show it

cout << "Getting the last item: "<< endl;

/* CODE HERE */

return 0;

}

Excercise 2.cpp :

#include "simpleLinkedList.cpp"

int main(int argc, char const *argv[]) { simpleLinkedList list; int data[5] = {12,5,14,1,9};

cout << "Generating list" << endl; for (int i = 0; i < 5; ++i) { list.add(data[i]); } list.print(); cout << " List Length: " << list.length() << endl<< endl;

cout << "Removing 5 from list" << endl; list.remove(5); list.print(); cout << " List Length: " << list.length() << endl<< endl;

int location = 4; cout << "Inserting 100 at location "<< location << endl; list.insertAt(100, location); list.print(); cout << " List Length: " << list.length() << endl<< endl;

return 0; }

simpleLinkedList.cpp:

#include

#include

using namespace std;

// Declare in simpleLinkedList.cpp a structure 'node' which defines a single

// node of linkedList containing integers only. Your structure should have at

// least one variable for data section and a pointer for the next node.

/* CODE HERE */

class simpleLinkedList

{

public:

simpleLinkedList();

~simpleLinkedList();

void add(int data);

void print();

int length();

int itemAt(int location);

void insertAt(int insertItem, int location); // insert a node at a specific position in the list

void remove(int removeItem); // delete a node containing specific item

private:

int count; //variable to store the number of list elements

Node* first; //pointer to the first node of the list

Node* last; //pointer to the last node of the list

};

simpleLinkedList::simpleLinkedList(){

count = 0;

first = NULL;

last = NULL;

}

simpleLinkedList::~simpleLinkedList(){

Node *temp; //pointer to deallocate the memory

//occupied by the node

while (first != NULL) //while there are nodes in the list

{

temp = first; //set temp to the current node

first = first->link; //advance first to the next node

delete temp; //deallocate the memory occupied by temp

}

last = NULL; //initialize last to NULL; first has already

//been set to NULL by the while loop

count = 0;

}

void simpleLinkedList::print(){

Node* temp = first;

while(temp != NULL){

cout << temp->data << " ";

temp = temp->link;

}

}

int simpleLinkedList::length(){

return count;

}

void simpleLinkedList::add(int data){

Node* node = new Node();

node->data = data;

node->link = NULL;

if(first == NULL){

first = node;

last = node;

count++;

}

else{

last->link = node;

last = node;

count++;

}

}

int simpleLinkedList::itemAt(int location){

Node* node = new Node();

if(location==0){ // insert in the first position

return first->data;

}

else if(location

int localCount = 0;

Node *current = first;

while(localCount<(location) ){

current = current->link;

localCount++;

}

return current->data;

}

else

cout <<"Can not find item at location = "<

}

// insert a node at a specific position in the list

void simpleLinkedList::insertAt(int data, int location){

/* CODE HERE */

}

// delete a node containing specific item

void simpleLinkedList::remove(int deleteItem){

/* CODE HERE */

}

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!