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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How do i write destructor for linked list c++?
Next
Answered
Kye Singh
  • 26
Kye Singh
Asked: May 18, 20222022-05-18T16:41:19+00:00 2022-05-18T16:41:19+00:00In: cpp

How do i write destructor for linked list c++?

  • 26

. Advertisement .

..3..

. Advertisement .

..4..

How to write destructor for linked list c++? Below is my code

LinkedList::~LinkedList()
  {
  ListNode *ptr;
 
  for (ptr = head; head; ptr = head)
  {
  head = head->next
  delete ptr;
  }
 }

Please give me a few practical suggestions.

linked list c++
  • 2 2 Answers
  • 53 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-11T00:25:14+00:00Added an answer on June 11, 2022 at 12:25 am

    Instead of attempting to carefully analyze whether that overcompilated for-loop is right, why not make it much simpler with an elegant while-loop?

    ListNode* current = head;
    while( current != 0 ) {
        ListNode* next = current->next;
        delete current;
        current = next;
    }
    head = 0;
    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Sacha Sanchez
    2022-05-25T20:33:02+00:00Added an answer on May 25, 2022 at 8:33 pm

    It can be run through a debugger, or through the wetware in your skull. Both will confirm that it works. Let’s take, for example, the following list.

    head(47) -> [47]single_node -> [NULL]end-of-list.

    This list can be used to generate statements.

    • ptr = head changes ptr to 47
    • head is non-zero so enter loop.
    • head = head->next sets head NULL.
    • delete ptr will remove single_node.
    • ptr = head sets ptr NULL
    • head is NULL(0), exit loop.

    You’ve now deleted the last entry from the list. head is now set as NULL. This is all there is to it.

    It’s possible to do the same thing with a longer or empty list. You’ll still be able to use it. There’s no difference between a one-element and fifty-element listing.

    A side note: I don’t like pointers being treated as booleans. I would rather have it written as follows:

    for (ptr = head; head != NULL; ptr = head)

    This makes code more readable and doesn’t sacrifice performance (unless you are a brain-dead programmer). It’s all a matter taste.

    Comment:

    The thing that concerns me is reaching the very last node. The condition “head;” should check that it is not null, but I’m not sure if it will work.

    It will work. It will work. A zero value will be considered false. You’ll see that head->next is never used when head is NULL. This is because you have already exited the loop body (or not entered the body, if it is empty).

    Any other pointer value you provide will be considered true, and you will enter the loop body or continue it.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.