destructor with a link list

This is a discussion on destructor with a link list within the C++ Programming forums, part of the General Programming Boards category; In the program given, I have to insert a destructor to the class that will delete all the links when ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    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 wrong..it only deletes one....what do I put in to delete all????
    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;
       }
    Thanks for any help...
    cheers

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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, 03:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 09: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, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21