My program runs fine until it exits, where it displays a Window's error that the program has stopped working. Could someone lead me in the right direction on how to deal with this? Thanks.
#include <iostream>
#include <cassert>
#include <vector>
usingnamespace std;
//definition of the node
template<class Type>
struct node
{
int data;
node<Type> *link;
};
template<class Type>
class CircularList
{
template<class Type2>
friend ostream& operator<<(ostream&, const CircularList<Type>&);
//overload the stream insertion operator.
public:
const CircularList<Type>& operator=(const CircularList<Type>&);
//overload the assignment operator
void AdvanceN(const Type& newItem);
void DeleteThis();
void Eliminated();
void initializeList();
//function to initialize the list to an empty state.
//postcondition: first = null; last = null; count = 0;
bool isEmptyList();
//function to determin whether the list is empty.
//postcondition: returns true if the list is empty; otherwise, returns false.
int length();
//function to return the number of nodes in the list.
//postcondition: the value of count is returned
void destroyList();
//function to delete all the nodes from the list.
//postcondition: first = null; last = null; count = 0;
Type front();
//function to return the first element of the list.
//precondition: the list must exist and must not be empty.
//postcondition: if the list is empty, then the program terminates
//otherwise, the first element of the list is returned.
Type back();
//function to return the last element of the list.
//precondition: the list must exist and must not be empty.
//post condition: if the list is empty, then the program terminates.
//otherwise, the last element of the list is returned.
void insertLast(const Type& newItem);
CircularList(const Type& newItem);
//default constructor
CircularList(const CircularList<Type>& otherList);
//copy constructor
~CircularList();
//destructor
int count;
node<Type> *first, *last, *newNode;
node<Type> *current;
vector<int> myVector;
private:
void copyList(const CircularList<Type>& otherList);
//function to make a copy of otherList
};
template <class Type>
void CircularList<Type>::AdvanceN(const Type& newItem)
{
if(first==NULL)
return;
node<Type> *temp;
if(current==NULL){
current = first;
}
else{
temp=current;
}
for(int i=1;i<=newItem;++i){
current=current->link;
}
cout<<"You are here: "<<current->data<<endl;
}
template <class Type>
void CircularList<Type>::DeleteThis()
{
if(first==NULL)
return;
node<Type> *temp;
if(current==NULL){
current = first;
}
else{
temp=current;
}
while(current->link!=temp){
current=current->link;
}
current->link=temp->link;
if(temp==first){
first = temp->link;
}
if(temp==last){
last = current;
}
myVector.push_back(temp->data);
//cout<<endl<<temp->data<<endl;
delete temp;
count--;
}
template<class Type>
void CircularList<Type>::Eliminated()
{
int j = 0;
cout<<"Eliminated in this order:"<<endl;
for(int j; j<myVector.size(); j++)
{
cout<<myVector[j]<<endl;
}
}
template<class Type>
bool CircularList<Type>::isEmptyList()
{
return(first==NULL);
}
//Default Constructor
template<class Type>
CircularList<Type>::CircularList(const Type& newItem)
{
first = NULL;
last = NULL;
count = 0;
int i = 1;
while (i<=newItem)
{
insertLast(i);
i++;
}
cout<<endl<<"There are "<<length()<<" people standing in a circle:\n";
}
template<class Type>
void CircularList<Type>::destroyList()
{
node<Type> *temp;
while(first!= NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
void CircularList<Type>::initializeList()
{
destroyList();
}
//Overloads << operator
template<class Type>
ostream& operator<<(ostream& osObject, const CircularList<Type>& list)
{
node<Type> *current; //pointer to traverse the list
current = list.first;// points to the first node
for(int i=1;i<=list.count;i++)
{
osObject<<current->data<<" ";
current = current->link;
}
return osObject;
}
template<class Type>
int CircularList<Type>::length()
{
return count;
}
//returns info of the first node
template<class Type>
Type CircularList<Type>::front()
{
assert(last != NULL);
return first->data;
}
//returns info of the last node
template<class Type>
Type CircularList<Type>::back()
{
assert(last != NULL);
return last->data;
}
template<class Type>
void CircularList<Type>::insertLast(const Type& newItem)
{
node<Type> *newNode;
newNode = new node<Type>;
assert(newNode != NULL);
newNode->data = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
newNode->link=first; //point back to first node to be circular
count++;
}
else // the list is not empty, insert newnode after last
{
last->link = newNode;
last = newNode;
count++;
newNode->link=first; //point back to first node to be circular
}
}
It's the last block of code. Sorry for the break in between the code of the first file. cplusplus.com yelled at me for the length I was trying to post.
The most likely thing is something is going wrong in the destructor. You probably are trying to delete something that doesn't exist. (Perhaps deleting something twice).
You guys rock! Thanks for the help. Turns out I was deleting temp twice, once in the DeleteThis() method and a second time in the destroyList() method that gets called in the destructor. I removed the while loop in destroyList(). I've also changed the header file from a .cpp file to a .h file.