Reputation: 2184
what is wrong with this code? This is simply creating a link list and printing it.
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *next;
int data;
};
void printLinklist(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
printf("\n");
}
void pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
temp->next = head;
temp->data = val;
head = temp;
}
int main()
{
struct node *head = NULL;
pushData(head, 1);
pushData(head, 2);
pushData(head, 3);
pushData(head, 4);
pushData(head, 5);
printLinklist(head);
}
I know correct way of doing it. For that we will have to pass &head in pushData() function and define argument of this function as double pointer (replace head to *head in pushData() function and passing &head as an argument)
I can't understand the problem in this code.
Upvotes: 0
Views: 34
Reputation: 1
The True code is:
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
struct node {
struct node* next;
int data;
};
void printLinklist(struct node* head)
{
struct node* temp = head;
while (temp->next != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
struct node* pushData(struct node* head, int val)
{
struct node* temp = malloc(sizeof(struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
int main()
{
struct node* head = malloc(sizeof(struct node));
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
struct node* head2 = head;
for (int i = 0; i < 5; i++)
{
head2 = head2->next;
}
head2->next = NULL;
printLinklist(head);
}
Upvotes: 0
Reputation: 11453
Arguments are always passed by value
. When calling pushData()
, the value of head
, that is the address stored in the pointer variable is copied into the local variable head
of the function. Changing this won't change the head
variable in the main()
function.
If you need that to happen, you have to pass &head
and then assign value by dereferencing it in the pushData()
function.
Upvotes: 3
Reputation: 53016
The problem is that you assign NULL
to head
in main()
, and that never chagnes, it could work like this
struct node *pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
and in main
int main()
{
struct node *head;
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
printLinklist(head);
}
passing the pointer only modifies the data the pointer points to, but not the pointer itself.
So in your pushData()
function, you do this
head = temp;
which changes the local pointer, and makes it pointer to temp, but once the function returns, the change is lost.
When you pass the address of head
like &head
, and then you can alter the pointer because your head
pointer will be pointing to the pointer itself instead of the data the pointer points to.
Upvotes: 1