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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:13:10+00:00 2026-05-23T22:13:10+00:00

I wrote a recursive doubly linked C++ functions to delete all the nodes in

  • 0

I wrote a recursive doubly linked C++ functions to delete all the nodes in a doubly linked list having a certain value. Is it possible to optimize this? Thank you.

struct marynode {
    int value;
    marynode* next;
    marynode* prev;
};

void DoubleLinkedListDeleteMultiple(marynode*& llist, int value){
    marynode* tmp = llist;

    if (llist == NULL)
        return;

    if (llist->value == value){
            DoubleLinkedListDeleteMultiple(llist->next, value); 
            if (llist->next == NULL){
                marynode* prev = llist->prev;
                delete llist;
                llist = NULL;
                llist = prev;
                if (llist)
                    llist->next = 0;
            }   
            else if (llist->prev == NULL){
                marynode* next = llist->next;
                delete llist;
                llist = NULL;
                llist = next;
                if (llist)
                    llist->prev = 0;
            }   
            else{
                marynode *x = llist;
                marynode  *mynext = x->next;
                while (x){
                    if (x->value == value){
                        marynode* clara = x->next;
                        marynode* zprev = x->prev;
                        delete x;
                        x = NULL;
                        x = clara;
                        llist = clara;
                        if (llist)
                            llist->prev = zprev;
                    }
                    else {
                        x = mynext;
                    }   
                    if (mynext){
                        mynext = mynext->next;
                    }
                }

            }
    }
    else{
            DoubleLinkedListDeleteMultiple(llist->next, value); 
    }
}
  • 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-23T22:13:11+00:00Added an answer on May 23, 2026 at 10:13 pm

    Is something wrong with std::list?

    Also, structs + global functions? You probably want to use object oriented constructs like member methods instead.

    To optimize this particular code a little, I’d change your recursive call to a while loop.

    while(llist != NULL) {
         if(llist->value == value) {
              if(llist->prev) {
                  llist->prev->next = llist->next;
              }
              if(llist->next) {
                  llist->next->prev = llist->prev;
              }
              node* next = llist->next;
              delete llist;
              llist = next;
         } else {
             llist = llist->next;
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this snippet of code and I assume len is tail-recursive, but a
I wrote a recursive function, which returns an array with the paths to all
I wanted to have a tail-recursive version of List.map , so I wrote my
This is a recursive function I wrote to determine whether or not a given
I was once asked of this in an interview: How to write a recursive
For all you compiler gurus, I wanna write a recursive descent parser and I
I have a mathematical formula to apply: I wrote a non recursive code (sadly
I wrote this simple Prolog program. man(socrates). mortal(X) :- man(X). immortal(X) :- immortal(X). I
Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem
I designed a recursive algorithm and wrote it down in Python. When I measure

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.