链接列表删除节点 [英] Linked List Delete Node

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

问题描述

因此,我正在尝试实现从链接列表中删除节点的功能.

So I'm trying to implement a function that deletes nodes from a linked list.

这是我的主音:

int main(void)
{
    NODE* first = generateNodes(5);
    NODE* jank = getNode(first, 2);
    deleteNode(first,2);
    printf("Length of Node List: %d\n",getNodeListLength(first));
    printf("First Node: %d\n",first -> pos); 
    printf("Jank Node: %d\n",jank -> pos); 
    return 0;
}

这是我的输出:

Length of Node List: 2
First Node: 0
Jank Node: 2

输出应该是(因为在main()中我去掉了jank,并将链表的大小减一):

The output should be (because in main() I removed jank, and decreased the size of the linked list by one):

Length of Node List: 4
First Node: 0
Jank Node: NULL

这是我的整个源代码:

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

/* NODE STRUCTURE */

typedef struct node{

    char* thing;

    int pos; /* Index of node */
    struct node* next; /* Pointer to next node */

} NODE;

/* Generates a single node */ 
NODE* generateNode();

/* Generates linked nodes and returns the first node */
NODE* generateNodes(int num);

/* Gets a node at a certain index */
NODE* getNode(NODE* start, int index);

/* Returns the length of a list of nodes */
size_t getNodeListLength(NODE* start);

/* Removes a node at a certain index */
NODE* deleteNode(NODE* start, int index);

int main(void)
{
    NODE* first = generateNodes(5);
    NODE* jank = getNode(first, 2);
    deleteNode(first,2);
    printf("Length of Node List: %d\n",getNodeListLength(first));
    printf("First Node: %d\n",first -> pos); 
    printf("Other Node: %d\n",jank -> pos); 
    return 0;
}

NODE* generateNode()
{
    return (NODE*) malloc(sizeof(NODE));
}

NODE* generateNodes(int num)
{
    NODE* one = generateNode();
    NODE* cpy = one;

    int i;

    for(i = 0; i < num - 1; i++)
    {

        NODE* next = generateNode();
        cpy -> next = next;
        cpy -> pos = i;
        cpy = next;

    }

    cpy -> pos = i;
    cpy -> next = NULL;

    return one;
}

NODE* getNode(NODE* start, int index)
{
    int i;
    for(i = 0; i < index; i++)
    {
        start = start -> next;
    }
    return start;
}

size_t getNodeListLength(NODE* start)
{
    size_t i;
    while(start -> next != NULL)
    {
        start = start -> next;
        i++;
    }
    return i - 1;
}

NODE* deleteNode(NODE* start, int index)
{
    if(index > 0)
    {
        NODE* f = getNode(start,index - 1);
        NODE* l = getNode(start,index + 1);
        NODE* d = getNode(start,index);
        f -> next = l;
        free(d);
        return start;
    }
    if(index == 0)
    {
        NODE* up = start -> next;
        free(start);
        return up;
    }
    if(index + 1 == getNodeListLength(start))
    {
        NODE* r = getNode(start,index);
        NODE* c = getNode(start,index - 1);
        c -> next = NULL;
        free(r);
        return start;
    }
}

我哪里出错了?

推荐答案

我注意到您在 getNodeListLength 中的 size_t i 未初始化为任何值-这可能是源意外大小报告.

I notice your size_t i in getNodeListLength is not initialized to any value - this could be the source of the unexpected size reporting.

此外,您从列表中删除了 jank ,但是您的 jank 指针仍指向该节点(即使它已经 free 'd)-这意味着使用 jank 之后访问不再属于您的内存!

Also, you remove jank from the list, but your jank pointer is still pointing to the node (even though it has been free'd) - this means using jank after that is accessing memory that is no longer yours!

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

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