I've been studying linked lists in C and regarding the append function, I came across the following code:
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL){
right=right->next;
}
right->next =temp;
right=temp;
right->next=NULL;
}
In order to save a line of code, wouldn't it be possible to just add NULL to the temp's next atribute? like so:
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
temp -> next = NULL;
right=(struct node *)head;
while(right->next != NULL){
right=right->next;
}
right->next =temp;
}