使用C在链接列表中进行分段错误(核心转储)

时间:2017-05-04 12:17:17

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

我想要一个链接List的数组,显然每个链接应该有单独的头节点。最初,作为一个例子,我从一个数组元素开始。我将linkedlist存储到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;
}

2 个答案:

答案 0 :(得分: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;
}

请注意您的功能中的参数current&#39;原型和声明not the samecurrent函数中创建的数组main。我刚刚离开了这个名字。

注意:您应该使用head->next指针执行某些操作,然后对其进行初始化。

另请阅读this link,了解为什么不展示mallocanother one的结果,了解为什么要检查结果。

答案 1 :(得分:0)

你可能想要这个:

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