I'm working on an implementation of the Double-Ended Queue as a Doubly-Linked List (for personal enrichment), and I was wondering if anyone minded taking a look at my Constructor/Destructor and Empty functions to see if I'm on the right track. It should be self-explanatory enough on it's own (I hope).
DeQueue::DeQueue() {
// set up the sentinels
head = new QueueItem();
tail = new QueueItem();
head->next = tail;
tail->last = head;
head->data = NULL;
tail->data = NULL;
tail->next = head->last = NULL;
}
DeQueue::~DeQueue() {
QueueItem* current = head;
QueueItem* next;
current = current->next;
for (;current != tail; current = current->next){
delete current->last;
}
delete current;
}
bool DeQueue::Empty() {
return tail->last == head;
}
The idea is that the two head
and tail
sentinels will always remain at the ends, so the easiest way to check if it is empty is to determine if head
points to tail
or vice versa. Also, in my destructor, I start at the tail and delete everything from there back, sentinels included. Any suggestions?
EDIT: The destructor now moves forward one node, deletes what's behind it, then moves on. Sound like a better approach?