这个链表删除尾节点函数有什么问题吗?

发布于 2024-09-27 09:31:13 字数 2378 浏览 1 评论 0原文

我编写了这个函数来删除单链表的最后一个节点。

问题是,它能够删除除第一个/起始节点之外的所有节点。

这段代码片段缺少什么?

请回答我的具体问题。

    #include <stdio.h>
#include <stdlib.h>

struct Node
{
    char CharContent;
    struct Node * NextNodePointer;
};
typedef struct Node Node;

#pragma region Prototypes
Node * CreateNewNode(char ch);
Node * AddNode(Node * start, Node * newNode);
void DeleteTailNode(Node * start);
void PrintAllNodes(const Node * start);
#pragma endregion Comments

main()
{
    Node * start = NULL;
    Node * newNode = NULL;

    start = AddNode(start, CreateNewNode('A'));
    start = AddNode(start, CreateNewNode('B'));
    start = AddNode(start, CreateNewNode('C'));
    start = AddNode(start, CreateNewNode('D'));
    PrintAllNodes(start);

    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);

    getch();
}

#pragma region Node * CreateNewNode(char ch)
Node * CreateNewNode(char ch)
{
    struct Node * newNode = (struct Node *) malloc(sizeof(struct Node *));

    newNode->CharContent = ch;
    newNode->NextNodePointer = NULL;

    return newNode;
}
#pragma endregion Comment

#pragma region UnifiedAddNode()
Node * AddNode(Node * start, Node * newNode)
{
    Node * copyOfStart = start;

    if(start == NULL)
    {
        return newNode;
    }
    else
    {
        while(copyOfStart->NextNodePointer != NULL)
        {
            copyOfStart = copyOfStart->NextNodePointer;
        }

        copyOfStart->NextNodePointer = newNode;

        return start;
    }
}
#pragma endregion Comment


void DeleteTailNode(Node * start)
{
    Node * prev = NULL;
    Node * current = start;

    while(current->NextNodePointer != NULL)
    {
        prev = current;
        current = current->NextNodePointer;
    }

    free (current);

    if (prev != NULL)
    {
        prev->NextNodePointer = NULL;
    }
}


#pragma region PrintAllNodes()
void PrintAllNodes(const Node * start)
{
    struct Node * tempRoot = start;

    while(tempRoot != NULL)
    {
        printf("%c, ", tempRoot->CharContent);

        tempRoot = tempRoot->NextNodePointer;
    }

    printf("\n");
}
#pragma endregion Comment

I have written this function to delete the last node of a singly linked list.

The problem is, it is able to delete all the nodes except the first/starting node.

What is missing from this code snippet?

Please answer my particular problem.

    #include <stdio.h>
#include <stdlib.h>

struct Node
{
    char CharContent;
    struct Node * NextNodePointer;
};
typedef struct Node Node;

#pragma region Prototypes
Node * CreateNewNode(char ch);
Node * AddNode(Node * start, Node * newNode);
void DeleteTailNode(Node * start);
void PrintAllNodes(const Node * start);
#pragma endregion Comments

main()
{
    Node * start = NULL;
    Node * newNode = NULL;

    start = AddNode(start, CreateNewNode('A'));
    start = AddNode(start, CreateNewNode('B'));
    start = AddNode(start, CreateNewNode('C'));
    start = AddNode(start, CreateNewNode('D'));
    PrintAllNodes(start);

    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);
    DeleteTailNode(start);
    PrintAllNodes(start);

    getch();
}

#pragma region Node * CreateNewNode(char ch)
Node * CreateNewNode(char ch)
{
    struct Node * newNode = (struct Node *) malloc(sizeof(struct Node *));

    newNode->CharContent = ch;
    newNode->NextNodePointer = NULL;

    return newNode;
}
#pragma endregion Comment

#pragma region UnifiedAddNode()
Node * AddNode(Node * start, Node * newNode)
{
    Node * copyOfStart = start;

    if(start == NULL)
    {
        return newNode;
    }
    else
    {
        while(copyOfStart->NextNodePointer != NULL)
        {
            copyOfStart = copyOfStart->NextNodePointer;
        }

        copyOfStart->NextNodePointer = newNode;

        return start;
    }
}
#pragma endregion Comment


void DeleteTailNode(Node * start)
{
    Node * prev = NULL;
    Node * current = start;

    while(current->NextNodePointer != NULL)
    {
        prev = current;
        current = current->NextNodePointer;
    }

    free (current);

    if (prev != NULL)
    {
        prev->NextNodePointer = NULL;
    }
}


#pragma region PrintAllNodes()
void PrintAllNodes(const Node * start)
{
    struct Node * tempRoot = start;

    while(tempRoot != NULL)
    {
        printf("%c, ", tempRoot->CharContent);

        tempRoot = tempRoot->NextNodePointer;
    }

    printf("\n");
}
#pragma endregion Comment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-10-04 09:31:14

您没有检测到 start 为 NULL 的情况,即列表为空。

您不想在将下一个节点设置为 NULL 之前释放它吗?

如果起始节点是最后一个节点,当列表遍历完成时,prev 将为 NULL,但是当发生这种情况时,您将删除 (NULL) start->NextNodePointer,当你想删除启动本身。

尝试:

void DeleteTailNode(Node *& start)
{
    Node * prev = NULL;
    Node * current = start;

    if (start == NULL)
        return;

    while(current->NextNodePointer != NULL)
    {
        prev = current;
        current = current->NextNodePointer;
    }

    if(current != NULL)
        free(current);

    if(current == start)
        start = NULL;

    if(prev != NULL)
        prev->NextNodePointer = NULL;
}

You aren't detecting the case where start is NULL, i.e. the list is empty.

Don't you want to free the next node before setting it to NULL?

If the start node is the last node prev will be NULL when the list traversal completes, but when that happens you are deleting the (NULL) start->NextNodePointer, when you want to delete start itself.

Try:

void DeleteTailNode(Node *& start)
{
    Node * prev = NULL;
    Node * current = start;

    if (start == NULL)
        return;

    while(current->NextNodePointer != NULL)
    {
        prev = current;
        current = current->NextNodePointer;
    }

    if(current != NULL)
        free(current);

    if(current == start)
        start = NULL;

    if(prev != NULL)
        prev->NextNodePointer = NULL;
}
温暖的光 2024-10-04 09:31:14

怎么知道删不删呢?正如我所看到的,这里没有任何内容被删除..这是100%确定的内存泄漏..你没有在DeleteTailNode中使用free..你只是使分配的内存不可访问..

编辑:调用循环后free( current )。并且不需要检查,如果 current 为 NULL,则删除 NULL 指针是安全的。

How do you understand if it's deleted or not ? As I see, nothing here is deleted.. it's a 100% sure memory leak.. you're not using free in DeleteTailNode .. you're just making the allocated memory unaccessible..

Edit: call free( current ) after the loop. And there's no need from the check, if current is NULL, it's safe to delete NULL pointer.

岁月苍老的讽刺 2024-10-04 09:31:13

CreateNewNode() 中将

struct Node * newNode = (struct Node *) malloc(sizeof(struct Node *));  
                                                                  ^
                                                                  |  

                                                                 Ouch!!

其更改为:struct Node * newNode = (struct Node *) malloc(sizeof(struct Node));

EDIT 2

测试运行此处

Inside CreateNewNode()

struct Node * newNode = (struct Node *) malloc(sizeof(struct Node *));  
                                                                  ^
                                                                  |  

                                                                 Ouch!!

Change it to : struct Node * newNode = (struct Node *) malloc(sizeof(struct Node));

EDIT 2

Test Run HERE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文