Okay I am creating a doubly linked list with simple functions for head insert, tail insert, head remove, tail remove and a few others. head insert and remove seem to be working fine and the tail insert does as well but for some reason my tail remove causes a segfault whenever it is trying to remove the last file in a list. So if I have a list of 3 integer 5 6 7 it removes 7 fine then 6 but seg faults as soon as it tail points to 5. The debugger reports a seg fault but when I try tpo print the list it spits out random numbers. I have included almost all my code, starting with the offending.
rear_remove(problem area):
Code:
//tail remove
template <class T>
void Dlist<T>::rear_remove()
{
if(tail != NULL){
Dnode<T> * removeptr;
removeptr = tail;
tail = tail->prev();
if(tail != NULL) //can't use set_next if tail is not pointing to a node (thought this would fix the problem
tail->set_next(NULL);
delete removeptr;
}
}
front_insert(being used to populate the list):
Code:
//head insert
template <class T>
void Dlist<T>::front_insert(const T& entry)
{
if(head == NULL)
head = tail = new Dnode<T>(entry);
else {
Dnode <T> * temp;
temp = new Dnode<T>(entry);
temp->set_next(head);
head->set_prev(temp);
head = temp;
}
}
header file for Dlist - dlist.h:
Code:
#include "Dnode.h"
#include <iostream>
#include <string>
template <class T>
class Dlist{
public:
Dlist();
Dlist(const Dlist& other);
~Dlist();
/*
void list_clear(Dnode<T>*& head_ptr);
void list_copy(const Dnode<T>* source_ptr, Dnode<T>*& head_ptr, Dnode<T>*& tail_ptr);
std::size_t list_length(const Dnode<T>* head_ptr);
*/
void rear_insert(const T& entry);
void rear_remove();
void front_remove();
void front_insert(const T& entry);
void print();
private:
Dnode<T>* head;
Dnode<T>* tail;
};
#include "Dlist.template"
and lastly Dnode.h if it helps:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <iterator>
template <class T>
class Dnode{
public:
Dnode(T d = T(), Dnode *n = NULL, Dnode *p = NULL){datafield = d; link_next = n; link_prev = p;}
T& data(){return datafield;}
Dnode * next(){return link_next;}
Dnode * prev(){return link_prev;}
void set_data(T d){datafield = d;}
void set_link(Dnode * n = NULL, Dnode * p = NULL){link_next = n; link_prev = p;}
void set_next(Dnode * n = NULL){link_next = n;}
void set_prev(Dnode * p = NULL){link_prev = p;}
private:
T datafield;
Dnode * link_next;
Dnode * link_prev;
};
sorry if this is a lot to wade through I just wanted to make sure I included everything to make my question understandable. thanks for taking the time to look through this
-ac