Help with linked lists/ pointers
I am trying to write a simple C program that creates a linked list and then prints out the values stored in the list. I am not sure what part of the code isn't correct but when it prints out the entries it will only print out one entry and it looks to be a memory location instead of the value stored in the node. Also, and this is the part that really throws me off, sometimes when i compile the code i get the error "invalid conversion from 'void*' to 'node*' " on every line that I use malloc and other times it compiles with no errors. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node *nodePtr;
struct node {
int value;
nodePtr next;
};
typedef struct node node;
void createNewNode(nodePtr head, nodePtr newNode);
void printList(nodePtr head);
int main(int argc, const char * argv[]) {
nodePtr first = malloc(sizeof(node));
nodePtr newNode = malloc(sizeof(node));
int numberOfEntries = 0;
first->next = NULL;
printf("Number of Entries: ");
scanf("%d", &numberOfEntries);
printf("\n");
if(numberOfEntries == 1) {
printf("Enter Value: ");
scanf("%d", first->value);
printf("\n");
} else {
int i = 0;
while(i < numberOfEntries) {
printf("Enter Value: ");
scanf("%d", first->value);
printf("\n");
createNewNode(first, newNode);
i++;
}
}
printList(first);
return 0;
}
void createNewNode(nodePtr head, nodePtr newNode) {
newNode->next = head;
head = newNode;
}
void printList(nodePtr head) {
nodePtr current = malloc(sizeof(node));
current = head;
while(current->next != NULL) {
printf("Entry: %d\n", current->value);
current = current->next;
}
printf("Entry: %d\n", current->value);
free(current);
}
If someone could take a look at this and point me to where I've made a mistake I would greatly appreciate it.