In my program I have to put a destructor in the class which will delete all the links when a linklist object is distroyed. I have to delete each link as it goes, displaying a message that the element was destoryed.....this is the code for the class....
Anyhelp on where this destructor goes would be great.Code://////////////////////////////////////////////////////////////// struct link //one element of list { int data; //data item link* next; //Pointer to next link }; //////////////////////////////////////////////////////////////// class linklist { private: link* first; //pointer to first link public: linklist() : first(NULL) { } void additem(int d); void display() const; }; //-------------------------------------------------------------- void linklist::additem(int d) { link* newlink = new link; //make a new link newlink->data = d; //give it data newlink->next = first; //it points to next link first = newlink; //now first points to this } //--------------------------------------------------------------- void linklist::display() const { link* current = first; //set ptr to first link while(current != NULL) //quit on last link { cout<<current->data <<endl; //print data current = current->next; //move to next link } } ////////////////////////////////////////////////////////////////
Cheers