Hi!
I am trying to create a Linked List and inserting some numbers and displaying those numbers through display function.
But I am not able to get the appropriate result.
Hope someone will help.
thanks !.
Anil
Code which I am using.
Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
}*temp;
void insert(struct node **p, int x)
{
temp=*p;
if(temp!=NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
*p=temp;
}
else
{
while(temp==NULL)
{
temp=temp->next;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
}
return;
}
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}
void main()
{
temp=NULL;
clrscr();
insert(&temp,10);
insert(&temp,20);
insert(&temp,30);
insert(&temp,40);
display(temp);
getch();
}