Language: c++ Please write comments so I can study. USING the program you implem
ID: 666872 • Letter: L
Question
Language: c++
Please write comments so I can study.
USING the program you implemented for #2:
(a) add a member function declaration in the class declaration (in the .h header file) for search
(b) implement the search member function (by adding code to the .cpp file)
(c) in main(), as the user for a number to search for
(d) invoke your search function and return the result as described by program #5 in the book
(e) please paste the code in text
// This is the existed code we are suppose to modify
=====================================================================
Program IntList.h
=====================================================================
#ifndef INTLIST_H
#define INTLIST_H
// **************************************************************************************
// CLASS DECLARATION
// **************************************************************************************
class IntList
{
private:
struct ListNode
{
int value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
IntList(); //Default constructor
~IntList(); // Destructor
void appendNode(int);
void insertNode(int);
void deleteNode(int);
void print();
};
#endif
=====================================================================
Program IntList.cpp
=====================================================================
#include "IntList.h"
#include<iostream>
using namespace std;
IntList::IntList() // Constructor
{
head = NULL;
}
IntList::~IntList() // Desstructor
{
head = NULL;
cout<<"Destructor called, IntList is being destroyed!";
}
void IntList::appendNode(int data)
{
//Create a node of type ListNode
ListNode *newnode;
newnode=new ListNode;
//Assign data
newnode->value=data;
newnode->next=NULL;
//Check if the head is empty
if(head==NULL)
{
head=newnode;
}
else
{
//Iterate over the nodes till last node is reached
ListNode *temp;
temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void IntList::insertNode(int data)
{
//Create a node of type ListNode
ListNode *newnode;
newnode=new ListNode;
//Assign data
newnode->value=data;
newnode->next=NULL;
//Check if the head is empty
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next = head;
//make head of the current inserted node
head = newnode;
}
}
void IntList::deleteNode(int data)
{
ListNode *temp,*temp1;
temp = head;
if(head==NULL)
{
cout<<"No elements to delete ";
}
else
{
while(temp->value!=data)
{
temp1=temp;
temp=temp->next;
}
if(temp->value==data)
{
//If delete element at head
if(temp==head)
{
if(head->next!=NULL)
head = head->next;
else
head=NULL;
}
//If delete element at end of list
else
{
temp1->next=temp->next;
}
}
else
{
cout<<"No element with "<<data<<" to delete ";
}
}
}
void IntList::print()
{
ListNode *temp;
temp =head;
if(temp==NULL)
{
cout<<"No elements to display ";
}
else
{
cout<<"List Elements ";
while(temp!=NULL)
{
cout<<"["<<temp->value<<"] ->";
temp = temp->next;
}
cout<<endl;
}
}
=====================================================================
Program main.cpp
=====================================================================
#include <iostream>
#include "IntList.h"
using namespace std;
int main() {
// local variables
//
IntList list;
// initialization
//
list.appendNode(2); // Append 2 to the list
list.appendNode(4); // Append 4 to the list
list.appendNode(6); // Append 6 to the list
// Display the nodes.
//
cout << "Here are the initial values: ";
list.print();
cout << endl;
// Insert the value 5 into the list.
//
cout << "Now inserting the value 5. ";
list.insertNode(5);
// Display the nodes now.
//
cout << "Here are the nodes now. ";
list.print();
cout << endl;
// Delete the node holding 6.
//
cout << "Now deleting the last node. ";
list.deleteNode(6);
// Display the nodes.
//
cout << "Here are the nodes left. ";
list.print();
return 0;
}
Explanation / Answer
#ifndef INTLIST_H
#define INTLIST_H
// **************************************************************************************
// CLASS DECLARATION
// **************************************************************************************
class IntList{
private:
struct ListNode{
int value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
IntList(); //Default constructor
~IntList(); // Destructor
void appendNode(int);
void insertNode(int);
void deleteNode(int);
int searchNode(int);
void print();
};
#endif
IntList.cpp
#include "IntList.h"
#include<iostream>
using namespace std;
IntList::IntList() // Constructor
{
head = NULL;
}
IntList::~IntList() // Desstructor
{
head = NULL;
cout<<"Destructor called, IntList is being destroyed!";
}
void IntList::appendNode(int data)
{
//Create a node of type ListNode
ListNode *newnode;
newnode=new ListNode;
//Assign data
newnode->value=data;
newnode->next=NULL;
//Check if the head is empty
if(head==NULL)
{
head=newnode;
}
else
{
//Iterate over the nodes till last node is reached
ListNode *temp;
temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void IntList::insertNode(int data)
{
//Create a node of type ListNode
ListNode *newnode;
newnode=new ListNode;
//Assign data
newnode->value=data;
newnode->next=NULL;
//Check if the head is empty
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next = head;
//make head of the current inserted node
head = newnode;
}
}
void IntList::deleteNode(int data)
{
ListNode *temp,*temp1;
temp = head;
if(head==NULL)
{
cout<<"No elements to delete ";
}
else
{
while(temp->value!=data)
{
temp1=temp;
temp=temp->next;
}
if(temp->value==data)
{
//If delete element at head
if(temp==head)
{
if(head->next!=NULL)
head = head->next;
else
head=NULL;
}
//If delete element at end of list
else
{
temp1->next=temp->next;
}
}
else
{
cout<<"No element with "<<data<<" to delete ";
}
}
}
void IntList::print()
{
ListNode *temp;
temp =head;
if(temp==NULL)
{
cout<<"No elements to display ";
}
else
{
cout<<"List Elements ";
while(temp!=NULL)
{
cout<<"["<<temp->value<<"] ->";
temp = temp->next;
}
cout<<endl;
}
}
int IntList::search(int data){
ListNode *temp = head;
int i = 0;
while (temp != NULL){
if (temp->value == data) return i;
temp = temp->next;
i++;
}
return -1;
}
main.cpp
#include <iostream>
#include "IntList.h"
using namespace std;
int main() {
// local variables
//
IntList list;
// initialization
//
list.appendNode(2); // Append 2 to the list
list.appendNode(4); // Append 4 to the list
list.appendNode(6); // Append 6 to the list
// Display the nodes.
//
cout << "Here are the initial values: ";
list.print();
cout << endl;
// Insert the value 5 into the list.
//
cout << "Now inserting the value 5. ";
list.insertNode(5);
// Search in the list
list.search(5);
list.search(12003);
// Display the nodes now.
//
cout << "Here are the nodes now. ";
list.print();
cout << endl;
// Delete the node holding 6.
//
cout << "Now deleting the last node. ";
list.deleteNode(6);
// Display the nodes.
//
cout << "Here are the nodes left. ";
list.print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.