I am currently experimenting with singly-linked-list in C. I have written a
newNode
function to create a Node and a printNodes
function to print out all nodes - it looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int data;
struct Node *next;
};
void printNodes(struct Node *current_node)
{
while(current_node != NULL)
{
printf("Node is: %d\n", current_node->data);
current_node = current_node->next;
}
}
int main()
{
int number_1 = 2;
int number_2 = 3;
int number_3 = 4;
struct Node *head;
struct Node *second;
struct Node *third;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = number_1;
head->next = second;
second->data = number_2;
second->next = third;
third->data = number_3;
third->next = NULL;
printNodes(head);
}
output is correctly:
Node is: 2
Node is: 3
Node is: 4
Now I wanted to write a function newNode
which is used to create a new node, I changed my code to this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *newNode(int number_x, struct Node *nextnode)
{
struct Node *tmp_node;
tmp_node = malloc(sizeof(struct Node));
tmp_node->data = malloc(sizeof(struct Node));
tmp_node->data = number_x;
tmp_node->next = nextnode;
return tmp_node;
}
void printNodes(struct Node *current_node)
{
while(current_node != NULL)
{
printf("Node is: %d\n", current_node->data);
current_node = current_node->next;
}
}
int main()
{
int number_1 = 2;
int number_2 = 3;
int number_3 = 4;
struct Node *head;
struct Node *second;
struct Node *third;
head = newNode(number_1, second);
second = newNode(number_2, third);
third = newNode(number_3, NULL);
printNodes(head);
}
After compiling I first get this warning message:
test.c:16:20: warning: incompatible pointer to integer conversion
assigning to 'int' from 'void *' [-Wint-conversion]
tmp_node->data = malloc(sizeof(struct Node));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
And the output looks like this:
Node is: 2
It only shows the Node head
, I guess there is something wrong with next
pointing
(e.g. head->next = second
), but what is wrong? I am unable to solve this.
Thanks
Here
tmp_node->data = malloc(sizeof(struct Node)); /* remove this statement */
data
is a member of struct Node
& that's a integer, for this separately you don't have to allocate memory. you have already allocated for complete structure here
tmp_node = malloc(sizeof(struct Node)); /* this is enough */
Also here
head = newNode(number_1, second);
what is second
? It should be initialized with NULL
like
struct Node *second = NULL;
then only in newNode()
function tmp_node->next
assigned with correct value
tmp_node->next = nextnode; /* now nextnode contains NULL, that's correct */
Or you can make it like below
head = newNode(number_1, NULL);
second = newNode(number_2, head);
third = newNode(number_3, second);
and then while calling printNodes()
pass the third
instead of head
. For e.g
printNodes(third);
Sample code :
struct Node {
int data;
struct Node *next;
};
struct Node *newNode(int number_x, struct Node *nextnode) {
struct Node *tmp_node;
tmp_node = malloc(sizeof(struct Node));
tmp_node->data = number_x;
tmp_node->next = nextnode;
return tmp_node;
}
void printNodes(struct Node *current_node) {
while(current_node != NULL) {
printf("Node is: %d\n", current_node->data);
current_node = current_node->next;
}
}
int main(void) {
int number_1 = 2;
int number_2 = 3;
int number_3 = 4;
struct Node *head = NULL;
struct Node *second = NULL;
struct Node *third = NULL;
head = newNode(number_1, NULL);
second = newNode(number_2, head);
third = newNode(number_3, second);
printNodes(third);
return 0;
}