Hello, I was wondering if I could get any suggestions to improve the code below. The assignment is to create a circular doubly-linked list with a dummy node. We had to create the constructor, copy constructor, assignment operator, destructor, and a swap method ( along with other methods). The program compiles for me and I was able to get past all the checks required, though I'm not 100% confident that the implementation below takes care of memory leaks.
Any and all input is appreciated, thanks.
Code:
class List
{
public:
List();
List(const List& other);
~List();
List& operator=(const List& right);void List::swap(List& other);
private:
struct node
{
int data;
node* next;
node* prev;
};
node* head;
node* temp;
int mSize;
};
Code:
List::List()
:mSize(0), temp(NULL)
{
head = new node; //dummy node
head->next = head;
head->prev = head;
}
List::List(const List& other)
:mSize(0), temp(NULL)
{
head = new node; //dummy node
head->next = head;
head->prev = head;
int in = 0;
for(int i = 0; i < other.mSize; i++)
{
bool buffer = insert(i, in);
}
node* tmp = other.head->next;
for(temp = head->next; tmp != other.head; temp = temp->next, tmp = tmp->next)
{
temp->data = tmp->data;
}
tmp = NULL;
}
List::~List()
{
temp = head->prev;
while(temp != head)
{
temp = temp->prev;
delete temp->next;
}
delete head;
}
List& List::operator=(const List& right)
{
if(this != &right)
{
List tmp(right);
swap(tmp);
}
return *this;
}
void List::swap(List& other)
{
node* temp = head;
head = other.head;
other.head = temp;
temp = NULL;
}