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

  • Home
  • SEARCH
  • 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 6389025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:21:10+00:00 2026-05-25T03:21:10+00:00

Suppose, I have a singly linked list and its basic building block is, struct

  • 0

Suppose, I have a singly linked list and its basic building block is,

struct Node {
  Data d;
  Node *pNext;
  // methods
  ~Node();
};

The head of the linked list is stored as,

Node *m_Head; // member of some class

When, I am done with the list, I will clean it by deleting each node as,

void Erase()
{
  Node *pIter, *pTemp = m_Head;
  while((pIter = pTemp) != 0)
  {
    pTemp = pIter->pNext;
    delete pIter;
    pIter = pTemp;
  }
}

I thought, if I can simplify this. So I came up with an idea where I can clean this whole linked list with just a single instruction !

delete m_Head;

and destructor will look like:

Node::~Node() { delete this->pNext; }

Here my concern is, will it cause recursion (implicitly due to delete) ? If yes, then it’s definitely a concern for bigger linked lists. Will compiler be able to help in any way for optimizing that ?

[Note: Not using any library facility like std::list or other.]

  • 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-25T03:21:11+00:00Added an answer on May 25, 2026 at 3:21 am

    Of course: Only do this for learning purposes or when you are sure that your own List is really better for your use case


    It depends. Possibly your compiler will detect a tail recursion and emit code that is conceptually equivalent to using a loop.

    If not, then yes, it will recurse. Usually, some thousands of recursions should be possible on commodity boxes, if stack pressure is small (like in your case). However, there is no guarantee, and indeed, for really large lists, this can be a problem.

    Also, I think that recursion is indeed not entirely appropriate for the concept of sibling nodes. A node hierarchy, like with quadtrees, cries for recursion, but I have a not so good time thinking in recursion (which forms a call hierarchy) when the list-concept is about sibling-nodes.

    You may also consider the manual loop as a easy-to-achieve optimization over the recursion that will make your code more robust in a guaranteed way.

    Btw, you could also should rip out the deletion of nodes into a holder class:

    class List {
    public:
        ~List() {
            for-each-node
                delete-node
        }
    
    private:
        class Node {
            Node *node_;
            ...
        };
        ...
    };
    

    This is basically how the standard library’s list is usually implemented. It makes the whole implementation easier to achieve and conceptually more correct (Nodes don’t own their sibling logically)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a basic, singly-linked list class in C++. I did it
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have a simple to-do list application. The application contains two models: lists
Suppose I have 1 million data in my database. what will be the most
I have an application that is saving clob data into database. Suppose when I
Quick question... Suppose I have a C# struct that contains a single reference member.
Suppose there are two singly linked lists both of which intersect at some point
Suppose I have two methods, Foo and Bar , that do roughly the same
Suppose I have an existing assembly, and some of the classes have overloaded methods,
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX

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.