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 8849679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:42:51+00:00 2026-06-14T12:42:51+00:00

I am working on a Linked List class. Whenever I compile, I get no

  • 0

I am working on a Linked List class. Whenever I compile, I get no errors or warnings. However, the executable stops working (using Windows Dev C++). I have deduced that the problem is my destructor.

My understanding is the destructor gets called with the delete keyword for a dynamically created object, or whenever the object goes out of scope for an object on the stack.

I think my problem occurs when my stack object (Node* result) calls the destructor, but not really sure.

Here’s my header file:

    #ifndef Node_H
    #define Node_H

    class Node{
          int data;
          Node* next;

    public:
           Node(int data);       
           ~Node();
           void insert(int d);
           Node* remove(int d);
           void removeDups(void);
           Node* add(Node* a, Node* b);
           void printOut(void);
    };

    #endif

Revelant parts of .cpp file:

    #include <iostream>
    #include "Node.h"

    using namespace std;

    Node::Node(int d){
          data = d;
          next = NULL;
          }

    Node::~Node(){
           Node* n = this;
           Node* delptr;

           while(n->next != NULL){
                         delptr = n;
                         n = n->next;
                         delete delptr;
                         }
           delete n;            

        }

    void Node::insert(int d){

          Node* n = this;
          Node* current = new Node(d);

          while(n->next != NULL){
                  n = n->next;                   
                  }

          n->next = current;
          }

Main:

    int main (void){
        int i = 0;

        Node* root = new Node(111);
        Node* result;

        root->printOut();

        for (i = 0; i < 11; i++){
            root->insert(i);
            } 

        root->printOut();

        delete root; 
        getchar();
        return 0;   
    }

Also, I am running Dev C++ on a USB. My hope with that was to prevent memory leaks on my OS. Is that correct?

Thanks

— Tring Vu

  • 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-06-14T12:42:53+00:00Added an answer on June 14, 2026 at 12:42 pm

    The key idea when writing resource management is to keep concepts and responsibilities as simple as possible.

    What you have is a root node responsible for deleting all its children. You would very much prefer a structure that is responsible for the delete, than the individual node.

    The common thing to do is to create a LinkedList type/class along with a Node class. This will simplify the responsibilities. The LinkedList type will own the Nodes, and as an owner be responsible for the deleting in its destructor.

    inserts and remove / removeDups would then go into the LinkedList class. The node can help with functions like

    insert(Node* afterThisNode){  
        this->next = afterThisNode->next;
        afterThisNode->next= this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The addToHead function of the linked list class is not working properly. The function
This is homework I'm working on implementing a linked list class for my C++
I am working on a Polynomial class which uses the STL linked list. One
Ive been working on a linked list implementation. Previously i had it working for
I am currently working on overloading the == operator for my linked list. I
I'm working on a program using doubly linked lists. I have already gotten the
For a project I'm working on, I'm implementing a linked-list data-structure, which is based
For a project I'm working on, I'm implementing a linked-list data-structure, which is based
I'm attempting to create a linked list of length n using Python. I have
I have a simple doubly linked list example that I'm working on, but for

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.