const int MAX_SIZE = 5;
#include <iostream>
#include <string>
using namespace std;
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType<ItemType>* back;
NodeType<ItemType>* next;
};
template <class ItemType>
class DLLCType
{
public:
DLLCType();
~DLLCType();
int Length();
void InsertItem( ItemType );
void PrintList();
bool isEmpty();
bool isFull();
void deleteItem(int);
private:
NodeType<ItemType>* ListData;
int length;
};
template <class ItemType>
DLLCType<ItemType>::DLLCType()
{
ListData = NULL;
length = 0;
}
template <class ItemType>
DLLCType<ItemType>::~DLLCType()
{
if( ListData == NULL ) //list already empty
return;
else
{
NodeType<ItemType>* location;
NodeType<ItemType>* temp;
while (length > 1)
{
location = ListData;
temp = location->next;
location->next = temp->next;
temp->next->back = location;
delete temp;
}
delete ListData;
ListData = NULL;
}
}
template <class ItemType>
bool DLLCType<ItemType>::isFull()
{
return (length == MAX_SIZE);
}
template <class ItemType>
bool DLLCType<ItemType>::isEmpty()
{
if ( ListData == NULL )
return true;
else
return false;
}
template <class ItemType>
void DLLCType<ItemType>::PrintList()
{
if( ListData == NULL ) //list already empty
{
cout << "List is empty" << endl;
return;
}
NodeType<ItemType>* location;
location = ListData;
while(location->next != ListData)
{
cout << location->info << " ";
location = location->next;
}
cout << location->info << endl;
}
template <class ItemType>
int DLLCType<ItemType>::Length()
{
return length;
}
template <class ItemType>
void DLLCType<ItemType>::InsertItem( ItemType item )
{
cout << " here1";
/*if( isFull() )
{
cout << "\n\t<- will not be entered LIST FULL -------\n";
return;
}*/
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = item;
NodeType<ItemType>* location;
location = ListData;
if( location == NULL ) //empty list
{
cout << " here2";
newNode->next = newNode;
newNode->back = newNode;
ListData = newNode;
}
else
{
cout << " here3";
while( location->next != ListData)
{
cout << " here4";
location = location->next;
}
cout << " here5";
location->next = newNode;
newNode->back = location;
newNode->next = ListData;
ListData->back = newNode;
}
length++;
cout << " here6";
return;
}
template <class ItemType>
void DLLCType<ItemType>::deleteItem(int pos)
{
NodeType<ItemType>* location;
location = ListData;
if (pos < 1)
{
cout << "Invalid position: Position has to be greator than or equal to 1";
return;
}
if (pos > length)
{
cout << "Invalid position: These many items do not exist";
return;
}
else
{
if( location == NULL )
{
cout << "Empty list";
return;
}
else
{
if ( length == 1)
{
delete ListData;
ListData = NULL;
length = 0;
}
else
{
for(int i = 0 ; i < pos-1 ;i++)
{
location= location->next;
}
location->back->next = location->next;
location->next->back = location->back;
ListData = location->next;
delete location;
length--;
}
}
}
}
int main()
{
DLLCType<int> intList;
cout << "Numbers as inserted into integer List" << endl;
int i = 0;
for( i=0; i<2; i++)
{
//int temp = rand() % 997 ;
intList.InsertItem( i+1 );
// cout << temp << " ";
}
cout << "\n\n\n ---------- Print List -----------\n";
//intList.PrintList();
cout << "\n ---------- delete item -----------\n";
cout << "delete position 0\n";
//intList.deleteItem( 1 );
cout << "\n\n\n ---------- Print List after delete-----------\n";
//intList.PrintList();
return 0;
}
This program goes never terminates !! Can you help me figure out why ?
Thanks!!
Please use code tags.
Infinite loop in destructor??
you are not decreasing the length variable in the destructor.
include "--length" inside the while loop in the destructor and it fixes your problem.