I have been trying to store head & tail of linked list into vector and use it as container. so vector will have { head_1stLL, tail_1stLL, head_2ndLL, tail_2ndLL ...}
Problem is when I tried to clean up linked list in this vector. I could not find a way to delete linked list. After CleanVector is called I use target.clear(). vector appear to be empty but still I got memory allocated to previous Linked Lists and was able to access all the contents.
Before I overhaul my code to use 2D vector for simplicity, I would love to know what went wrong.
struct node
{
Document doc;
node* next;
node* prev;
char LBnType; //Leg type and base definition
}
void cleanVector(std::vector <node*> &target)
{
node* temp;
if(target.size()%2 ==0 )
{
while(!target.empty())
{
node* head = target.front();
temp = head;
target.erase(target.begin());
node* tail = target.front();
target.erase(target.begin());
deepCleanLL(head, tail);
if(temp != NULL) // here it says not deleted
{
cout << "FAILED TO DELETE" << endl;
printList(temp); //this was to test if I can still access LL
}
}
}
else
cout << "ERROR: nodeDeepCleanVector. Target size not even" << endl;
}
void deepCleanLL(node* &begin, node* &end)
{
node* current = end;
node* temp = begin;
while(current == NULL)
{
node* currentLag = current;
current = current->prev;
current->next = NULL;
current->prev = NULL;
delete currentLag;
currentLag = current;
}
if(temp != NULL)
{//temp here says LL is cleaered
cout<< "delete failed" << endl;
printList(temp);
}
}