首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何删除节点

如何删除节点
EN

Stack Overflow用户
提问于 2015-11-15 20:19:26
回答 3查看 256关注 0票数 0

我正在尝试从列表中删除节点。我必须使用我用来从列表中删除第一个节点的函数,然后必须添加其他节点(在我想要删除的节点之前)。函数删除列表开头的节点并添加到列表的开头。

我试过这段代码:

代码语言:javascript
运行
复制
List* Delete_theNode(int the_node){

    List *temp;

    if(head->next == NULL)
    {
        printf("empty list..");
    }

    else
    { 
        for(temp = head; temp!= NULL; temp = temp-> next)
        {
            Delete_node();
        }
        if(temp->number == the_node)
        {
            Delete_Node();
        }

        else
        {
            printf("\n%d there is no such a node\n", the_node);
        }   

    }
}   
EN

回答 3

Stack Overflow用户

发布于 2015-11-15 20:32:09

你的代码中有很多bug!

这种结构更像是一个链表,而不是一个堆栈

您需要知道前一个节点,以便删除该节点并将列表再次链接到下一个节点。

此外,在删除节点之前,您可能需要释放已删除节点上的内存。

理想情况下:

代码语言:javascript
运行
复制
Stack* Delete_theNode(int the_node) {
    //check if it is on the head
    if (the_node==head->number) {
        Stack * temp = head;
        head = head->next;
        free(temp);
        return;

     }

    Stack* cur = head->next;
    Stack* prev = head;
    //while cur is not NULL and prev is not NULL, this is also legit  
    while (!cur && !prev) { 
        if (the_node == cur->number) {
            Stack *tmp = cur;//the deleted node
            prev->next = cur->next;
            free(tmp);
            return;
        }
        prev = cur;
        cur = cur->next;
     }
 }
票数 1
EN

Stack Overflow用户

发布于 2015-11-15 22:18:02

使用递归重新添加已删除的节点。我试图修复,但没有编译,所以可能有一些语法错误。请验证

代码语言:javascript
运行
复制
Stack* Delete_theNode(int the_node){
    if(head == NULL)
    {
        printf("empty stack..");
    }

    else
    { 

        if((head != NULL) && (head->number != the_node))
        { 
            Stack *deletedNode = Delete_Node();//Delete top node & reset the head
            Delete_theNode(the_node); //Recursive Call
            Add_toStack(deletedNode);  //Add node and reset the head
        }

        else if(head->number == the_node)
        {
            Delete_Node();
        }

        else
        {
            printf("\n%d there is no such a node\n", the_node);
        }   

    }
} 
票数 0
EN

Stack Overflow用户

发布于 2017-04-24 01:57:57

我使用struct做到了这一点。希望能对你有所帮助。

代码语言:javascript
运行
复制
#include<iostream>

using namespace std;

struct node
{
    int number;
    node *next;
};

node a, b, c, d, e, *head, *current, *old, myNewNode;//global
bool flag, token;//global
int deletionInt, searchInt, newInt;//global


 void Deletion ()
    {
        cout << "Enter the integer to be deleted: " ;

    cin >> deletionInt;
old = head;

current = head;

while(1)
{
    if(head==NULL)
    {
        cout << "Your list is empty, nothing to delete here!" << endl;

        break;
    }

    if(deletionInt==current->number)
    {
        if(current == head)
        {
            head= current->next;

            current = head;
        }
        else
        {
            old->next= current->next;

            current = old;
        }
        break;
    }
    if(current->next==NULL) break;

    else
    {
        old = current;

        current = current->next;
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33719586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档