I am trying to implement my first LinkedList class in C++ and I'm having trouble with deleting a pointer.
LinkedList.h:
#pragma once
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include "ListNode.h"
class LinkedList
{
private:
int theSize;
ListNode* head;
ListNode* tail;
public:
LinkedList();
~LinkedList();
LinkedList(const LinkedList&);
const LinkedList& operator=(const LinkedList&);
void push_front(const Line&);
void push_back(const Line&);
void pop_front();
void pop_back();
int const size();
bool const empty();
void insert(const Line&, int);
void remove(int);
void print();
void print(int);
};
#endif //!LINKEDLIST_H
part of LinkedList.cpp:
LinkedList::LinkedList() : theSize{ 0 }, head{ nullptr }, tail{ nullptr } {
}
//Destructor: Need to go through entire list and delete one by one
LinkedList::~LinkedList() {
if (!empty()) {
cout << "Destroying List" << endl;
ListNode* currPtr{ head };
ListNode* tempPtr{ nullptr };
while (currPtr != nullptr) {
tempPtr = currPtr;
currPtr = currPtr->next;
delete tempPtr;
}
}
}
//LinkedList::LinkedList(const LinkedList& ll) {
//
//}
//const LinkedList& LinkedList:: operator=(const LinkedList& ll) {
//
//}
void LinkedList::push_front(const Line& l) {
ListNode* ln = new ListNode(l);
if (empty())
head = tail = ln;
else {
ln->setNext(*head);
(*head).setPrev(*ln);
head = ln;
}
theSize++;
}
void LinkedList::push_back(const Line& l) {
ListNode* ln = new ListNode(l);
//If List is empty, head and tail will point to one node
if (empty())
head = tail = ln;
else {
(*tail).setNext(*ln);
(*ln).setPrev(*tail);
tail = ln;
}
theSize++;
}
void LinkedList::pop_front() {
if (empty())
return;
else {
ListNode* tempPtr = head;
if (head == tail) //If there is only one Node in the List
head = tail = nullptr;
else
head = head->next;
delete tempPtr;
}
//theSize--;
}
ListNode.h:
#pragma once
#ifndef LISTNODE_H
#define LISTNODE_H
#include<iostream>
#include"Line.h"
//using namespace::std;
class ListNode
{
friend class LinkedList;
private:
Line data;
ListNode* next;
ListNode* prev;
public:
ListNode();
~ListNode();
ListNode(const Line&);
ListNode(const Line&, ListNode*, ListNode*);
Line& getData();
ListNode& getNext();
ListNode& getPrev();
void setData(const Line&);
void setNext(ListNode&);
void setPrev(ListNode&);
friend ostream& operator<<(ostream&, const ListNode&);
friend istream& operator>>(istream& input, ListNode&);
};
The pop_front() method simply removes the first Node in the LinkedList:
void LinkedList::pop_front() {
if (empty())
return;
else {
ListNode* tempPtr = head;
if (head == tail) //If there is only one Node in the List
head = tail = nullptr;
else
head = &((*tempPtr).getNext()); //getNext() returns a Node&
//delete tempPtr;
}
}
In my main I create a LinkedList and add a few nodes, which works well. The pop_front() method works fine until I include the delete statement at the end, in which time I get a run time error. I can't seem to understand why it is not working. This is for a doubly linked list.
See Question&Answers more detail:os