You have a large number of memory leaks.
1 2 3 4 5
|
Node *traversalNode = new Node;
traversalNode = head;
Node *newNode = new Node;
Node *previousNode = new Node;
| |
The first line accesses new, anonymous, memory space via a pointer ... then loses all access to that memory space by pointing elsewhere. I suspect you meant
Node *traversalNode = head;
but who knows? You don't need a 'new' every time you declare another pointer! Your code suggests that you are under a misapprehension here. That pointer is only required to point to a pre-existing memory location. (I personally prefer the terminology that other languages use: "allocate" rather than "new", to convey the fact that it is used only where extra memory is needed to store something.)
The third and fourth lines also (unnecessarily) create new memory slots via pointers ... but, again, all access to those memory slots is lost later, because the pointers are redirected elsewhere. Maybe you meant something like
1 2
|
Node *nextNode = traversalNode->next;
Node *previousNode = traversalNode->prev;
| |
You could - but don't - make any use of these pointers later.
I don't think you help yourself by calling a variable
newNode; one stray blank in the middle ...
In your code snippet
1 2 3 4 5 6
|
head->currentIndex = 1;
tail->prev = head;
tail->next = head;
tail->data = 0;
head->currentIndex = 0;
| |
you are assigning head->currentIndex twice, whilst poor old tail->currentIndex is forgotten.
You don't usually traverse linked lists by counting indices - you usually end when the next pointer is a nullptr.
May I suggest that, to get better help in the forum you:
- combine into a single file, so that it can be run immediately in cpp.sh; you can split it between files LATER if really necessary;
- remove all the extra comments and blank lines, which are confusing and distracting;
- sit down with a piece of paper and draw boxes for each anonymous piece of memory that is accessed with a 'new' statement, making sure that it is permanently pointed to by something (until 'delete'd).
Linked lists are definitely better understood when their operations are drawn out with diagrams.
You can definitely use this forum as an educational resource - typing "doubly linked list" into the search box will pull up many previous posts, and you can observe how linked lists have been built and traversed.