In the program given, I have to insert a destructor to the class that will delete all the links when a linklist object is destroyed. I also need a message to display in the destructor that the element is destroyed... this is the code I have...I know the destructor is wrong..it only deletes one....what do I put in to delete all????
Thanks for any help...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; ~linklist() //destructor... { cout<<"Deleting first."; delete[] first; // delete ???not sure what } }; //-------------------------------------------------------------- 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 } } //////////////////////////////////////////////////////////////// int main() { linklist li; //make linked list li.additem(25); //add four items to list li.additem(36); li.additem(49); li.additem(64); li.display(); //display entire list getch(); return 0; }
cheers