#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* next_node;
}NODE;
NODE* create_list(void) {
NODE* new_node=malloc(sizeof(NODE));
new_node->next_node=NULL;
new_node->data=0;
return new_node;
}
int insert_at_beginning(NODE* first_node, int data) {
NODE* new_node=malloc(sizeof(NODE));
if (new_node==NULL) {
printf("error allocating memory");
return -1;
}
if (first_node==NULL) {
printf("invalid linked list passed");
return -2;
}
new_node->data=data;
new_node->next_node=first_node; //The next_node points to the preivous first node.
first_node=new_node; //New node is set to the first node.
return 0;
}
int print_list(NODE* first_node) {
NODE* buffer=first_node;
while (buffer!=NULL) {
printf("%i\n", buffer->data);
buffer=buffer->next_node;
}
return 0;
}
int main(void) {
NODE* new_table=create_list();
int num=5;
int num1=10;
int num2=15;
insert_at_beginning(new_table, num);
insert_at_beginning(new_table, num1);
insert_at_beginning(new_table, num2);
print_list(new_table);
}
trying to make a simple implementation of a linked list, but the print_list function just prints out 0. Where might be the problem ??