【问题标题】:An array of linked list in CC中的链表数组
【发布时间】:2017-04-30 22:05:31
【问题描述】:

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

Array of 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;
}

举个例子:-

它现在做了什么:-

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

我的期望:-

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

这是带有数组元素的修改代码。我将数据存储在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 arrays list data-structures linked-list


    【解决方案1】:

    我认为你想要一个链接列表数组,显然每个链接都应该有单独的头节点。如果我的想法是正确的,那么您可以看到下面给出的代码。在此处发布之前会对其进行检查。

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

    【讨论】:

    • 嗨@Zaid Khan,感谢您的回答。我试图在我的代码中使用相同的逻辑。我已经用修改后的代码更新了这个问题。例如,我正在尝试将数据存储在当前 [0] 中。但它显示分段错误。你能看看吗?
    • 您的代码有点难以理解,但我仍然在您的代码中发现了一些逻辑错误。您想在开头插入每个节点,但请查看 insert_at_beg() 中的代码,您正在制作两个节点的循环。而且我不明白为什么你已经在全局声明了一个参数 current[0] 在函数内部。以及为什么在 insert_at_beg() 中使用 while 循环,不需要使用循环。您尚未分配第一个节点->next=NULL。我已经编辑了您的代码,请在ideone.com/UdTlZy 上查看此代码。
    【解决方案2】:

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

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

    【讨论】:

    • 数组未在此处实现。我的代码只是一个链表。数组索引,就是我要实现的。
    • 要实现数组索引是什么意思?请尝试更好地解释您的目的是什么。另外,请提及已经给出的代码部分以及您的部分。
    • 嗨,我想在每个索引中存储一个新的链表。到目前为止,我已经实现了一个简单的链表数据结构,我想将它存储在数组的每个索引中。
    • 因此,正如我试图解释的那样,您必须在每个索引中创建一个新的头节点(代表一个新的链表)。现在您只在程序开始时执行一次。你需要在你的 for 循环中,在每次迭代中这样做。
    【解决方案3】:

    嘿,这个用 C 编写的简单代码可能对你有一点帮助:

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

    :输出:

    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
    

    说明:在这个程序中,我创建了一个用户的动态数组 定义大小,然后创建用户定义大小的链接列表和 在每个数组块中分配每个链表的头。我有 显示了直接实现,所以一切都在 main 函数中,但是 这也可以通过创建单独的函数来完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多