#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);
h=h->link;
}
printf("\n");
}
void main()
{
NODE *start,*p,*q;
clrscr();
start = create_sll();
display(start);
if(start!=NULL && start->link!=NULL)
{
p = q = start;
while(p->link!=NULL)
{
q = p;
p = p->link;
}
q->link = NULL;
p->link = start;
start = p;
}
display(start);
getch();
}
Output-
Enter no.of nodes: 3
Enter data:2
Enter data:3
Enter data:4
2 3 4
4 2 3
/