C 中的链表数组:初始化和插入? [英] Array of Linked Lists in C: initializing and inserting?

查看:17
本文介绍了C 中的链表数组:初始化和插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个链表数组(如图),这是我目前所做的:

I need to create an array of linked lists (as pictured) and this is what I've made so far:

typedef struct Node {
    int data;
    struct Node *next;
} Node;

int main(void) {
    Node* link[5];
    for(int q = 0; q < 5; q++) {
        link[q] = malloc(sizeof(struct Node));
        link[q] = NULL;
    }
}

我已经有一段时间没有在 C 中使用链表了,所以我已经忘记了很多语法,而且我很难想象在我编写链表时到底发生了什么.如果我没记错的话,当我在我的代码中调用 malloc 时,我正在创建一个没有任何内容的节点?

It's been awhile since I've used linked lists in C, so I've forgotten a lot of the syntax and I'm having trouble visualizing what exactly happens when I code linked lists. If I'm not mistaken, when I call malloc in my code, I'm creating a Node with nothing in it yet?

我想将其初始化为指向 NULL.我这样做了

I want to initialize it to point to NULL. And I did this with

link[q] = NULL;

我说这就是它在内存中的样子吗?

Am I right in saying this is what it looks like in memory?

|1|-> 空

|2|-> 空

|3|-> 空

我的下一个问题是将数据插入链表中.

My next problem would be inserting data into the linked list.

(参考图片):如果我想插入另一个元素到数组的第三个索引中([3]-> d -> NULL)

(Referring to the picture): If I want to insert another element into say the 3rd index of the array ([3]-> d -> NULL)

这是正确的吗?

Node* newNode = link[3];
newNode->data = 1;
link[3] = newNode;

感谢您的帮助!

推荐答案

这个循环

Node* link[5];
for(int q = 0; q < 5; q++) {
    link[q] = malloc(sizeof(struct Node));
    link[q] = NULL;
}

导致内存泄漏,因为首先分配内存,然后指针被 NULL 覆盖.所以分配的内存地址丢失了.

results in memory leaks because at first memory is allocated and then the pointers are overwritten with NULL. So the addresses of the allocated memory are lost.

你可以写

Node* link[5] = { 0 };

这是一个演示程序,展示了如何将节点添加到列表数组的元素中.int 类型的数据成员 data 的插入 我正在使用 char 类型的数据成员 data可见性.

Here is a demonstrative program that shows how nodes can be added to elements of the array of lists. Insetad of the data member data of the type int I am using the data member data of the type char for visibility.

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

typedef struct Node 
{
    char data;
    struct Node *next;
} Node;


int push_front( Node **head, char data )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

void output( Node **head )
{
    for( Node *current =*head; current != NULL; current = current->next )
    {
        printf( "%c ", current->data );
    }
    printf( "%s", "NULL" );
}

void display( Node **set, size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        output( set++ );
        putchar( '
' );
    }
}

#define N   5

int main(void) 
{
    Node * link[N] = { 0 };

    push_front( &link[0], 'b' );
    push_front( &link[0], 'a' );
    push_front( &link[1], 'c' );
    push_front( &link[2], 'd' );

    display( link, sizeof( link ) / sizeof( *link ) );

    return 0;
}

程序输出为

a b NULL
c NULL
d NULL
NULL
NULL

这篇关于C 中的链表数组:初始化和插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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