Writing a Destructor that deletes every node in the list

I was wondering how would i delete every node in the list

Page 1 of 1

4 Replies - 1025 Views - Last Post: 22 February 2009 - 02:27 PM Rate Topic: -----

#1 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Writing a Destructor that deletes every node in the list

Posted 22 February 2009 - 12:49 PM

I have everything in my program working except my destructor, i was wondering how would it be possible for me to delete every node in the list with my destructor



typedef double ListType;


struct Node
{
		ListType  data;
		Node *next;
};


class List
{

public:

	 List();
	 ~List();
	ListType ListMean();
	int ListRange(ListType High, ListType Low);
	int ListCount();
	ListType ListLow();
	ListType ListHigh();
	bool ListAdd(ListType Grade);
	ListType ListSum();



private:

	Node *head;
	ListType high;
	ListType sum;
	ListType mean;
	int count;


	
};



List.cpp
#include "List.h"
#include<iostream>


List::List()
{
	head = NULL;
}

bool List::  ListAdd(ListType Grade)
{
	Node *current;
	Node *temp;

	List();

	if(head == NULL)
	{
		head = new Node;
		head ->data = Grade;	//Puts the data in the new node
		head->next = NULL;

		return true;

	}
	else
	{
		current = head;

		while(current ->next != NULL)
			current = current ->next;


		temp = new Node;
		temp->data = Grade;		  
		temp->next = NULL;
		current->next = temp;

		return true;


	}

	return false;
}

int List::ListCount()
{
	count = 0;


	//Assume P points to the first node in the list
	for (Node * curr = head; curr != NULL; curr = curr->next)
		count++;
	return count;


}
ListType List::ListHigh()
{
	Node *current= head;


	double lowest = 0, highest = 0;
	lowest = highest = current->data;

	while(current->next != NULL) 
	{
		if (current->data < lowest)
			lowest = current->data;	

		else if (current->data > highest)	
			highest = current->data;

		current = current->next;


	}
	return highest;

}
ListType List::ListLow()
{


	Node *current= head;


	double lowest = 0, highest = 0;

	lowest = highest = current->data;		//assign current ptr value to both lowest and highest

	while(current->next != NULL) 
	{
		if (current->data < lowest)
			lowest = current->data;		

		else if (current->data > highest)	
			highest = current->data;

		current = current->next;
	}


	return lowest;


}
ListType List::ListMean()
{
	ListType total = 0;

	for (Node * curr = head; curr != NULL; curr = curr->next)
		total += curr->data;

	mean = total / count;



	return mean;
}

int List::ListRange(ListType High, ListType Low)
{
	int grade_count = 0;


	for (Node * curr = head; curr != NULL; curr = curr->next)
	{
		if( curr->data <= High && curr->data >= Low)
			grade_count++;
	}

	return grade_count;


}

ListType List::ListSum()
{

	for (Node * curr = head; curr != NULL; curr = curr->next)
		sum += curr->data;

	return sum;
}

List::~List()
{

   
}



Is This A Good Question/Topic? 0
  • +

Replies To: Writing a Destructor that deletes every node in the list

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Writing a Destructor that deletes every node in the list

Posted 22 February 2009 - 12:57 PM

have it remove every entry as long as the list isnt empty, use a while loop
Was This Post Helpful? 0
  • +
  • -

#3 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Writing a Destructor that deletes every node in the list

Posted 22 February 2009 - 01:03 PM

i did something like this for the destructor and i ended up having an exception error

List::~List()
{		
		
	for (Node * curr = head; curr != NULL; curr = curr->next)
		delete curr;
}


Was This Post Helpful? 0
  • +
  • -

#4 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Writing a Destructor that deletes every node in the list

Posted 22 February 2009 - 02:17 PM

Here is an example of destructor I used before:

Class::~Class()
{
		node* clear = head;
	node* temp = NULL;

	while(clear)
	{
		temp = clear->next;
		delete clear;
		clear = temp;
	}

		delete temp;
}


Was This Post Helpful? 0
  • +
  • -

#5 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Writing a Destructor that deletes every node in the list

Posted 22 February 2009 - 02:27 PM

the problem with the way you did it is that if you delete curr, then curr->next no longer exists...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1