0

I want an array of linked List and obviously each linked should have separate head node. Initially, as an example, I am starting with one array element. I am storing linkedlist into current[0] . But it is giving segmentation fault. If I use Node *current it will create a list and working fine. But, I want to store the list within array. What is wrong with the code ?

#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;
}
cyberoy
  • 363
  • 2
  • 7
  • 18

2 Answers2

1

I think you have mixed the global array current's use. Change your code to this :

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

Keep in mind that the parameter current in your functions' prototypes and declarations is not the same as the array current created in your main function. I just left the name as it was.

NOTE : You should do something with head->next pointer, initialize it.


Also read this link on why not to cast the result of malloc and another one on why you should check its result.

Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33
0

You probably want this:

#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;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I appreciate your answer. My interest is not just to create a list, but I want to create an array of pointers and store the list within array element. In future, I will use current [1], current [2] and so on – cyberoy May 04 '17 at 12:47
  • There is still room for improvement, especially you should totally encapsulate the list with functions, so you don't need to mess with `head` and `next` in the calling functions (`main` here). I just updated the answer accordingly. – Jabberwocky May 04 '17 at 12:58
  • Got it. Thanks a lot – cyberoy May 04 '17 at 13:03