Click here to Skip to main content
15,614,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to code a program that does several operations on a linked list like append a node, display linked list, delete a given node etc.

I have got all functions except delete a node to work.
Can you please help me fix the delete node function?
Here's my delete node function:

void single_llist::deleteNode(double value)
{
    new (struct node);
    struct node *s;
    
    s = start;
    s->info=s->next->info;
        free(s);
        cout<<"Element Deleted"<<endl;
}


My Node and class definitions are:

/*
 * Node Declaration
 */
struct node
{
    double info;
    struct node *next;
}*start;

/*
 * Class Declaration
 */
class single_llist
{
public:
    node* create_node(double);
    void appendNode(double);
    void deleteNode(double);
    void findNode(double);
    int countList();
    void displayList();
    single_llist()
    {
        start = NULL;
    }
};


What I have tried:

Code given above including the Node and class definition and the
delete node function.
Posted
Updated 6-Mar-17 9:53am

If you want to delete a node by it's value you have to locate it first (e.g. using your findNode() function but that should return something).

Then you have to adjust the next pointer of the previous node (set to next of the node that will be deleted). This requires finding the previous node by iterating from start until the next member is pointing to the node to be deleted:

Untested example:
void single_llist::deleteNode(double value)
{
    struct node *s = findNode(value);
    if (s)
    {
        // Find previous node
        struct node *prev = start;
        while (prev && prev->next != s)
            prev = prev->next;
        if (prev)
        {
            prev->next = s->next;    
            free(s);
            cout<<"Element Deleted"<<endl;
        }
    }
}
 
Share this answer
 
Take a sheet of paper and simulate your linked list.
adding a mode to list A will give you list B. deleting the same node from list B will bring you back to list A.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Thanks a lot for the suggestion.
I made the findNode return true or false depending on if the node is present or not.


void single_llist::deleteNode(double value)
{
struct node *s;
int flag;
flag=findNode(value);
if (flag)
{
// Find previous node
struct node *prev = start;
while (prev && prev->next != s)
prev = prev->next;
if (prev)
{
prev->next = s->next;
free(s);
cout<<"Element Deleted"<<endl;
}
}
}

I made the above changes.
But when I call this function, the node doesn't get deleted.
Am I missing anything else?
 
Share this answer
 
Comments
Patrice T 6-Mar-17 14:46pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
I tried my xcode debugger to find the cause of this issue.

(lldb) print prev->next
(node *) $0 = 0x0000000100500220
(lldb) print s->next
error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory
(lldb) print s
(node *) $2 = 0x0000000000001efc
(lldb) print s->next
error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory
 
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