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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.