Un tableau de liste liée dans C

Je veux créer un tableau de liste liée, où chaque élément du tableau est un nœud de la liste liée. En gros, je veux un index d'une liste liée par un élément de tableau. Supposons, nous avons un tableau a[20], où chaque élément à représenter un nœud de la liste liée. L'image ci-dessous :-

Tableau de la liste chaînée

J'ai créé une liste liée, où il va prendre une entrée et d'imprimer la liste. Mais, j'ai besoin de l'aide de l'index avec un tableau. C'est ma liste liée.

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

À titre d'exemple :-

ce qu'il fait maintenant :-

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

Ce que j'attends :-

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

C'est le code modifié avec l'élément de Tableau. Je suis de stocker les données dans 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;
}

C'est de la faute de Segmentation.

InformationsquelleAutor Cyberzinga | 2017-04-30