#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}
NODE;
NODE * get_node()
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&p->data);
p->link = NULL;
return p;
}
NODE * create_sll()
{
int n,i;
NODE *first,*last,*q;
printf("Enter no.of nodes:");
scanf("%d",&n);
first = last = NULL;
for(i=1;i<=n;i++)
{
q = get_node();
if(first==NULL)
first = q;
else
last->link = q;
last = q;
}
return first;
}
void display(NODE *h)
{
while(h!=NULL)
{
printf("%4d",h->data);
if(h->link->link!=NULL)
h=h->link->link;
else
h=NULL;
}
printf("\n");
}
void main()
{
NODE *h=NULL;
h = create_sll();
display(h);
getch();
}
Output-
Enter no.of nodes: 4
Enter data:4
Enter data:5
Enter data:6
Enter data:7
4 6
/