View Single Post
Old 01-31-2005, 08:23 AM   #3 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Thanks, revised code:

Good eye. I made 2 updates (constructor, deletion of tempPtr in destructor) and to me it looks OK. I can't compile this yet or even spend more than 5 minutes working on it ATM because I'm also trying to do some C++ homework right now. I should be done in an hour or so, and if my constructor/destructor look good then I'm going to write the appendHead and appendTail member functions.

Code:
struct Node
{
    int data;
    Node* nextPtr;
};

class LinkedList
{
public:
    LinkedList() : nodePtr->nextPtr(0) {  }
    ~LinkedList();

private:
    Node* nodePtr;
};

LinkedList::~LinkedList()
{
    Node* tmpPtr;
    while(nodePtr->nextPtr != 0)
    {
        tmpPtr = nodePtr;
        nodePtr = nodePtr->nextPtr;
        delete tmpPtr;
    }
}
Does that look better? I basically made a typo in the destructor the first time, if my destructor's right then that's what I was trying to do all along... I gotta watch out for those typing errors. And can I use the arrow operator in the member initializer list of the constructor like I'm doing? I don't really think that any data should be filled in to the first node, I'm thinking right now that I will use that as a head node - a pointer to the first element in the List, instead of it being the first node in the List.

Thanks for your help.

Last edited by fp_unit; 01-31-2005 at 12:01 PM.
fp_unit is offline   Reply With Quote