Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everybody,
My name is Samuel and I'm a C++ self-learner.
I'm making a simple project that deletes an element from a linked list through a recursive function, But it deletes every element before the selected element,
Here is the code
C++
#include <iostream>

using namespace std;

struct piece
{
    int num;
    piece* p_next;
};

piece* grow(piece* p_list,int num) \\ adds a new element with a specific number
{
    piece* new_num = new piece;
    new_num->num = num;
    new_num->p_next = p_list;
    p_list = new_num;
    return p_list;
}

piece* del(piece* p_list,int number) \\ deletes the element (number) from list
{
    if (p_list->num == number)
    {
        piece* p_delete = p_list;
        p_list = p_list->p_next;
        delete p_delete;
        return p_list;
    }
    else
    {
        return p_list->p_next = del(p_list->p_next,number);
    }
}

int main()
{
    piece* p_t1 = NULL;
    for (int i = 5; i >= 1; i--)            \\create the linked list p_t1
    {
        p_t1 = grow(p_t1,i);
    }
    piece* p_current = p_t1;                \\dipslay the linked list
    for (int i = 0; i < 7; i++)
    {
        if (p_current != NULL)
        {
            cout << p_current->num << endl;
            p_current = p_current->p_next;
        }
        else
        {
            cout << "NULL";
            break;
        }
    }
    int num = 0;
    cin >> num;
    cout << "\n\n";
    p_t1 = del(p_t1,num);
    p_current = p_t1;               \\ display the numbers of the list after 
    for (int i = 0; i < 5; i++)     \\ deleting an element  
    {
        if (p_current != NULL)
        {
            cout << p_current->num << endl;
            p_current = p_current->p_next;
        }
        else
        {
            break;
        }
    }
}

I don't know how could I solve it.
Could any one help me please ?
Thanks, Sam.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jun-15 17:09pm    
And what elements did you want to delete? :-)
—SA
Samuel Shokry 8-Jun-15 21:56pm    
An element that is entered by the user from the linked list
Sergey Alexandrovich Kryukov 8-Jun-15 22:09pm    
What do you mean by "entering the element"? It could easily be ambiguous.
—SA
Samuel Shokry 9-Jun-15 0:42am    
How to delete an element from the linked list chosen by the user. That's it ^_^
Mohibur Rashid 8-Jun-15 17:39pm    
Yes, your del function is in correct. If you debug you will get it quickly. Next thing is you are writing c++ but you are using global methods. In your main function you are using for loop. Try that with while loop.

About del function. If first element match then its delete that element and returning next. The same thing is happening when you are doing it for a value which is in the middle of the link. A->b->c->d, here if you delete c, b should connect with d. But your code will return d only

1 solution

There are many things that an experience programmer would do differently. But I understand that you are still learning. So let's first fix the bug in your program.

Take a look at your function del:
C++
piece* del(piece* p_list,int number) \\ deletes the element (number) from list
{
    if (p_list->num == number)
    {
        piece* p_delete = p_list;
        p_list = p_list->p_next;
        delete p_delete;
        return p_list;
    }
    else
    {
        return p_list->p_next = del(p_list->p_next,number); // here is the error
    }
}

Actually, this function should not return anything. But as you are passing the list pointer by value (first argument) you can't modify the list pointer that resides outside the function. Therefore, you returned the new list pointer and the caller's responsibility is to assign it to the outside list pointer.

As the function del should work recursively (no good idea!), you are deferring another responsibility to the caller, namely to fix the p_next pointer in the previous element (to which function del has no access). And that is the design flaw. The caller has no idea, whether he should simply fix the previous p_next or whether he has to fix the outside list pointer.

Here is the way to resolve that: Give del as first agrument a piece**, that is:
C++
void del (piece** pp_list, int number)


Now you can manipulate the outside list pointer from with del. And the interface of del is that after it returns, all is done. The caller has not to care about anything.

I leave it up to you to modify the code inside the function. The important part is to give the previous element's p_next pointer's address as first argument in the recursive call:
C++
else
{
   piece* p_elem = *pp_list;
   del (&p_elem->p_next, number);
}


After you fixed that, I would strongly recommend to do a non-recursive version,
which is not only easier to write, but will also be faster.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900