okay guys I have an updated version of the List class. I am trying to work on the destructors, but I am having a bit of bad luck. here is the code and then I will explain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#ifndef LIST_H
#define LIST_H
#include <cstdlib>
template <typename E> class List {
private:
template <typename T> struct ListNode {
T value;
ListNode<T>* next;
ListNode<T>* prev;
//template constructor for the structure
ListNode(const T &object, ListNode<T> *nextNode = NULL,
ListNode<T> *prevNode = NULL) : value(object),
next(nextNode), prev(prevNode) { }
//delete the data contained in the node
~ListNode() {
if(prev != 0) //Not NULL
// delete prev; //if I leave this line in, I receive a segmentation fault.
if(next != 0) //Not NULL
delete next;
}
};
ListNode<E>* head; //root ListNode
ListNode<E>* tail;
public:
List();
~List();
void insertTail(const E &object); //insert and object at end of the list
void insertHead(const E &object); //insert and object at front of the list
void removeNode(E &object); //remove a ListNode containing "object"
void toString();
};
template <typename E> List<E>::List() {
head = NULL;
tail = NULL;
}
template <typename E> void List<E>::insertTail(const E &object) {
if(head == NULL)
head = new ListNode<E>(object); //Only called when the list is first initialized
else {
ListNode<E> *temp = head;
while(temp->next != NULL) { //iterate to the end of the list
temp = temp->next;
}
temp->next = new ListNode<E>(object);
temp->next->prev = temp; //prev equal to the address of temp
}
}
template <typename E> List<E>::~List() {
ListNode<E> *current = head;
ListNode<E> *next;
while(current->next != NULL) {
next = current->next; //get the address of the next node so when current
delete current; //is deleted we can keep iterating through the list
current = next;
}
head = 0; //set head to NULL
}
#endif /* LIST_H */
|
A couple of thing I am not sure about.
1) For the list node constructor, it takes in an address of an object
const T &object
, so how do I delete this in the destructor. I assign it to
T value
which holds the address of the object passed in correct? I should be able to call
delete value
. When I do this the compiler tells me it expects a pointer, so I have tried *value, &value, and still no luck.
2)In the ListNode destructor if I try to
delete prev
i always receive a segmentation fault. That means I am trying to access bad memory or a null pointer. I check for null and it still happens. Maybe I am overlooking something. Also if I
delete next and prev
does it delete the next node, so when it returns to the List destructor will
next
have already been deleted.
3) Do i need to have my functions take parameters such as
const E &object
. Is it so that the address does not get changed? Why not just have it as
const E *object
.
thanks guys I know it is a lot of questions, and I really appreciate the help.
EDIT: I ran my code through the Netbeans debugger and for some reason my destructor for ListNode is being call recursively. That is why I am receiving a segmentation fault when I have
delete prev
included. Any clues as to why this is happening?