#include<stdio.h>
typedef struct node
{
int data;
struct node * link;
}n;
void addatbeg(n *);
void display(n *);
n* first=NULL;
int main()
{
int ch;
printf("%u",first);
while(1)
{
printf("\n enter 0 to terminate else any other key");
scanf("%d",&ch);
if(ch==0)
break;
addatbeg(first);
}
}
void addatbeg(n* first)
{
int ele;
n *new;
printf("enter the element");
scanf("%d",&ele);
new=(n*)malloc(sizeof(n));
new->data=ele;
printf("\nddd%u\n",first);
if(first==NULL)
{
new->link=NULL;
}
else
new->link=first;
printf("\nnew=%u\n",new);
first=new;
printf("\n first=%u\n",first);
printf("%u",first);
printf("\n%u",new->link);
printf("\n before function is called first=%u",first);
display(first);
}
void display(n* temp)
{
printf("\n inside function,first=%u",first);
while(1)
{
printf("\ne");
printf("\n%d->",temp->data);
if(temp->link==NULL)
break;
temp=temp->link;
}
}
i am trying to implement a linked list with a function to ADD ELEMENTS AT THE BEGINNING.
i have used a variable,first that holds the first node of the linked list.
but when i pass it to the display function,its value becomes NULL without doing anything. so what is the mistake??
WHY IS THE VALUE OF THE VARIABLE first getting modified??