Need help adding the Overloading + operator. And running the SimpleListTestDrive
ID: 3883013 • Letter: N
Question
Need help adding the Overloading + operator.
And running the SimpleListTestDriver.cpp
NodeSLList.h
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
#include <iostream>
using std::ostream;
using std::cout;
using std::cin;
using std::endl;
struct IntNode
{
int data;
IntNode * next;
};
// Class NodeSLList1 Declaration
class NodeSLList {
friend ostream & operator<<(ostream &, NodeSLList &);
public:
NodeSLList();
NodeSLList(NodeSLList &);
~NodeSLList();
bool IsEmpty();
int GetSize();
void AddToHead(const IntNode & node);
IntNode DeleteFromHead();
void AddToTail(const IntNode & node);
IntNode DeleteFromTail();
IntNode DeleteNode(int nodeNum);
void InsertNode(int nodeNum, const IntNode &node);
void UpdateNode(int nodeNum, const IntNode &node);
void DestroyList();
NodeSLList & operator=(NodeSLList &);
bool operator==(NodeSLList &);
bool operator!=(NodeSLList &);
///////////////////////////////////////////////////////////////////////
// operator+
//
// Description: addition operator
// Input: reference to NodeSLList object to append to this one
// Output: none
// Returns: NodeSLList object that is the result of the addition operator
///////////////////////////////////////////////////////////////////////
NodeSLList operator+(NodeSLList &);
private:
IntNode *head, *tail;
IntNode &RetrieveNode(int nodeNum) const; //helper function
//nodeNum is
};
#endif INT_LINKED_LIST
//NodeListSL.cpp
#include "NodeSLList.h"
///////////////////////////////////////////////////////////////////////
// default constructor
///////////////////////////////////////////////////////////////////////
NodeSLList::NodeSLList()
{
head = tail = 0;
}
///////////////////////////////////////////////////////////////////////
// copy constructor
///////////////////////////////////////////////////////////////////////
NodeSLList::NodeSLList(NodeSLList & list)
{
IntNode *tmp;
for(int i=1;i<list.GetSize();i++)
{
*tmp=list.RetrieveNode(i);
AddToTail(*tmp);
}
}
///////////////////////////////////////////////////////////////////////
// destructor
///////////////////////////////////////////////////////////////////////
NodeSLList::~NodeSLList()
{
// call destroyList() to remove all nodes
// and reset linked list
DestroyList();
}
///////////////////////////////////////////////////////////////////////
// IsEmpty
///////////////////////////////////////////////////////////////////////
bool NodeSLList::IsEmpty()
{
// if head is NULL, then the list is empty, and we will return true
// otherwise, we will return false
return (head == 0);
}
///////////////////////////////////////////////////////////////////////
// GetSize
///////////////////////////////////////////////////////////////////////
int NodeSLList::GetSize()
{
// check to see if the list is empty. if
// so, just return 0
if ( IsEmpty() ) return 0;
int size = 1;
IntNode *p = head;
// compute the number of nodes and return
while (p != tail)
{
// until we reach the tail, keep counting
size++;
p = p->next;
}
return size;
}
///////////////////////////////////////////////////////////////////////
// AddToHead
///////////////////////////////////////////////////////////////////////
void NodeSLList::AddToHead(const IntNode & node)
{
// create a new node, and make it the head. the
// previous head will become head->next
IntNode * next = head;
head = new IntNode;
head->next = next;
head->data = node.data;
// if this is the first node, make the tail the
// same as the head
if (tail == 0)
tail = head;
}
///////////////////////////////////////////////////////////////////////
// DeleteFromHead
///////////////////////////////////////////////////////////////////////
IntNode NodeSLList::DeleteFromHead()
{
IntNode temp;
temp.data=0;
temp.next=NULL;
if (IsEmpty())
{
cout << "*****ERROR: Can't delete from head. List is Empty" << endl;
return temp;
}
// get current value of node we are about to delete,
// so we can return it.
temp.data = head->data;
IntNode *tmp = head;
// if there is only one node, set the head and pointer tails
// to NULL (0)
if (head == tail)
head = tail = 0;
// otherwise, move the head pointer to the next node
// in the list
else
head = head->next;
// delete head node
delete tmp;
// return value of node that was deleted
return temp;
}
///////////////////////////////////////////////////////////////////////
// AddToTail
///////////////////////////////////////////////////////////////////////
void NodeSLList::AddToTail(const IntNode & node)
{
// create a new node, and make it the tail. the
// previous tail will become tail->next
IntNode *tmp;
tmp->data=node.data;
tmp->next=NULL;//or nullptr?
tail->next=tmp;
// if this is the first node, make the tail the
// same as the head
if (head == 0) //
tail=tmp;
}
///////////////////////////////////////////////////////////////////////
// DeleteFromTail
///////////////////////////////////////////////////////////////////////
IntNode NodeSLList::DeleteFromTail()
{
IntNode nodeData;
// get the current data at the tail
nodeData.data = tail->data;
// if there is only one node, delete the only node, and set the
// head and tail pointers to NULL (0)
if (head == tail)
{
delete head;
head = tail =0;
}
// otherwise, traverse to the tail node and delete it
else
{
IntNode * temp;
// traverse to tail pointer
for (temp = head; temp->next != tail; temp = temp->next);
delete tail;
tail = temp;
tail->next = 0;
}
return nodeData;
}
///////////////////////////////////////////////////////////////////////
// DeleteNode
///////////////////////////////////////////////////////////////////////
IntNode NodeSLList::DeleteNode(int nodeNum)
{
if (nodeNum <= 0) nodeNum = 1;
IntNode *prev=head , *temp=head;
IntNode current;
// traverse to the node
for (int loop=1; loop<nodeNum; loop++)
{
prev=temp, temp=temp->next;
// check for case where nodeNum is > the number of
// nodes in the list. if we reach the tail before
// we traverse to the node, delete the tail
if ( temp == tail )
return DeleteFromTail();
}
// if deleting the head just call
// the appropriate member function
// and don't repeat that logic here
if (temp == head) return DeleteFromHead();
// otherwise, delete the node we traversed to
prev->next = temp->next;
current.data = temp->data;
delete temp;
return current;
}
///////////////////////////////////////////////////////////////////////
// InsertNode
///////////////////////////////////////////////////////////////////////
void NodeSLList::InsertNode(int nodeNum, const IntNode &node)
{
IntNode *prevNode = head;
for (int i = 1; i < nodeNum; i++)
prevNode = prevNode->next;
IntNode * insert;
insert = new IntNode(node);
prevNode->next = insert;
insert->next = prevNode->next->next;
}
///////////////////////////////////////////////////////////////////////
// UpdateNode
///////////////////////////////////////////////////////////////////////
void NodeSLList::UpdateNode(int nodeNum, const IntNode &node)
{
IntNode *tmp = head;
// traverse to the node, or to the last node, whichever comes
// first. if the last node is reached, then that is the node
// that is updated
for (int i=1; i< nodeNum && tmp != tail; i++)
tmp = tmp->next;
tmp->data = node.data;
}
///////////////////////////////////////////////////////////////////////
// DestroyList
///////////////////////////////////////////////////////////////////////
void NodeSLList::DestroyList()
{
// while the list is NOT empy
// keep removing the head node and make
// the next node the head node
for (IntNode *p; !IsEmpty(); )
{
p = head->next;
delete head;
head = p;
}
head = tail = 0;
}
///////////////////////////////////////////////////////////////////////
// operator=
///////////////////////////////////////////////////////////////////////
NodeSLList & NodeSLList::operator=(NodeSLList & list)
{
IntNode *origPtr, *LastPtr;
origPtr=list.head;
LastPtr= new IntNode();
head=LastPtr;
while(LastPtr != NULL){
LastPtr = new IntNode();
LastPtr = LastPtr->next;
}
return *this;
}
///////////////////////////////////////////////////////////////////////
// operator==
///////////////////////////////////////////////////////////////////////
bool NodeSLList::operator==(NodeSLList & list)
{
IntNode *tmp1;
*tmp1=RetrieveNode(1);
IntNode *tmp2=list.head;
int c=0;
for(int i=2;i<list.GetSize();i++,tmp2=tmp2->next)
{
*tmp1 = RetrieveNode(i);
if(tmp1->data==tmp2->data)
c=1;
else
{
c=0;
break;
}
}
return(c==1);
}
///////////////////////////////////////////////////////////////////////
// operator!=
///////////////////////////////////////////////////////////////////////
bool NodeSLList::operator!=(NodeSLList & list)
{
IntNode *tmp1;
IntNode *tmp2=list.head;
int c=0;
for(int i=1;i<GetSize();i++,tmp2=tmp2->next)
{
*tmp1 = RetrieveNode(i);
if(tmp1->data!=tmp2->data)
{
c=1;
break;
}
}
return(c==1);
}
///////////////////////////////////////////////////////////////////////
// operator+
///////////////////////////////////////////////////////////////////////
NodeSLList NodeSLList::operator+(NodeSLList & list)
{
//need help here
}
///////////////////////////////////////////////////////////////////////
// RetrieveNode
//
// Description: retrieve data from a node without removing it from
// the list
// Input: node number (1-N; not 0-N-1)
// Output: none
// Returns: reference to node data
///////////////////////////////////////////////////////////////////////
IntNode & NodeSLList::RetrieveNode(int nodeNum) const
{
IntNode *tmp = head;
// traverse to the node, or to the last node, whichever comes
// first
for (int i=1; i< nodeNum && tmp != tail; i++)
tmp = tmp->next;
return *tmp;
}
ostream & operator<<(ostream & output, NodeSLList & list)
{
IntNode *tmp = list.head; // temporary pointer starting from head
for (int i=1; i<=list.GetSize(); i++)
{
output<<tmp->data;
tmp=tmp->next;//movethe pointer till the specified nodeNum
}
return output;
}
// end of NodeSLList
SimpleListTestDriver.cpp
#include <cstdlib>
#include <ctime>
#include "NodeSLList.h"
void NodeSLList_Test();
void TestSort();
void main(void)
{
NodeSLList_Test();
}
void NodeSLList_Test()
{
NodeSLList list1;
cout << "*****************************************************************************" << endl;
cout << "Testing default constructor" << endl;
cout << "*****************************************************************************" << endl;
cout << "Creating NodeSLList list1" << endl;
cout << "cout << list1 " << endl;
cout << list1 << endl << endl;
IntNode temp;
IntNode n1;
n1.data=10;
IntNode n2;
n2.data=20;
IntNode n3;
n3.data=30;
IntNode n4;
n4.data=40;
IntNode n5;
n5.data=50;
cout << "*****************************************************************************" << endl;
cout << "Testing AddToHead() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "Adding " << n5.data << " to list1" << endl;
list1.AddToHead(n5);
cout << "Adding " << n4.data << " to list1" << endl;
list1.AddToHead(n4);
cout << "Adding " << n3.data << " to list1" << endl;
list1.AddToHead(n3);
cout << "Adding " << n2.data << " to list1" << endl;
list1.AddToHead(n2);
cout << "Adding " << n1.data << " to list1" << endl;
list1.AddToHead(n1);
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing Copy Constructor. Creating list2 from list1" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing AddToTail() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing GetSize() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "list1 contains " << list1.GetSize() << " node(s)" << endl << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing operator= by assigning list1=list2" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing DeleteFromHead() operation" << endl;
cout << "*****************************************************************************" << endl;
temp = list1.DeleteFromHead();
cout << "node retrieved " << temp.data << endl;
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing equality == and inequality != operators" << endl;
cout << "*****************************************************************************" << endl;
if ( list1 == list2 ) cout << "list1 == list2" << endl << endl;
if ( list1 != list2 ) cout << "list1 != list2" << endl << endl;
cout << "Putting the previous node back on the head of list1. Now list1 and list2 should be equal" << endl;
list1.AddToHead(temp);
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing equality == and inequality != operators" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing DeleteFromTail() operation" << endl;
cout << "*****************************************************************************" << endl;
temp = list1.DeleteFromTail();
cout << "node retrieved " << temp.data << endl;
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << endl;
cout << "Adding " << n3.data << " to head" << endl;
cout << "Adding " << n2.data << " to head" << endl;
list1.AddToHead(n3);
list1.AddToHead(n2);
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing DeleteNode() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "Deleting node 35 (should be last node, since 35 is beyond end of list)" << endl;
temp = list1.DeleteNode(35);
cout << "cout << list1 " << endl;
cout << list1 << endl;
temp = list1.DeleteNode(3);
cout << "Deleteing node 3 (" << temp.data << ")" << endl;
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing InsertNode() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing IsEmpty() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "cout << list2 " << endl;
cout << list2 << endl;
cout << (list2.IsEmpty() ? "list2 is EMPTY" : "list2 is NOT EMPTY") << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing UpdateNode() operation (updating 3rd node with 500)" << endl;
cout << "*****************************************************************************" << endl;
temp.data = 500;
list1.UpdateNode(3,temp);
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing the ability to delete nodes from head." << endl;
cout << "*****************************************************************************" << endl;
cout << "list1 has " << list1.GetSize() << " nodes" << endl;
int numNodesToDelete = list1.GetSize();
for (int x=0; x<numNodesToDelete; x++)
{
cout << "Deleting from head" << endl;
temp = list1.DeleteFromHead();
cout << "cout << list1 " << endl;
cout << list1 << endl;
}
cout << "Deleting from head, after list is empty. Should receive an error." << endl;
temp = list1.DeleteFromHead();
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing DestroyList() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "cout << list2 " << endl;
cout << list2 << endl;
cout << "calling DestoryList()" << endl;
list2.DestroyList();
cout << "cout << list2 " << endl;
cout << list2 << endl;
cout << "*****************************************************************************" << endl;
cout << "Test IsEmpty() operation" << endl;
cout << "*****************************************************************************" << endl;
cout << "cout << list2 " << endl;
cout << list2 << endl;
cout << "calling list2.IsEmpty()" << endl;
cout << (list2.IsEmpty() ? "list2 is EMPTY" : "list2 is NOT EMPTY") << endl << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing SortList() operation, ascending" << endl;
cout << "*****************************************************************************" << endl;
cout << "*****************************************************************************" << endl;
cout << "Testing SortList() operation, descending" << endl;
cout << "*****************************************************************************" << endl;
system ("pause");
} // end SimpleListTestDriver.cpp
Explanation / Answer
Here is the code:
///////////////////////////////////////////////////////////////////////
// operator+
///////////////////////////////////////////////////////////////////////
NodeSLList NodeSLList::operator+(NodeSLList & list)
{
NodeSLList output(*this);
IntNode *tmp;
for(int i=1;i<list.GetSize();i++)
{
*tmp=list.RetrieveNode(i);
output.AddToTail(*tmp);
}
return output;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.