-2

I have compiled my code and it seemed to work correctly. But out of no-where I get the error (this->tail was nullptr). I have tried changing the creation of the new node. But nothing seems to work. I cannot tell where tail is being set to nullptr and messing up the code.

How would I go about fixing this problem? Is there any way to set tail to non-nullptr without ruining every other function? I am not too familiar with exception throwing, so if you could explain the situation it would help a lot.

#ifndef MYDLL_H
#define MYDLL_H

#include <iostream>
#include <new>
using namespace std;

class MyDLL
{
    struct Node
{
    int i;
    Node* next;
    Node* prev;
};

Node* head;
Node* tail;

public:
MyDLL();
~MyDLL();
void append(int);
void remove(int);
bool find(int) const;
void clear();
void print() const;
void reverse() const;
};

MyDLL::MyDLL()
{
head = nullptr;
tail = nullptr;
}
MyDLL::~MyDLL()
{
clear();
}

void MyDLL::append(int i)
{
    Node *n = new Node{ i, nullptr, nullptr };
if (head = nullptr)
{
    head = n;
    tail = n;
}
else
{
    n->prev = tail;
    tail->next = n; **<--- This is where the exception thrown error is showing up**
    tail = n;
}
}

void MyDLL::remove(int i)
{
Node* p = head;
Node* q = tail;

while (p != nullptr && p->i != i)
{
    q = p;
    p = p->next;
}

if (p = nullptr)
{
    return;
}

if (q = nullptr)
{
    head = p->next;
}
else
{
    q->next = p->next;
}

if (p->next = 0)
{
    tail = q;
}
else
{
    p->next->prev = q;
}
delete(p);
}

bool MyDLL::find(int i) const
{
Node* p = tail;

while (p != nullptr)
{
    if (p->i = i)
    {
        return (true);
    }
    p = p->prev;
}
return (false);
}

void MyDLL::clear()
{
while (tail != nullptr)
{
    Node* p = tail;
    tail = p->prev;
    delete (p);
}
head = nullptr;
}

void MyDLL::print() const
{
Node* p = head;

while (p)
{
    cout << p->i << "\t";
    p = p->next;
}
cout << "\n";
}

void MyDLL::reverse() const
{
Node* p = tail;

while (p)
{
    cout << p->i << "\t";
    p = p->prev;
}
cout << "\n";
}
#endif

int main()
{
    MyDLL list;
list.append(5);
list.append(6);
list.append(7);
list.append(8);

list.print();
list.reverse();
cout << system("pause");
}
John Doee
  • 207
  • 4
  • 15
  • 1
    Here's a tip for finding and solving problems easier: Do everything step by step. Take small baby-steps, implement just a small part of the code, and test it fully to make sure it works before continuing to the next step. That way it will become much easier to pinpoint when and where you introduce bugs into your code, and also to solve them. [Some related reading](https://ericlippert.com/2014/03/21/find-a-simpler-problem/). – Some programmer dude Oct 11 '18 at 05:51
  • I also suggest you take some time to do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). Explain your code, each line of it, in detail to your (real or imaginary) "rubber duck" (or friend if you can't find a rubber duck). Of course, that requires you to know the difference between e.g. assignment using `=` and comparison for equality with `==`. – Some programmer dude Oct 11 '18 at 05:53
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Oct 11 '18 at 06:13
  • On a side note: you should use clear variable names, for example why not have `Node* headNode = head; Node* tailNode = tail;` instead of `Node* p = head; Node* q = tail;` in `remove`? – Jabberwocky Oct 11 '18 at 07:24

2 Answers2

1

This code is exactly why you want to use nullptr = tail instead of tail = nullptr. In particular, every time you "check" for tail to be a null pointer, you are assigning nullptr to tail, and then assign operator returns a value which gets then implicitly casted to a boolean value, giving you no errors. But the error is actually there. Replace the "=" operator with "==" when performing a comparison, unless you actually have a reason for assigning and checking the return value.

-1

Please fix = with == in your code.

void MyDLL::append(int i)
{
    Node *n = new Node{ i, nullptr, nullptr };
    if (head == nullptr)

It is always recommended to do like the reverse comparison (nullptr == head)