My code prints an extra node(garbage value) why? Is there is any issue with my code? just let me how to fix it.
void push(node **head_ref,int value) //function to insert a new node on front of the list
{
node *new_node=(node*)malloc(sizeof(node));
new_node->data=value;
new_node->next=*head_ref;
*head_ref=new_node;
}
void printll(node *head) //function to print the list
{
node *temp = head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
Actual Output :
45 88 24 34 77 0
Expected Output :
45 88 24 34 77
Full code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cassert>
using namespace std;
struct node
{
int data;
node *next;
};
void push(node **head_ref,int value)
{
node *new_node=(node*)malloc(sizeof(node));
new_node->data=value;
new_node->next=*head_ref;
*head_ref=new_node;
}
void printll(node *head)
{
node *temp = head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int main()
{
node *head= (node*)malloc(sizeof(node));
push(&head,77);
push(&head,34);
push(&head,24);
push(&head,88);
push(&head,45);
printll(head);
printf("\n");
return 0;
}
When you allocate memory using malloc
the memory is not initialized in any way, it's content is indeterminate . That means that when you allocate the first node (which is a dummy extra node that's not needed) it's next
pointer is not null, and dereferencing this indeterminate pointer leads to undefined behavior .
The simplest solution? Considering your code is closer to C than C++, don't allocate memory initially at all, instead just create a pointer and initialize it to NULL
:
node *head = NULL;
One proper way to do it in C++ is to not use malloc
at all, instead use the C++ operator new
and add a constructor to the node
structure which initializes it:
struct node
{
node() : data(0), next(nullptr) {}
node(int d, node* n) : data(d), next(n) {}
int data;
node* next;
};
void push(node** head_ref, int value)
{
*head_ref = new node(value, *head_ref);
}
...
int main()
{
node* head = nullptr;
...
}
Now you can create a new node and it will have an initial value
of 0
, and its next
pointer will be a null-pointer. You can also, as shown above, create and initialize a new node with specific value
and next
.
[Substitute nullptr
for 0
if your compiler doesn't support the C++11 nullptr
value]
Instead of this definition
node *head= (node*)malloc(sizeof(node));
you should write simply
node *head = NULL;
or
node *head = nullptr; // C++
Otherwise your program has undefined behaviour because the allocated node for the head was not initialized.
Also if it is a C++ program you should use operator new
instead of C function malloc
. For example function push
will look like
void push( node * &head_ref, int value )
{
head_ref = new node { value, head_ref };
}
and called like
push( head, 77 );
Take into account that you have also to write a function that will free the all allocated memory for the list.
You have a dummy node (head itself) in your design. So the print function needs to skip that dummy node.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.