Topic#9 - CS162 Topic#9 Lecture Dynamic Data Structures Deallocating all nodes in a LLL Inserting nodes into sorted order Programming Assignment

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS162 - Topic #9 Lecture: Dynamic Data Structures Deallocating all nodes in a LLL Inserting nodes into sorted order Programming Assignment Questions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS162 - Deallocating all The purpose of the destructor is to perform any operations necessary to clean up memory and resources used for an object’s whose lifetime is over this means that when a linked list is managed by the class that the destructor should deallocate all nodes in the linear linked list delete head won’t do it!!!
Background image of page 2
CS162 - Deallocating all So, what is wrong with the following: list::~list() { while (head) { delete head; head = head->next; } We want head to be NULL at the end, so that is not one of the problems We are accessing memory that has been deallocated. Poor programming!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS162 - Deallocating all The answer requires the use of a temporary pointer, to save the pointer to the next node to be deleted prior to deleting the current node: list::~list() { node * current; while (head) { current = head->next; delete [] head->data->title; delete head->data; delete head; head = current; }
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 16

Topic#9 - CS162 Topic#9 Lecture Dynamic Data Structures Deallocating all nodes in a LLL Inserting nodes into sorted order Programming Assignment

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online