I have been working on this for a few days now. I'm just trying to make a deep copy of a template linked list. I have successfully copied the list, but when I enter a new item to the new list, it also adds it to the original list. I'm using the operator= to copy the list and I am having problems getting a copy function to work. I have read many different ways to copy a linked list but have failed every time. Any help would be greatly appreciated. Here is SortedType.h and driver.cpp.
// SortedType.h
#include "ItemType.h"
using std::bad_alloc;
// Header file for Sorted List ADT.
template<class ItemType>
struct NodeType;
template<class ItemType>
class SortedType
{
public:
SortedType(); // Class constructor
~SortedType(); // Class destructor
bool IsFull() const;
int GetLength() const;
void MakeEmpty();
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
void Print();
//SortedType<ItemType>(const SortedType<ItemType>& newList);
//friend void Copy(SortedType<ItemType>, SortedType<ItemType>&);
void operator=(SortedType<ItemType>);
//Copy constructor.
void GetNextItem(ItemType&);
private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* topPtr;
NodeType<ItemType>* currentPos;
};
.
.
.
template<class ItemType>
void SortedType<ItemType>::operator =
(SortedType<ItemType> anotherList)
{
if ( listData == NULL)
listData = NULL;
else
{
NodeType<ItemType>* ptr1;
NodeType<ItemType>* ptr2;
topPtr = new NodeType<ItemType>;
topPtr->info = topPtr->info;
ptr1 = topPtr->next;
ptr2 = topPtr;
while (ptr1 != NULL)
{
ptr2->next = new NodeType<ItemType>;
ptr2 = ptr2->next;
ptr2->info = ptr1->info;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}
}
template<class ItemType>
SortedType<ItemType>::SortedType(const SortedType<ItemType>& newList)
{
NodeType<ItemType>* ptr1;
NodeType<ItemType>* ptr2;
if (anotherList.topPtr == NULL)
topPtr = NULL;
else
{
topPtr = new NodeType<ItemType>;
topPtr->info = anotherList.topPtr->info;
ptr1 = anotherList.topPtr->next;
ptr2 = topPtr;
while (ptr1 != NULL)
{
ptr2->next = new NodeType<ItemType>;
ptr2 = ptr2->next;
ptr2->info = ptr1->info;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}
}
// driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "SortedType.h"
using namespace std;
int main()
{
int num; // Data items in intList.
SortedType<int> intList;
ifstream intFile;
intFile.open("assignment4int.dat");
while(intFile >> num)
{
intList.InsertItem(num);
}
int a = intList.GetLength(); // Stores the length of the list in a.
cout << "intList contains " << a << " items:" << endl;
intList.Print();
SortedType<int>& newIntList = intList; // Copies intList to newIntList;
cout << "Copy of intList:" << endl;
newIntList.Print();
cout << endl;
cout << "Enter integer to add to newIntList" << endl;
cin >> num;
newIntList.InsertItem(num);
cout << "newIntList now contains:" << endl;
newIntList.Print();
cout << endl;
cout << "Original intList:" << endl;
newIntList.Print(); // Displays copied list.
}