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;
}
5
  • 3
    Node *current[0] --> Node *current Commented May 4, 2017 at 12:20
  • You must always check malloc returned values != NULL Commented May 4, 2017 at 12:21
  • 3
    @BLUEPIXY Correct, but I bet OP is confusing global current array with the passed parameter... Commented May 4, 2017 at 12:23
  • with Node *current , it will not be stored within array element. In future, I want to use current [1], current [2] and so on. Commented May 4, 2017 at 12:35
  • currrent[0] = head;.... insert_beg_of_list(currrent[0], data); Commented May 4, 2017 at 12:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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
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.
Got it. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.