bmcisme
bmcisme

Reputation: 57

Assignment Operator not working properly in circular doubly linked list

I'm doing a doubly linked list with a sentinel node that makes the doubly linked list a circular list (there is no head and no tail pointers to the front and back, instead head is referred to by m_sentinel->m_next and tail is referred to by m_sentinel->m_prev). Here is the following code:

In MyList.h:

template <typename T>
class Node
{
    public:
        T m_element;

        Node<T> *m_prev;
        Node<T> *m_next;

        // Helps make a dummy/sentinel/junk node
        Node(Node<T> *in_prev, Node<T> *in_next): 
            m_prev(in_prev), m_next(in_next){}

        Node(const T &x, Node<T> *in_prev, Node<T> *in_next): 
            m_element(x), m_prev(in_prev), m_next(in_next){}
};

template <typename T>
class MyList
{
    private:
        Node<T> *m_sentinel = nullptr;

        int m_size;

    public:
        MyList();

        ~MyList();

        MyList<T> & operator=(const MyList<T> &source);

        void clear();

        void push_back(const T &x);

In MyList.hpp:

template <typename T>
MyList<T>::MyList()
{
  m_size = 0;
  m_sentinel = new Node<T>(NULL, NULL);
}

template <typename T>
MyList<T>::~MyList()
{
  clear();

  m_size = 0;
}

template <typename T>
MyList<T> & MyList<T>::operator=(const MyList<T> &source)
{
  if(this == &source)
  {
    return *this;
  }
  while(source.m_sentinel->m_next != source.m_sentinel)
  {
    Node<T> *temp = source.m_sentinel->m_next;
    push_back(temp->m_element);
    source.m_sentinel->m_next = temp->m_next;
  }

  return *this;
}

template <typename T>
void MyList<T>::clear()
{
  if(m_sentinel->m_prev == NULL && m_sentinel->m_next == NULL)
  {
    delete m_sentinel;
  }
  else
  {
    int k = size();
    for(int i = 0; i < k; i++)
    {
      pop_back();
    }
    delete m_sentinel;
  }
}

template <typename T>
void MyList<T>::push_back(const T &x)
{
  Node<T> *newNode;
  newNode = new Node<T>(x, NULL, NULL);
  if(m_sentinel->m_prev == NULL && m_sentinel->m_next == NULL)
  {
    newNode->m_prev = m_sentinel;
    newNode->m_next = m_sentinel;
    m_sentinel->m_prev = newNode;
    m_sentinel->m_next = newNode;
  }
  else
  {
    newNode->m_next = m_sentinel;
    newNode->m_prev = m_sentinel->m_prev;
    Node<T> *temp = newNode->m_prev;
    m_sentinel->m_prev = newNode;
    temp->m_next = m_sentinel->m_prev;
  }
  m_size++;
}

In main.cpp:

#include "MyList.h"

int main()
{
    MyList<int> x;
    x.push_front(1);
    x.push_front(2);
    x.push_front(3);
    x.push_front(4);
    x.push_front(5);
    x.push_front(6);
    x.push_front(7);
    MyList<int> p;
    p = x; 

    // Below just outputs each linked list
    int j = 0;
    int m = x.size();
    cout << endl << endl;
    for(auto i = 0; i < m; i++)
    {
        cout << x.front() << endl;
        x.pop_front();
        j++;
    }
    cout << endl << endl;
    j = 0;
    m = p.size();
    for(auto i = 0; i < m; i++)
    {
        cout << p.front() << endl;
        p.pop_front();
        j++;
    }
    cout << endl << endl;

When running this code, x does get copied to p successfully. When outputting p, the following output is given: 7 6 5 4 3 2 1, but when outputting x, the following output is given: -19823746 ... which is just junk memory values (which tells me x is being changed obviously, but p is successfully getting x's contents). I have no clue why it's changing; I'm looking for a fix/solution to the assignment operator because it's not working properly.

Upvotes: 1

Views: 359

Answers (1)

David G
David G

Reputation: 96810

Inside of your MyList::operator= you should be iterating with a new variable, not the source.m_sentinel member itself. This changes the actual list and is actually removing nodes.

Use this instead:

Node<T>* current = source.m_sentinel;
while (current->m_next != source.m_sentinel) {
  Node<T>* temp = current->m_next;
  push_back(temp->m_element);
  current = temp;
}

Upvotes: 1

Related Questions