Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of Linked Lists in C: initializing and inserting?

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

enter image description here

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!

like image 855
zuzu Avatar asked Sep 20 '17 11:09

zuzu


People also ask

How do you implement a linked list in an array in C?

To add to it, an array in C/C++ can store derived data types such as structures, pointers, etc. Given below is the picture representation of an array. A linked list is a linear data structure consisting of nodes where each node contains a reference to the next node.

How do you initialise in array in C?

Array Initialization Using a LoopAn array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C.

How insertion operation can be performed in linked list?

To insert a node at the end of the Linked List, the next pointer of the last node should point to the new node and the next pointer of the new node must point to null.


1 Answers

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
like image 97
Vlad from Moscow Avatar answered Oct 20 '22 14:10

Vlad from Moscow