Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here are the values in myList: 1 2 4 6 8 10 12 Inserting a value at position 0:

ID: 3731082 • Letter: H

Question

Here are the values in myList:
1 2 4 6 8 10 12

Inserting a value at position 0:
values in the list are
99 1 2 4 6 8 10 12
Inserting a value at position 99:
values in the list are
99 1 2 4 6 8 10 12 100
Inserting a value at position 2:
values in the list are
99 1 98 2 4 6 8 10 12 100
Removing node at position 0...
values in the list are
1 98 2 4 6 8 10 12 100
Removing node at position 99...
values in the list are
1 98 2 4 6 8 10 12 100
Removing node at position 4...
values in the list are
1 98 2 4 8 10 12 100
Removing node at position 2...
values in the list are
1 98 4 8 10 12 100
Before reversing the list
1 98 4 8 10 12 100
After reversing the list
100 12 10 8 4 98 1

Here is the code for the test driver hw4.cpp:

#include <iostream>
#include "IntList.h"
using namespace std;

int main()
{
// Create an instance of IntList.
IntList myList;

// Build a list.
myList.appendNode(2); // Append 2 to the list
myList.appendNode(6); // Append 6 to the list
myList.appendNode(8); // Append 8 to the list
myList.insertNode(1); // insert 1 in the list
myList.insertNode(4); // insert 4 in the list
myList.insertNode(12); //insert 12 in the list
myList.insertNode(10); //insert 10 in the list

// Display the nodes.
cout << "Here are the values in myList: ";
myList.print();
cout << " ";

//try insertion
//insert a node at position 0
myList.insert(99, 0);
cout << "Inserting a value at position 0: values in the list are ";
myList.print();
cout << endl;

//insert a node at position 99
myList.insert(100, 99);
cout << "Inserting a value at position 99: values in the list are ";
myList.print();
cout << endl;

//insert a node at position 2
myList.insert(98, 2);
cout << "Inserting a value at position 2: values in the list are ";
myList.print();
cout << endl << endl;

// Remove node at position 0.
cout << "Removing node at position 0... values in the list are ";
myList.removeByPos(0);
myList.print();
cout << endl;

// Try a position that is too big.
cout << "Removing node at position 99... values in the list are ";
myList.removeByPos(99);
myList.print();
cout << endl;

// Remove node at position 4.
cout << "Removing node at position 4... values in the list are ";
myList.removeByPos(4);
myList.print();
cout << endl;

// Remove node at position 2.
cout << "Removing node at position 2... values in the list are ";
myList.removeByPos(2);
myList.print();
cout << endl << endl;

// reverse the list
cout << "Before reversing the list ";
myList.print();
cout << " ";
myList.reverse();
cout << "After reversing the list ";
myList.print();
cout << " ";
return 0;
}

Design your own linked list class IntList to hold a series of integers (1) Provide a constructor for the class. The constructor initializes an empty list. IntListO (2) Provide a destructor for the class. The destructor destroys the list. ~IntList0 Provide member functions as below (3) A member function that appends a node to the list. void appendNode(int value) (4) A member function that inserts a node in the list. void insertNode(int value) (5) A member function that displays all the values in the linked list. void print0 (6) A member function that rearranges the nodes in the list so that their order is reversed. void reverse) (7) A member function that inserts a new value at a specified position. A position of 0 means that the value will become the first item on the list, a position of 1 means that the value will become the second item on the list, and so on. A position equal to or greater than the length of the list means that the value is placed at the end of the list. void insert(int value, int position) (8) A member function that deletes a node at a specified position. A value of 0 for the position means that the first node in the list is deleted. The function does nothing if the specified position is greater than or equal to the length of the list. void removeByPos(int position,) Separate your class definition from implementation. Your class definition must be stored in a file called "IntList.h". Your class implementation must be stored in a file called "IntList.cpp". Use the test driver hw4.cpp (posted on Canvas) to test your IntList class. The output is provided below

Explanation / Answer

IntList.h :

#include <iostream>

class node{

int data;

node* next;

friend class IntList;

};

class IntList{

public:

node* head;

IntList();

~IntList();

void appendNode(int value);

void insertNode(int value);

void print();

void reverse();

void insert(int value,int position);

void removeByPos(int position);

};

IntList.cpp:

#include<iostream>

#include "IntList.h"

using namespace std;

IntList::IntList(){

head=NULL;

}

IntList::~IntList(){

}

void IntList::appendNode(int value){

node* tmp = new node();

tmp->data = value;

tmp->next = NULL;

if(head==NULL){

head=tmp;

return ;

}

node* tmp1 = head;

while(tmp1->next!=NULL){

tmp1 = tmp1->next;

}

tmp1->next = tmp;

}

void IntList::insertNode(int value){

node* tmp = new node();

tmp->data = value;

tmp->next = NULL;

if(head==NULL){

head=tmp;

return ;

}

node* tmp1 = head;

while(tmp1->next!=NULL){

tmp1 = tmp1->next;

}

tmp1->next = tmp;

}

void IntList::print(){

node* tmp = head;

while(tmp->next!=NULL){

cout<<tmp->data<<" ";

tmp = tmp->next;

}

cout<<tmp->data;

}

void IntList::insert(int value,int position){

if(position==0){

node* temp = new node();

temp->data = value;

temp->next = head;

head = temp;

return;

}

int cnt=1;

node* temp = new node();

temp->data = value;

node* tmp = head;

while(tmp->next!=NULL && cnt<position){

tmp = tmp->next;

cnt+=1;

}

if(tmp->next==NULL){

this->appendNode(value);

}

else{

temp->next = tmp->next;

tmp->next = temp;

}

}

void IntList::removeByPos(int position){

int cnt = 0;

node* tmp = new node();

tmp = head;

if(position==0){

head=head->next;

}

else{

while(cnt+1<position){

tmp = tmp->next;

cnt++;

}

tmp->next = tmp->next->next;

}

}

void IntList::reverse(){

node* currNode = head;

node* prevNode = NULL;

node* nextNode = NULL;

while(currNode != NULL){

nextNode = currNode->next;

currNode->next = prevNode;

prevNode = currNode;

currNode = nextNode;

}

head = prevNode;

}

int main(){

IntList myList;

myList.appendNode(2); // Append 2 to the list

myList.appendNode(6); // Append 6 to the list

myList.appendNode(8); // Append 8 to the list

myList.insertNode(1); // insert 1 in the list

myList.insertNode(4); // insert 4 in the list

myList.insertNode(12); //insert 12 in the list

myList.insertNode(10); //insert 10 in the list

// Display the nodes.

cout << "Here are the values in myList: ";

myList.print();

cout << " ";

myList.insert(15,6);

myList.print();

cout<<endl;

myList.removeByPos(7);

myList.print();

myList.reverse();

cout<<endl;

myList.print();

cout<<endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote