简体   繁体   中英

An array of linked list in C

I want to create an array of linked list where each array element is a node of linked list. Basically, I want to index a linked list by an array element. Suppose, we have an array a[20], where each element represent a node of linked list. Picture given below :-

Array of Linked list

I have created a linked list, where it will take input and print the list. But, I need help to index it with an array. This is my linked list.

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

int a[20];

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

void insert_beg_of_list(Node *current, int data);

void print_list(Node *current);


void insert_beg_of_list(Node *current, int data) {
    //keep track of first node
    Node *head = current;

    while(current->next != head) {
        current = current->next;
    }
    current->next = (Node*)malloc(sizeof(Node));
    current = current->next;
    current->data = data;
    current->next = head;


}


void print_list(Node *current) {

    Node *head = current;
    current = current->next;
    while(current != head){
        printf(" %d ", current->data);
        current = current->next;
    }

}



int main() {

    Node *head = (Node *)malloc(sizeof(Node));
    head->next = head;  

    int data = 0 ;
    int usr_input = 0;
    int i;
    int m;




        scanf("%d", &usr_input);

        for (i=0; i<usr_input; i++) {

            scanf("%d", &data);
            insert_beg_of_list(head, data);

        }


            printf("The list is ");
            print_list(head);
            printf("\n\n");





    return 0;
}

As an example :-

what it does now :-

Input :- Number of elements = 5
Input :- Elements = 1 2 3 4 5
Output :- 1 2 3 4 5

What I am expecting :-

Input :- 
Number of elements for a[0] = 4
Input for a[0] = 1 2 3 4
Number of elements for a[1] = 4
Input for a[1] = 2 3 5 4

Expected Output :- 

a[0] = 1 2 3 4
a[1] = 2 3 5 4

This is the modified code with Array element. I am storing the the data within current [0]

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




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


Node *current[20];

void insert_beg_of_list(Node *current[0], int data);

void print_list(Node *current[0]);



void insert_beg_of_list(Node *current[0], int data) {


    //keep track of first node
    Node *head = current[0];

    while(current[0]->next != head) {
        current[0] = current[0]->next;
    }
    current[0]->next = (Node*)malloc(sizeof(Node));
    current[0] = current[0]->next;
    current[0]->data = data;
    current[0]->next = head;


}



void print_list(Node *current[0]) {


    Node *head = current[0];
    current[0] = current[0]->next;
    while(current[0] != head){
        printf(" %d ", current[0]->data);
        current[0] = current[0]->next;
    }

}



int main() {

    Node *head = (Node *)malloc(sizeof(Node));
    head->next = head;  

    int data = 0 ;
    int usr_input = 0;
    int i;
    int m;
    int j;




        scanf("%d", &usr_input);

        for (i=0; i<usr_input; i++) {

            scanf("%d", &data);
            insert_beg_of_list(head, data);

        }


            printf("The list is ");
            print_list(head);
            printf("\n\n");





    return 0;
}

It is showing Segmentation fault.

If you want to store a new linked list in every index of the array, you cannot use the same head node (the one from 1st line in main function). It should allocate a new head node to each index in the array, and then to push the rest of the data after it.

EDIT BTW: Your code is inconsistent with the image you attached: In my answer, I assume you want a data-structure like in the image. On the other hand, in your code, according to function print_list which you call only once (and not for each index in the array) and according to the fact you have only one head node, maybe you wanted something else (only one general list?), and then it's not really clear what you mean.

I think that you want an array of linked List and obviously each linked should have separate head node. If am thinking correct then you can see the code given below. It is checked before posting here.

#include <stdio.h>
#include <stdlib.h>
struct node{
    int data;
    struct node *next;
}*head[5];
void create(int count);
void print(int count);
int main()
{
    int i,n;
    n=5;  /*n is the total number of nodes */
    for(i=0;i<n;i++)
    {
         head[i]=NULL;
         create(i);
         print(i);
         printf("\n\n");
     }
    return 0;
}
void create(int count)
{
      int n2=5;  /*n2 is the number of nodes in a single linked list*/
      int j;
      struct node *temp;
      for(j=0;j<5;j++)
      {
             if(head[count]==NULL)
             {
                 temp=(struct node*)malloc(sizeof(struct node));
                 temp->data=j+5+count;
                 temp->next=NULL;
                 head[count]=temp;
             }
             else
             {
                temp->next=(struct node*)malloc(sizeof(struct node));
                 temp=temp->next;
                 temp->data=j+5+count;
                 temp->next=NULL;
             }
     }
}
void print(int count)
{
     struct node *temp;
     temp=head[count];
     while(temp!=NULL)
     {
          printf("%d->",temp->data);
          temp=temp->next;
     }
}

Hey, may be this easy code written in C may help you a little:

#include "stdio.h"
#include "stdlib.h"

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

struct node *temp;

int main(){
    int size,si;
    printf("\nEnter the size of array : ");
    scanf("%d",&size);
    struct node *A[size];
    for(int i=0;i<size;i++){
        printf("\nEnter the size of linked list in array[%d] : ",i);
        scanf("%d",&si);
        head = (struct node *)malloc(sizeof(struct node));
        printf("\nEnter the data:");
        scanf("%d",&head -> data);
        head -> next = NULL;
        temp = head;
        for(int j=1;j<si;j++){
            struct node *new = (struct node *)malloc(sizeof(struct node));
            printf("\nEnter the data:");
            scanf("%d",&new -> data);
            new -> next = NULL;
            temp -> next = new;
            temp = new;
        }
        A[i] = head;
    }
    
    for(int i=0;i<size;i++){
        printf("\nArr[%d] =>   ",i);
        struct node *p;
        p = A[i];
        while(p!=NULL){
            printf("%d\t",p->data);
            p = p -> next;
        }
        printf("\n");
    }
    return 0;
}

: OUTPUT :

Enter the size of array : 3

Enter the size of linked list in array[0] : 3

Enter the data:1

Enter the data:2

Enter the data:3

Enter the size of linked list in array[1] : 5

Enter the data:1

Enter the data:2

Enter the data:3

Enter the data:4

Enter the data:5

Enter the size of linked list in array[2] : 4

Enter the data:9

Enter the data:8

Enter the data:7

Enter the data:6

Arr[0] =>   1   2   3   

Arr[1] =>   1   2   3   4   5   

Arr[2] =>   9   8   7   6   
Program ended with exit code: 0

Explanation : In this program I have created a dynamic array of user defined size and then created a linked list of user defined size and assigned head of each linked list in each blocks of array. I have shown direct implementation, so everything is in main function, but this can also be done via creating separate functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM