【问题标题】:Segmentation fault (core dumped) in Linked List using C使用 C 的链表中的分段错误(核心转储)
【发布时间】:2017-05-04 12:17:17
【问题描述】:

我想要一个链表数组,显然每个链表都应该有单独的头节点。最初,作为一个例子,我从一个数组元素开始。我将链表存储到 current[0] 中。但它给出了分段错误。如果我使用Node *current,它将创建一个列表并正常工作。但是,我想将列表存储在数组中。代码有什么问题?

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

【问题讨论】:

  • Node *current[0] --> Node *current
  • 你必须经常检查malloc返回值!= NULL
  • @BLUEPIXY 正确,但我敢打赌 OP 将全局 current 数组与传递的参数混淆...
  • 使用 Node *current ,它不会被存储在数组元素中。以后我想用current[1],current[2]等等。
  • currrent[0] = head;....insert_beg_of_list(currrent[0], data);

标签: c data-structures linked-list segmentation-fault


【解决方案1】:

我认为你混合了全局数组current 的使用。将您的代码更改为:

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

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[0]->next;
    }
    current->next = malloc(sizeof(Node));
    if (current->next == NULL)
        return;
    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 *current[20];
    Node *head = malloc(sizeof(Node));
    if (head == NULL)
        return;

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

    //assign the newly created pointer to a place in the array
    current[0] = head;

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

    return 0;
}

请记住,函数原型和声明中的参数currentnot the same,就像在main 函数中创建的数组current。我只是保持原样。

注意:你应该用head-&gt;next指针做一些事情,初始化它。


另请阅读this link,了解为什么不强制转换malloc 的结果,以及another one,了解您应该检查其结果的原因。

【讨论】:

    【解决方案2】:

    你可能想要这个:

    #include <stdio.h>
    #include <stdlib.h>
    
    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;
      }
    }
    
    Node *NewList()
    {
      Node *newnode = (Node *)malloc(sizeof(Node));
      newnode->next = newnode;
    }
    
    int main() {
      Node *arrayofheads[20];
    
      // We are using only arrayofheads[0] in this example
    
      arrayofheads[0] = NewList();
    
      int data = 0;
      int usr_input = 0;
      int i;
    
      scanf("%d", &usr_input);
    
      for (i = 0; i<usr_input; i++) {
        scanf("%d", &data);
        insert_beg_of_list(arrayofheads[0], data);
      }
    
      printf("The list is ");
      print_list(arrayofheads[0]);  printf("\n\n");
    
      return 0;
    }
    

    【讨论】:

    • 感谢您的回答。我的兴趣不仅仅是创建一个列表,而是创建一个指针数组并将列表存储在数组元素中。以后我会用current[1],current[2]等等
    • 还有改进的余地,特别是你应该用函数完全封装列表,所以你不需要在调用函数中混淆headnextmain在这里)。我刚刚相应地更新了答案。
    • 知道了。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多