I'm starting to learn to work with linked lists. Specifically, I'm trying to make a LIFO (Last In, First Out) linked list. I made different functions to insert a node, to print the whole list, to free the allocated memory of every node... There's also a global variable called root that points to the first node of the list. Here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *root = NULL;

void InsertNode(int number) {
    struct node *NewNode;
    NewNode = malloc(sizeof(struct node));
    NewNode->data = number;

    if (root == NULL) {
        NewNode->next = NULL;
        root = NewNode;
    }
    else {
        NewNode->next = root;
        root = NewNode;
    }
}

void PrintList(void) {
    struct node *CurrentNode = root;
    while (root != NULL) {
        printf("%i\n", CurrentNode->data);
        CurrentNode = CurrentNode->next;
    }
}

int ExtractFirstNode(void) {
    if (root != NULL) {
        int extracted = root->data;
        struct node *NodeToBeFreed = root;
        root = root->next;
        free(NodeToBeFreed);
        return extracted;
    }
    else {
        return -1;
    }
}

void FreeWholeList(void) {
    struct node *CurrentNode = root;
    struct node *NodeToBeFreed;
    while (CurrentNode != NULL) {
        NodeToBeFreed = CurrentNode;
        CurrentNode = CurrentNode->next;
        free(NodeToBeFreed);
    }
}

int main(void) {
    InsertNode(6);
    InsertNode(4);
    InsertNode(8);
    InsertNode(7);
    printf("Complete list:\n");
    PrintList();
    printf("\n");
    printf("Extracting the first node: %i\n", ExtractFirstNode());
    FreeWholeList();
    return 0;
}
There's also a function called ExtractFirstNode() that just frees the first node. It returns the number of the extracted node, and is called in the main() function to see what number was storing the first node which has been freed. I'll paste the code of the function in question:
Code:
int ExtractFirstNode() {
    if (root != NULL) {
        int extracted = root->data;
        struct node *NodeToBeFreed = root;
        root = root->next;
        free(NodeToBeFreed);
        return extracted;
    }
    else {
        return -1;
    }
}
If the list is empty it will return -1, which will be used as an error code. Like I said, the number it returns is used in the main() function to inform the user the first number that has been freed:
Code:
int main(void) {
    InsertNode(6);
    InsertNode(4);
    InsertNode(8);
    InsertNode(7);
    printf("Complete list:\n");
    PrintList();
    printf("\n");
    printf("Extracting the first node: %i\n", ExtractFirstNode());
    FreeWholeList();
    return 0;
}
But the function is being quite problematic, because it doesn't return the number of the first node. Instead the program's output is the following:
Code:
Complete list:
7
8
4
6
Segmentation fault

Process returned 139 (0x8B)   execution time : 0.001 s
Press ENTER to continue.
Why is it not printing the recently freed node's data?
Thanks in advance.