typedef struct Node *ptrToNode;
typedef ptrToNode List;
typedef ptrToNode Position;
struct Node
{
int Element;
Position Next;
};
void InsertFirst(int x,List L)
{
Position NewNode;
NewNode=malloc(sizeof(struct Node));
if(NewNode==NULL)
{
printf("Out of Space");
}
else
{
NewNode->Element=x;
NewNode->Next=L->Next;
L->Next=NewNode;
}
}
void display(List S){
while(S!=NULL)
{
printf("%d\t",S->Element);
S=S->Next;
}
}
int main()
{
List P;
int ch;
P=(List)malloc(sizeof(struct Node));
InsertFirst(10,P);
InsertFirst(20,P);
InsertFirst(30,P);
display(P);
return 0;
}
Can someone what is wrong with this code? After entering the elements in the linked list. In display part it is starting from the last element and throwing garbage values