Here is the snippet which works fine.
My question now is the function append does not work with void return type. It works fine with a dummy return variable of an int. Any clue why is this happening?
Code:
/* Porgram appends a node at the end of the linked list*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
int append(struct node *,int);
int main(int argc, char **argv)
{
struct node *p, *head, *curr, *temp;
int num,i;
head = (struct node*)malloc(sizeof(struct node));
curr = head;
for(i=1;i<=5;i++)
{
curr ->link = (struct node*)malloc(sizeof(struct node));
curr ->data= i;
temp=curr; // temp is required becoz curr will point to 6th member and 5th element pointer should be pointing to NULL
curr = curr->link;
}
temp->link =NULL;
//Display existing linked list
printf("Existing List is:");
curr = head;
while(curr->link != NULL)
{
printf("\n %d", curr->data);
curr = curr->link;
}
printf("\n %d", curr->data);
printf("\n Enter num to be appended:");
scanf("%d", &num);
i = append(head,num);
//Display list
curr = head;
printf("\n Appended list:");
while(curr->link!=NULL)
{
printf("\n %d", curr->data);
curr = curr->link;
}
printf("\n %d", curr->data);
//printf("\n hello world\n");
return 0;
}
int append(struct node *head, int num)
{
struct node *temp,*q ;
temp = head;
do{
temp = temp->link;
}while(temp->link!=NULL);
//printf("\n temp->data = %d \n temp->link =%d",temp->data, temp->link);
q = (struct node *)malloc(sizeof(struct node));
q->data = num;
q->link = NULL;
temp->link = q;
return(1);
}