clinked-listsegmentation-faultfunction-pointers

Segmentation core dumped when trying to build a single linklist


I was learning link list concepts in C, as I was building a project that can create a single reverse order list, I am encountering segmentation code dumped again and again. Kindly help me find my errors


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

typedef struct node{
    int num;
    struct node *next;
}node;

node* createll(node *head, node *error);
void print_list(node *head);

int main(void){
    node *head = NULL;
    node *error = NULL;
    error->num = 1;
    error->next = NULL;

    head = createll(head, error);
    print_list(head);
    return 0;
}

node* createll(node *head, node *error){
    int len;
    printf("Size of list: ");
    scanf("%d", &len);

    for (int i = 0; i < len; i++) {
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL) {
            return error;
        }
        printf("Enter the data: ");
        scanf("%d", &new_node->num);
        new_node->next = NULL;

        new_node = head;
        head = new_node;
    }

    return head;
}


void print_list(node *head){
    node *temp;
    printf("Data stored is : ");
    for (temp = head; temp != NULL; temp = temp->next){
        printf("%d-->", temp->num);
    }
    printf("\n");
}

this is my code. I


Solution

  • Here are some of the issues:

    Here is a possible version of your code that addresses the above points:

    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
        int num;
        struct node *next;
    } Node;
    
    Node* create_node(int num, Node* next);
    void print_list(Node *head);
    
    int main(void) {
        Node *head = NULL;
        int num, len;
    
        // Perform input outside of functions that implement
        //    linked list operations (separation of concern).
        printf("Size of list: ");
        scanf("%d", &len);
        for (int i = 0; i < len; i++) {
            // Get the data into an int; not directly into a node member.
            printf("Enter the data for node %d: ", i + 1);
            scanf("%d", &num);
            head = create_node(num, head);
        }
        print_list(head);
        return 0;
    }
    
    // Separate the logic for creating one node and 
    //     for building a multi-node linked list:
    Node* create_node(int num, Node* next) {
        Node *new_node = malloc(sizeof(Node));
        if (!new_node) {
            // Don't return, but exit
            printf("Could not allocate memory for a new node.");
            exit(1);
        }
        new_node->num = num;
        new_node->next = next;
        return new_node;
    }
    
    void print_list(Node *head){
        printf("Linked list: ");
        for (Node *temp = head; temp; temp = temp->next) {
            printf("%d -> ", temp->num);
        }
        printf("null\n");
    }
    

    Note that prepending values into a linked list produces a linked list that has its values in reversed order. From your question I understand that this is what you wanted to happen.