某些 C 函数的垃圾值

Garbage value for certain C functions

我正在学习 C,我正在尝试制作一个同时包含多个链表操作的程序。他们没有给我错误,但是对于在末尾删除一个节点并在某个位置插入的函数,我在输出的开头得到了一个垃圾值。

仅实现插入功能的临时程序:

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

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

void inp(struct node **start, int ele)                       
{
    struct node *NEW = (struct node*)malloc(sizeof(struct node));  
    NEW->data = ele;                                               

    struct node *ptr = *start;                                      
    while(ptr != NULL)
    {
        if(ptr->next == NULL)                                      
        {
            ptr->next = NEW;                                        
            NEW->next = NULL;                                       
            break;                                                
        }
        ptr = ptr->next;

    }
}

void insert_pos(struct node **start, int ele, int pos)
{
    struct node *ptr = *start;                                      
    for(int i = 1; i < pos; i++)
    {
        ptr = ptr->next;                                                                                                  
    }

    struct node *NEW = (struct node*)malloc(sizeof(struct node));
    NEW->data = ele;
    NEW->next = ptr->next;
    ptr->next = NEW;
}

void display(struct node* start)
{
    struct node* ptr = start;
    while(ptr != NULL)
    {
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }
}

int main()
{
    struct node *a;
    int m;
    for(int i = 0; i < 10; i++)
    {
        scanf("%d", &m);
        inp(&a, m);
    }

    insert_pos(&a, 594, 3);
    printf("\n\n");
    display(a);

}

最后要删除的临时程序:

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

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

void inp(struct node **start, int ele)                       
{
    struct node *NEW = (struct node*)malloc(sizeof(struct node));  
    NEW->data = ele;                                               

    struct node *ptr = *start;                                      
    while(ptr != NULL)
    {
        if(ptr->next == NULL)                                      
        {
            ptr->next = NEW;                                        
            NEW->next = NULL;                                       
            break;                                                
        }
        ptr = ptr->next;

    }
}

void del_end(struct node **start)                                   
{
    struct node *ptr = *start;
    while(ptr != NULL)
    {
        if(ptr->next->next == NULL)
        {
            ptr->next = NULL;
            free(ptr->next->next);
            break;
        }
        ptr = ptr->next;
    }
}

void display(struct node* start)
{
    struct node* ptr = start;
    while(ptr != NULL)
    {
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }
}

int main()
{
    struct node *a;
    int m;
    for(int i = 0; i < 10; i++)
    {
        scanf("%d", &m);
        inp(&a, m);
    }

    del_end(&a);
    printf("\n\n");
    display(a);

}

知道如何解决吗?

你可以在这里看到:

输入问题:

struct node *a;

将垃圾值放入您不处理的 a 中,并像节点一样使用它, 添加 =NULL 来解决。

另外,你的从末尾删除的函数有如下错误:

struct node *ptr = *start;
    while(ptr != NULL)
    {
        if(ptr->next->next == NULL)
        {
            ptr->nexStack Overflow for Teams – Collaborate and share knowledge with a private group.t = NULL;
            free(ptr->next->next);
            break;
        }
        ptr = ptr->next;
    }

您正在更改 ptr->next 而不是访问 ptr->next->next...