linked list confusion

Does this correctly delete a linked list?

This code is included to show what everything is:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef NodeType* nodePtr;
struct NodeType
{
       dataType component;
       nodePtr link;
};

void Container::firstItem(dataType itemValue)
{
     head = new NodeType;
     head->component = itemValue;
     count++;
}

head is defined in the header file as a NodeType*.
and typedef int dataType; is in the header also.

This is the code i am trying to delete with:
1
2
3
4
5
6
7
8
9
10
11
12
void Container::deleteList(void)
{
     nodePtr tempPtr;
     
     while ( head != NULL )
     {
           tempPtr = head;
           head = head->link;
           delete tempPtr;
           
     }    
}


I have a function that adds items to the list then I want to be able to run this delete function to delete the entire list, so i dont have any memory leaks.
Also, i had another problem just come up.

bool operator ==(Container a, Container b);

this piece of code keeps giving me the error "must take exactly one argument". It is inside the Container class and I want to use it to see if two classes are equal.

the implimitation is:

1
2
3
4
5
bool operator ==(Container a, Container b)
{
     return
           a.head == b.head;
}

deleteList looks correct. firstItem is wrong because it does not initialize head->link.

declare it as

1
2
3
friend bool operator==( const Container& a, const Container& b ) {
 // ...
}

Topic archived. No new replies allowed.