c - 链接列表值指向仅在 C 中更改内部函数

标签 c function struct linked-list

我正在尝试用 C: 实现链表

struct Node{
    struct Node *next;
    void *data;
};

插入函数:

void insert(void *p2Node, void *data)
{
    struct Node *newNode;
    struct Node **p2p2Node= (struct Node **)p2Node;
    
    if (newNode = malloc(sizeof(struct Node))) /* if successfully allocated */
    {
        newNode->data = data;
        
        if ((*p2p2Node) != NULL) /* if the list is not empty */
        {
            newNode->next =  (*p2p2Node)->next;
            (*p2p2Node)->next = newNode;
        }
        else
            (*p2p2Node) = newNode;

        p2Node = p2p2Node;
    }
    printf("Inside the insert: %s\n", (*p2p2Node)->data);
}

我在 main() 中调用了插入:

int main()
{
    char *setA = "liquid ";
    char *setB = " lgd";
    char *setC = "sample";  
    struct Node *nList = malloc(sizeof(struct Node));

    insert(nList, setC);
    printf("2Get %s\n", nList->data);
    
    return 0;
}

没有报告错误或警告,但值仅在 insert 内更改。回到 main() 链表仍然是空的。

我不明白:main()中的nList是一个空指针。在 insert() 中,*p2Node 没有改变,我用 p2p2Node 改变了 p2Node 指向的值,为什么它不起作用?我是否不匹配指针?有没有一种方法可以在不修改 insert() 的参数的情况下使其工作?

谢谢。

最佳答案

使用此代码将值插入链表。

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

struct node *root = NULL;
int len;

int main()
{
    append();
    display();

    addatbegin();
    display();

    addatafter();
    display();
}

将值添加到列表的末尾。

void append(){
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    printf("Enter the data: ");
    scanf("%d", &temp->data);
    temp->link = NULL;
    if(root == NULL) //list is empty
    {
        root=temp;
    }else
    {
        struct node* p;
        p=root;
        while(p->link != NULL)
        {
            p = p->link;
        }
        p->link = temp;
    }
}

将值添加到列表的开头。

void addatbegin()
{
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    printf("Enter the data : ");
    scanf("%d", &temp->data);
    temp->link = NULL;
    if(root == NULL)
    {
        temp = root;
    }
    else
    {
        temp->link = root;
        root = temp;
    }
}

在节点后添加值

void addatafter()
{
    struct node* temp, *p;
    int loc, i=1;
    printf("Enter the location : ");
    scanf("%d", &loc);
    if(loc > len)
    {
        printf("Invalid input.");
    }
    else
    {
        p = root;
        while(i > loc)
        {
            p = p->link;
            i++;
        }
        temp = (struct node*)malloc(sizeof(struct node));
        printf("Enter the data : ");
        scanf("%d", &temp->data);
        temp->link = NULL;
        temp->link = p->link;
        p->link = temp;
    }   
}

显示链表

void display(){
    struct node* temp;
    temp = root;
    if(temp == NULL)
    {
        printf("List id empty.\n");
    }
    else
    {
        while (temp != NULL){
            printf("%d -> ", temp->data);
            temp = temp->link;
        }
        printf("\n\n");
    }
}

关于c - 链接列表值指向仅在 C 中更改内部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64455065/

相关文章:

c - Emscripten:我怎样才能编译一个带有像 immintrin.h 这样的内部头文件的 c 文件?

mysql - Symfony - 代币

c# - 如果数组用作 struct (C#) 中的元素,它存储在哪里?

c - 如何在结构体中搜索多个寄存器?

c - 在 C99 的结构中分配指向二维空数组的指针

c - libav - 对 'av_frame_alloc' 等的 undefined reference

c - 使用 LoadLibraryExA(...,...,LOAD_LIBRARY_AS_DATAFILE) 时 GetModuleInformation 失败?

c - 如何捕获数据包(NPF; WinPcap)?

php - 函数中的变量迭代

javascript - JS : Can I call a function with same scope as caller or do macro-like behavior