An array of linked list in C
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;
}
}
Cyberzinga
Updated on July 30, 2021Comments
-
Cyberzinga almost 3 years
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 :-
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.
-
Cyberzinga about 7 yearsArray not implemented here. My code is just a linked list. Array index, that is what I want to implement.
-
SHG about 7 yearsWhat does it mean that you want to implement an array index? Please try to explain better what's your purpose man. Also, mention what part of code is already given and what's your part.
-
Cyberzinga about 7 yearsHi, I want to store a new linked list in every index . Till now, I have implemented a simple linked list data structure that I want to store in every index of array.
-
SHG about 7 yearsSo, as I tried to explain, you'll have to create a new head node (that represents a new linked list) in every index. Right now you do it only once in the beginning of your program. You need to do it inside your for loop, in every iteration.
-
Cyberzinga about 7 yearsHi @Zaid Khan , I appreciate your answer. I was trying to use the same logic in my code. I have updated the question with modified code. I am trying to store data within current [0] , as an example. But it is showing segmentation fault. Can you please have a look ?
-
Zaid Khan about 7 yearsYour code is a bit less understandable but still i found some logical mistakes in your code. You want to insert every node at the beginning but look at your code in insert_at_beg() you are making a loop of two nodes. And i am not understanding that why you are declare an parameter current[0] inside function when you already declared it globally. And why you are using while loop in insert_at_beg() there is not any need of using loop. you have not assigned the very first node->next=NULL. I have edited your code and please see this at ideone.com/UdTlZy This code is working .