Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 579589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:22:57+00:00 2026-05-13T14:22:57+00:00

My C++ is a little rusty but I have made a program that reverses

  • 0

My C++ is a little rusty but I have made a program that reverses a linked list and now I am trying to write the proper destructors for it but I don’t know exactly what to destroy. Here are my class definitions:

class LinkedList
{
    private:ListElement *start;
    public:LinkedList();
    public:void AddElement(int val);
    public:void PrintList();
    public:void InvertList();
};

class ListElement
{
    public:int value;
    public:ListElement * link;
    public:ListElement(int val);
    public:ListElement();
};


class Stack
{

private:ListElement ** stack;
private:int index;
public:Stack(int size);
public:void push(ListElement * le);
public:ListElement * pop();

};

The stack is for when I reverse the list.
Anyway …
How would I go about writing the destructors for these?
I was thinking:

For the ListElement make the value 0 and the link 0(NULL).

For the LinkedList go through the elements and call up the ListElementDestructor for all of them.

I am not very sure about this because as I understand the destructor automatically calls the destructors of the member objects so would only writing an empty destructor for LinkedList suffice in this case? I don’t know … that is why I am asking

For the stack I don’t know … the pointers are already 0(NULL) after the list is inversed because they are all poped.

I am a bit confused.
Can anyone help?
Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:22:57+00:00Added an answer on May 13, 2026 at 2:22 pm

    For the ListElement make the value 0 and the link 0(NULL).

    You don’t need to reset any of the values in the destructor, as values won’t exist after the destructor executed.

    The main thing you need to be sure of is that all elements allocated on the heap (i.e Using new) are deleted using delete (or delete [] in the case of arrays).

    For the LinkedList go through the elements and call up the ListElementDestructor for all of them.

    For a object allocated on the stack it is automatically called when the object goes out of scope.
    For a dynamically allocated object (i.e created using new) the destructor is called when
    it is deleted using delete. In other words, you don’t need to call any destructors as they are called automatically if you clean up your objects correctly.

    Given (I assume) that you are allocating new ListElements on the heap in the LinkedList class, you should ensure that in the LinkedList destructor, every ListElement is deleted in the destructor by walking down the list and calling delete on every ListElement (after you have retrieved the address of the next list element from it of course). Something like this:

    ListElement* current = list.start;    
    while( current ){
        ListElement* next = current->next;
        delete current;
        current = next;
    }
    

    There is nothing in the ListElement class that needs cleaning up, as although it has a pointer to the next element the deletion should probably be handled in the LinkedList class, which means it doesn’t need a destructor.
    It isn’t required that you write a destructor for every class, as the compiler will automatically generate an empty one for you.

    As I said in the comments, you shouldn’t be using a stack for reversing a linked list. You should just be swapping the direction of the pointers.
    An quick example of roughly the sort of thing you would want.

    ListElement* previous = 0;
    ListElement* current = list.start;
    
    while( current->next ){
        //copy the address of current item on the list
        ListElement* next = current->next;
    
        //point the current list item to the previous list item
        current->next = previous;
    
        //set the current list item to the next list item
        current = next;
    
        //and the previous list item to the current one
        previous = current;
    }
    //set the start of the list to what was the end
    list.start = current;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My php is a little rusty but this is boggling my mind right now.
Do you agree that the designers of Java class java.io.IOException should have made it
Little bit confused... I am trying to track mailto links being clicked, but constantly
I have some very rusty C++ and lots of C experience, but have done
I have a simple little dialog that lets user setup a time block. The
Quick little question... I know that sometimes in other languages libraries have part of
My little site should be pooling list of items from a table using the
We are developing a little application that given a directory with PDF files creates
Background: I have a little video playing app with a UI inspired by the
Little help here please . I am trying to create this lisp macro which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.