如何从链表中删除节点? [英] How do I delete a node from a linked list?

查看:75
本文介绍了如何从链表中删除节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,在链接列表上执行多项操作,例如追加节点,显示链接列表,删除给定节点等。



I除了删除一个节点之外还有其他所有功能。

你能帮我解决删除节点功能吗?

这是我的删除节点功能:



 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;
}





我的节点和类定义是:



 / * 
*节点声明
* /
struct node
{
double info;
struct node * next;
} *开始;

/ *
*类声明
* /
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;
}
};





我尝试了什么:



上面给出的代码包括节点和类定义以及

删除节点函数。

解决方案

< blockquote>如果你想删除一个节点的值,你必须先找到它(例如使用你的 findNode()函数,但这应该返回一些东西)。



然后你必须调整前一个节点的下一个指针(设置为将被删除的节点的下一个)。这需要通过从开始迭代到 next 成员指向要删除的节点来查找前一个节点:



未经测试的示例:

 void single_llist :: deleteNode(double value)
{
struct node * s = findNode(value);
if(s)
{
//查找上一个节点
struct node * prev = start;
while(prev&& prev-> next!= s)
prev = prev-> next;
if(prev)
{
prev-> next = s-> next;
free(s);
cout<<Element Deleted<< endl;
}
}
}


拿一张纸并模拟你的链表。

将一个模式添加到列表A将为您提供列表B.从列表B中删除相同的节点将带您回到列表A.

-----

当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


非常感谢你的建议。

我让findNode返回true或者取决于节点是否存在。





void single_llist :: deleteNode(double value)

{

struct node * s;

int flag;

flag = findNode(value);

if(flag)

{

//查找上一个节点

struct node * prev = start;

while(prev&& prev-> next!= s)

prev = prev-> next;

if(prev)

{

prev-> next = s-> next;

free(s);

cout<<Element已删除<<< endl;

}

}

}



我做了以上更改。

但是当我调用这个函数时,节点会这样做不被删除。

我错过了什么吗?


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.

解决方案

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;
        }
    }
}


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.


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?


这篇关于如何从链表中删除节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆