Thread: destructor with a link list

  1. #1
    Registered User
    Join Date
    Oct 2002

    destructor with a link list

    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 only deletes one....what do I put in to delete all????
    struct link                                             //one element of list
            int data;                                       //data item
            link* next;                                     //Pointer to next link
    class linklist
                    link* first;                            //pointer to first link
                    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.display();      //display entire list
       return 0;
    Thanks for any help...

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    two easy ways....

    One: use a pointer to a node and walk down the list deleting one at a time.

    Two: put a destructor in the node itself that will delete its "next" if it exists.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM