I am trying to create a singly linked list. I have created five nodes and initialized them with a integer value. But when I print the Linked List I get nothing.
typedef struct node {
int value;
struct node* nextPtr;
} node;
node *nodePtr;
node *head;
void initializeLinkedList() {
static unsigned int i;
nodePtr = (node*)malloc(sizeof(node));
i = 0;
nodePtr->nextPtr = (node*)malloc(sizeof(node));
nodePtr->value = i;
head = nodePtr;
for (i = 1; i < 5; i++) {
nodePtr->nextPtr = (node*)malloc(sizeof(node));
nodePtr->value = i;
}
nodePtr->nextPtr = NULL;
}
void printLinkedList() {
static unsigned int i;
i = 0;
nodePtr = head;
while (nodePtr->nextPtr != NULL) {
printf("Value of ptr is %p \n", nodePtr->nextPtr);
printf("Value is %d \n", nodePtr->value);
}
}
I think I am not setting the pointers properly.