I can't seem to wrap my head around understanding how to assign the head and tail of a doubly linked list. Trying to get the remove function to work to no avail.
I'm fairly certain the problem lies in the LinkedList::insert()
and LinkedList::remove()
functions, specifically something to do with the head and tail properties of the LinkedList class.
One of the things I don't quite understand how to get it to work is what to assign to the current
variable in the LinkedList::remove()
function. If I use head
, then it assigns the previous node's value. If I use head.next
, I get an exception thrown. The other thing is how (if at all) to get previous
to work. Also throws an exception.
I've looked at the other examples in StackOverflow, but was unable to find the answers I need.
Node header file
#pragma once
class Node
{
public:
int data;
Node* next;
Node* previous;
};
Class header file
class LinkedList
{
private:
int length;
Node* head;
Node* tail;
public:
LinkedList();
void remove(int deleted);
void insert(int data);
void display();
int getLength();
};
LinkedList C++ file
#include <iostream>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
//Define variables used in the class
LinkedList::LinkedList()
{
length = 0;
head = NULL;
tail = NULL;
}
//Define the remove function
void LinkedList::remove(int deletedNode)
{
struct Node* current = head;
while (current)
{
if (current->data == deletedNode)
{
if (current->next == NULL)
{
current->previous->next = NULL;
current = NULL;
}
else if (head == NULL)
{
current->next->previous = NULL;
current = NULL;
}
else
{
current->previous->next = current->next;
current->next->previous = current->previous;
current = NULL;
}
}
current = current->next;
}
}
//Define insert function
void LinkedList::insert(int num1)
{
Node* node = new Node(); //Create new node
node->data = num1; //Assign new number to node's data variable
node->next = head; //Assign the current contents of the head variable to the new node's next pointer
node->previous = tail; //Assign the current contents of the tail variable to the new node's previous pointer
head = node; //Assign the new node to the head variable
tail = node->previous;
length++; //Increase the list's length by one
}
//Define display function
void LinkedList::display()
{
Node* curr = this->head;
int i = 1;
while (curr)
{
cout << "Value of node #" << i << " is " << curr->data << endl;
curr = curr->next;
i++;
}
}
//Define getLength function
int LinkedList::getLength()
{
return length;
}
Main C++ file
#include <iostream>
#include "LinkedList.h"
#include "Node.h"
#include <time.h>
using namespace std;
int main()
{
int userRemove = 1;
LinkedList list;
// Define & start clock
clock_t start, end;
start = clock();
for (int i = 1; i < 101; i++)
{
list.insert(rand() % 101);
}
// Display list
list.display();
// End clock
end = clock();
//Display duration it took to display list
cout << endl << "It took " << (end - start) << " milliseconds to list & display all nodes." << endl;
//Display total number of nodes
int len = list.getLength();
cout << endl << "# of nodes = " << len << endl;
//Ask user for node number to remove
while (userRemove != 0)
{
cout << endl << "Please enter the number of the node you wish to delete or press '0' to exit: " << endl;
cin >> userRemove;
list.remove(userRemove);
cout << endl << "The first node containing " << userRemove << " has been removed." << endl;
//Display list and list length after removal
list.display();
cout << endl << "# of nodes = " << len << endl;
}
}