Doubly Linked List issues (with iterators)

I'm currently making a Doubly Linked Lists using Iterators. I'm not sure whether it's my addhead/addtail functions that aren't working, or my iterator accessing the functions when I display it. Could someone help me? (btw: I want the data members, functions, parameters, all the same, just the logic in the functions fixed)

#pragma once
template<typename Type> class DLLIter;

template<typename Type> class DLList
{// Function : Constructor

public:

friend class DLLIter<Type>;


struct Node
{
Type element;
Node* next;
Node* prev;

Node(const Type& v, Node* n = NULL, Node* p = NULL)
{
element = v;
next = n;
prev = p;
}
};

Node* head;
Node* tail;
unsigned int Size;

public:
DLList()
{
tail = nullptr;
head = nullptr;
Size = 0;
}
/////////////////////////////////////////////////////////////////////////////
// Function : Destructor

~DLList()
{
while(head != nullptr)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
/////////////////////////////////////////////////////////////////////////////
// Function : Assignment Operator

DLList& operator=(const DLList<Type>& that)
{
if(this != &that)
{
this->clear();
if(that.head != nullptr)
{
head = new Node(that.head->element,nullptr);
Node * copying = that.head->next;
Node * copied = this->head;
while(copying)
{
copied->next = new Node(copying->element,nullptr);
copied = copied->next;
copying = copying->next;
}
tail = new Node(copying->element,nullptr,copying->prev);
Size = that.Size;
}
}

return *this;
}
/////////////////////////////////////////////////////////////////////////////
// Function : Copy Constructor

DLList(const DLList<Type>& that)
{
head = nullptr;
tail = nullptr;
this = that;
}
/////////////////////////////////////////////////////////////////////////////
// Function : addHead
// Parameters : v - the item to add to the head of the list
/////////////////////////////////////////////////////////////////////////////
void addHead(const Type& v)
{
Node*n = new Node(v, head);

if(!tail)
{
tail = n;
}
else
head->prev = n;
head = n;
++Size;
}
/////////////////////////////////////////////////////////////////////////////
// Function : addTail
// Parameters : v - the item to add to the tail of the list
/////////////////////////////////////////////////////////////////////////////
void addTail(const Type& v)
{
Node * n = new Node(v,nullptr,tail);
if(!head)
{
head = n;
}
else
tail->next = n;
tail = n;
++Size;

}
/////////////////////////////////////////////////////////////////////////////
// Function : clear
// Notes : clears the list, freeing any dynamic memory
/////////////////////////////////////////////////////////////////////////////
void clear()
{
while(head != nullptr)
{
Node * temp = head;
head = head->next;
delete temp;
}
tail = head = nullptr;
Size = 0;
}

};

template<typename Type> class DLLIter
{

friend class DLList<Type>;

typename DLList<Type>::Node* curr;

DLList<Type>* theList;

public:
/////////////////////////////////////////////////////////////////////////////
// Function : Constructor
// Parameters : listToIterate - the list to iterate
/////////////////////////////////////////////////////////////////////////////
DLLIter(DLList<Type>& listToIterate)
{
theList = &listToIterate;
beginHead();
}
/////////////////////////////////////////////////////////////////////////////
// Function : beginHead
// Notes : moves the iterator to the head of the list
/////////////////////////////////////////////////////////////////////////////
void beginHead()
{
curr = theList->head;
}
/////////////////////////////////////////////////////////////////////////////
// Function : beginTail
// Notes : moves the iterator to the tail of the list
/////////////////////////////////////////////////////////////////////////////
void beginTail()
{
curr = theList->tail;
}
/////////////////////////////////////////////////////////////////////////////
// Function : end
// Notes : returns true if we are at the end of the list, false otherwise
/////////////////////////////////////////////////////////////////////////////
bool end() const
{
return (curr == theList->tail);
}
/////////////////////////////////////////////////////////////////////////////
// Function : operator++
// Notes : move the iterator forward one node
/////////////////////////////////////////////////////////////////////////////
DLLIter<Type>& operator++()
{
if(curr != nullptr)
curr = curr->next;
return *this;
}
/////////////////////////////////////////////////////////////////////////////
// Function : operator--
// Notes : move the iterator backward one node
/////////////////////////////////////////////////////////////////////////////
DLLIter<Type>& operator--()
{
if(curr!= nullptr)
{
curr = curr->prev;
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////
// Function : current
// Notes : returns the item at the current iterator location
////////////////////////////////////////////////////////////////////////////
Type& current() const
{
return curr->element;
}

};

--------------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
// LEAK DETECTION
////////////////////////////////////////////////////////////////////////////////
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>


////////////////////////////////////////////////////////////////////////////////
// INCLUDES
////////////////////////////////////////////////////////////////////////////////
#include "DLList.h"
#include <iostream>
#include <string>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
// Function : forward
// Parameters : list - the DLList to print
// Return : void
////////////////////////////////////////////////////////////////////////////////
template<typename Type>
void forward(DLList<Type>& list)
{
DLLIter<Type> iter(list);
cout << "FWD { ";
for(iter.beginHead(); !iter.end(); ++iter)
cout << iter.current() << ' ';
cout << " }\n";
}

////////////////////////////////////////////////////////////////////////////////
// Function : backward
// Parameters : list - the DLList to print
// Return : void
////////////////////////////////////////////////////////////////////////////////
template<typename Type>
void backward(DLList<Type>& list)
{
DLLIter<Type> iter(list);;
cout << "BWD { ";
for(iter.beginTail(); !iter.end(); --iter)
cout << iter.current() << ' ';
cout << " }\n";
}

////////////////////////////////////////////////////////////////////////////////////////////////
// Function : main
// Parameters : argc - the number of command line arguments
// argv - the array of command line arguments
// Return : int - 0 for success
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv)
{
////////////////////////////////////////////////////////////////////////////////
// LEAK DETECTION
////////////////////////////////////////////////////////////////////////////////
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(-1L);


cout << "**********************************************************************\n";
cout << "** TEST FOR DOUBLY LINKED LIST AND ITERATOR **\n";
cout << "**********************************************************************\n\n";


// Declarations

DLList<string> list;
DLList<string> list2;
DLList<string>* list3;
DLLIter<string> iter(list);
DLList<int>* testPointer;

// TEST 1 - adding to the head and tail

cout << "*** TEST 1 ***\n\n";
list.addHead("works");
list.addHead("this");
list.addHead("way");
backward(list);
list.clear();
list.addTail("works");
list.addTail("that");
list.addTail("way");
forward(list);
list.clear();

// SUCCESS
system("pause");
return(0);



the output is supposed to be

BWD : {works this way}
FWD : {works that way}

but my output is currently

BWD : {}
FWD : {works that}
Topic archived. No new replies allowed.