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

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

问题描述

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

  typedef struct节点{整数数据;struct Node * next;}节点;int main(void){节点*链接[5];for(int q = 0; q< 5; q ++){link [q] = malloc(sizeof(struct Node));link [q] = NULL;}} 

自从我在C语言中使用链表以来已经有一段时间了,所以我忘记了很多语法,而且我在可视化编码链表时究竟会发生什么.如果我没记错的话,当我在代码中调用malloc时,是否正在创建一个没有任何内容的Node?

我想将其初始化为NULL.而我是用

  link [q] = NULL; 

我是说对了吗?

| 1 |-> NULL

| 2 |-> NULL

| 3 |-> NULL


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

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

这是正确的吗?

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

谢谢您的帮助!

解决方案

此循环

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

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

你可以写

  Node *链接[5] = {0}; 

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

  #include< stdio.h>#include< stdlib.h>typedef结构节点{字符数据;struct Node * next;}节点;int push_front(节点** head,char data){节点* new_node = malloc(sizeof(Node));int成功= new_node!= NULL;如果(成功){new_node-> data = data;new_node-> next = * head;* head = new_node;}返回成功;}无效输出(Node ** head){for(节点*当前= *头;当前!= NULL;当前=当前->下一个){printf(%c",current-> data);}printf(%s","NULL");}无效显示(Node ** set,size_t n){对于(size_t i = 0; i< n; i ++){输出(set ++);putchar('\ n');}}#定义N 5int main(无效){节点* 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));返回0;} 

程序输出为

  a b NULLc NULLd空空值空值 

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;
    }
}

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?

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| -> NULL

|2| -> NULL

|3| -> NULL


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

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

Would this be correct?

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

Thank you for the help!

解决方案

This loop

Node* link[5];
for(int q = 0; q < 5; q++) {
    link[q] = malloc(sizeof(struct Node));
    link[q] = 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.

You could just write

Node* link[5] = { 0 };

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( '\n' );
    }
}

#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;
}

The program output is

a b NULL
c NULL
d NULL
NULL
NULL

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

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