-1

I am currently trying to implement a Linked List in C. However, my function to create the head is not working apparently, since my other function to add a new node throws a dereferencing null pointer exception. Also, the size variable that keeps the amount of nodes is not being increased. Here is my full code.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>

typedef struct node {
    int val;
    struct node* next;
} Node;

void printView(int);
bool terminate();
void createHead(Node*, int);
void addNext(Node*, int);

int main()
{
    srand(time(NULL));
    int size = 0;
    Node* head = NULL;

    bool created = false;
    bool end = false;
    while (!end)
    {
        printView(size);
        int choice;
        scanf_s("%d", &choice);

        switch (choice)
        {
            case 1:
            {
                if (!created)
                {
                    createHead(head, size);
                    created = true;
                }
                else
                    printf("The head has already been created \n");
                break;
            }
            case 2:
            {
                if (created)
                    addNext(head, size);
                else
                    printf("The head needs to be created first \n");
                break;
            }
            case 0:
            {
                bool t = terminate();
                if (t)
                    end = true;
                break;
            }
        }
    }

    return 0;
}

void printView(int size)
{
    printf("Welcome to Linked Lists! - %d Nodes in List\n", size);
    printf("    Type 1 to create a head \n");
    printf("    Type 2 to create a new node \n");
    printf("    Type 0 to exit \n");
}

bool terminate() //Exit
{
    int save;
    printf("Would you like to save your Linked List? \n(Enter 1 to save or 0 for not to save) \n");
    scanf_s("%d", &save);
    if (save == 1)
    {
        printf("The Linked List has been saved. It will show up next time you start the program \n");
    }
    else if (save == 0)
        printf("Goodbye! \n");
    else
    {
        printf("Please type a valid option \n");
        return false;
    }
    return true;
}

void createHead(Node* head, int size)
{
    head = malloc(sizeof(Node));
    if (head == NULL) {
        printf("Failed to create head, aborting operation \n");
        return;
    }
    printf("Type a value for the new node: \n(It must be an integer / Type 0 to assign a random number) \n");
    int value; scanf_s("%d", &value);
    if (value == 0)
    {
        value = rand() % 11;
        head->val = value;
        printf("Value set to: %d \n", value);
    }
    else
        head->val = value;
    head->next = NULL;
    size++;
}

void addNext(Node* node, int size)
{
    Node* current = node;
    while (current->next != NULL)
        current = current->next;

    current->next = malloc(sizeof(Node));
    if (current->next == NULL)
    {
        printf("Failed to create new node, aborting operation \n");
        return;
    }
    printf("Type a value for the new node: \n(It must be an integer / Type 0 to assign a random number) \n");
    int value; scanf_s("%d", &value);
    if (value == 0)
    {
        value = rand() % 11;
        current->val = value;
        printf("Value set to: %d \n", value);
    }
    else
        current->val = value;
    current->next = NULL;
    size++;
}

1 Answers1

0

The function create_head only modifies its arguments, not the variables in the calling function main. You should change the prototype to bool createHead(Node **headp, int *sizep) and update the values indirectly`.

There are other problems:

  • some include files are missing
  • the same problem in add_next() prevents size from getting updated in main.
  • passing the address of head to addNext removes the need for a separate function to create the initial list node.
  • it would be safer to define a List structure with a head and a size fields and pass that to the different functions.

Here is a modified version:

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

typedef struct node {
    int val;
    struct node *next;
} Node;

void printView(int size);
bool terminate(void);
bool createHead(Node **headp, int *sizep);
bool addNext(Node **headp, int *sizep);

int main() {
    srand(time(NULL));
    int size = 0;
    Node *head = NULL;
    bool created = false;
    bool end = false;

    while (!end) {
        printView(size);
        int choice;
        scanf_s("%d", &choice);

        switch (choice) {
            case 1:
                if (!created) {
                    created = createHead(&head, &size);
                } else {
                    printf("The head has already been created \n");
                }
                break;
            case 2:
                if (created)
                    addNext(&head, &size);
                else
                    printf("The head needs to be created first \n");
                break;
            case 0:
                end = terminate();
                break;
            }
        }
    }
    return 0;
}

void printView(int size) {
    printf("Welcome to Linked Lists! - %d Nodes in List\n", size);
    printf("    Type 1 to create a head \n");
    printf("    Type 2 to create a new node \n");
    printf("    Type 0 to exit \n");
}

bool terminate(void) {
    int save = 0;
    printf("Would you like to save your Linked List? \n"
           "(Enter 1 to save or 0 for not to save) \n");
    scanf_s("%d", &save);
    if (save == 1) {
        // XXX: save the list
        printf("The Linked List has been saved. It will show up next time you start the program \n");
    } else if (save == 0) {
        printf("Goodbye! \n");
    } else {
        printf("Please type a valid option \n");
        return false;
    }
    return true;
}

bool createHead(Node **headp, int *sizep) {
    Node *head = malloc(sizeof(Node));
    if (head == NULL) {
        printf("Failed to create head, aborting operation \n");
        *headp = head;
        *sizep = 0;
        return false;
    }
    printf("Type a value for the new node: \n"
           "(It must be an integer / Type 0 to assign a random number) \n");
    int value;
    scanf_s("%d", &value);
    if (value == 0) {
        value = rand() % 11;
        printf("Value set to: %d \n", value);
    }
    head->val = value;
    head->next = NULL;
    *headp = head;
    *sizep = 1;
    return true;
}

bool addNext(Node **headp, int *sizep) {
    Node *new_node = malloc(sizeof(Node));
    if (new_node == NULL) {
        printf("Failed to create new node, aborting operation \n");
        return false;
    }
    printf("Type a value for the new node: \n(It must be an integer / Type 0 to assign a random number) \n");
    int value;
    scanf_s("%d", &value);
    if (value == 0) {
        value = rand() % 11;
        printf("Value set to: %d \n", value);
    }
    current->val = value;
    current->next = NULL;
    if (*headp == NULL) {
        *headp = new_node;
    } else {
        Node *current = *headp;
        while (current->next != NULL)
            current = current->next;
        current->next = new_node;
    }
    *sizep += 1;
    return true;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189