C语言中的链表数组

C语言中的链表数组,c,arrays,list,data-structures,linked-list,C,Arrays,List,Data Structures,Linked List,我想创建一个链表数组,其中每个数组元素都是链表的一个节点。基本上,我想通过数组元素索引一个链表。假设我们有一个数组a[20],其中每个元素表示链表的一个节点。图片如下: 我已经创建了一个链接列表,它将在其中输入并打印列表。但是,我需要帮助用数组索引它。这是我的链表 #include <stdio.h> #include <stdlib.h> int a[20]; typedef struct Node { int data; struct Node

我想创建一个链表数组,其中每个数组元素都是链表的一个节点。基本上,我想通过数组元素索引一个链表。假设我们有一个数组a[20],其中每个元素表示链表的一个节点。图片如下:

我已经创建了一个链接列表,它将在其中输入并打印列表。但是,我需要帮助用数组索引它。这是我的链表

#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;
}
我期待的是:-

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
这是使用数组元素修改的代码。我正在
当前[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;
}
#包括
#包括
类型定义结构节点{
int数据;
结构节点*下一步;
}节点;
节点*当前[20];
无效插入\u列表的\u beg\u(节点*当前[0],整数数据);
作废打印列表(节点*当前[0]);
void insert\u beg\u of_list(节点*当前[0],整数数据){
//跟踪第一个节点
节点*头=当前[0];
while(当前[0]->下一步!=头部){
当前[0]=当前[0]->下一步;
}
当前[0]->next=(节点*)malloc(sizeof(节点));
当前[0]=当前[0]->下一步;
当前[0]->数据=数据;
电流[0]->next=磁头;
}
作废打印列表(节点*当前[0]){
节点*头=当前[0];
当前[0]=当前[0]->下一步;
while(当前[0]!=头){
printf(“%d”,当前[0]->数据);
当前[0]=当前[0]->下一步;
}
}
int main(){
Node*head=(Node*)malloc(sizeof(Node));
头部->下一步=头部;
int数据=0;
int usr_输入=0;
int i;
int m;
int j;
scanf(“%d”和usr_输入);

对于(i=0;i如果要在数组的每个索引中存储一个新的链表,则不能使用相同的
head
节点(位于
main
函数第1行的节点)。它应该为数组中的每个索引分配一个新的head节点,然后将其余数据推送到后面


编辑顺便说一句:您的代码与您附加的图像不一致:在我的回答中,我假设您想要图像中的数据结构。另一方面,在您的代码中,根据只调用一次的函数
print_list
(而不是数组中的每个索引)根据您只有一个head节点的事实,可能您想要其他的东西(只有一个常规列表?),然后您就不清楚您的意思了。

如果您想在数组的每个索引中存储一个新的链接列表,您不能使用相同的
head
节点(在
main
函数中第一行的节点)。它应该为数组中的每个索引分配一个新的头节点,然后将其余的数据推送到后面


编辑顺便说一句:您的代码与您附加的图像不一致:在我的回答中,我假设您想要图像中的数据结构。另一方面,在您的代码中,根据只调用一次的函数
print_list
(而不是数组中的每个索引)根据你只有一个头部节点的事实,也许你想要其他的东西(只有一个总列表?),然后你的意思就不太清楚了。

我认为你需要一个链表数组,显然每个链表都应该有单独的头节点。如果我想的正确,那么你可以看到下面给出的代码。在这里发布之前,会检查它

#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;
     }
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
}*总目[5];
无效创建(整数计数);
无效打印(整数计数);
int main()
{
inti,n;
n=5;/*n是节点总数*/
对于(i=0;不精确=NULL;
人头[计数]=临时;
}
其他的
{
temp->next=(结构节点*)malloc(sizeof(结构节点));
温度=温度->下一步;
温度->数据=j+5+计数;
temp->next=NULL;
}
}
}
无效打印(整数计数)
{
结构节点*temp;
温度=人头[计数];
while(temp!=NULL)
{
printf(“%d->”,临时->数据);
温度=温度->下一步;
}
}

我认为您需要一个链表数组,显然每个链表都应该有单独的头节点。如果我的想法正确,那么您可以看到下面给出的代码。在这里发布之前会检查它

#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;
     }
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
}*总目[5];
无效创建(整数计数);
无效打印(整数计数);
int main()
{
inti,n;
n=5;/*n是节点总数*/
对于(i=0;不精确=NULL;
人头[计数]=临时;
}
其他的
{
temp->next=(结构节点*)malloc(sizeof(结构节点));
温度=温度->下一步;
温度->数据=j+5+计数;
temp->next=NULL;
}
}
}
无效打印(整数计数)
{
结构节点*temp;
温度=人头[计数];
while(temp!=NULL)
{
printf(“%d->”,临时->数据);
温度=温度->下一步;
}
}

这里没有实现数组。我的代码只是一个链表。数组索引,这就是我想要实现的。你想要实现数组索引是什么意思?请试着更好地解释你的目的是什么。另外,请说明已经给出了代码的哪一部分以及你的那一部分。嗨,我想在每个索引中存储一个新的链表.到目前为止,我已经实现了一个简单的链表数据结构,我想存储在数组的每个索引中。因此,正如我试图解释的,您必须创建一个新的头节点(表示一个新的链表)在每个索引中。现在,您只在程序开始时执行一次。您需要在for循环中,在每次迭代中执行一次。这里没有实现数组。我的代码只是一个链表。数组索引,这就是我要实现的。您要实现数组索引意味着什么?请尝试更好地解释您的pu是什么rpose man。另外,请说明已经给出了代码的哪一部分以及您的那一部分。您好,我想在每个索引中存储一个新的链表。到目前为止,我已经实现了一个简单的链表数据结构,我想存储它