Example:
Linked list is:3->4->1->9
Length is : 4
program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *first;
void create()
{
struct node *ptr,*cpt;
char ch;
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter Numbern");
scanf("%d",&cpt->data);
first=cpt;
do
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter Numbern");
scanf("%d",&ptr->data);
cpt->next=ptr;
cpt=ptr;
printf("Continue(y/n)?n");
ch=getch();
}while(ch=='y');
cpt->next=first;
}
void traverse()
{
struct node *ptr=first;
printf("Linklist is :n");
while(ptr->next!=first)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
void length()
{
int len=1;
struct node *ptr=first;
while(ptr->next!=first){
ptr=ptr->next;
len++;
}
printf("Length is %d",len);
}
void main()
{
clrscr();
create();
traverse();
length();
getch();
}
Enter Number
2
Enter Number
4
Continue(y/n)?
Enter Number
1
Continue(y/n) ?
Enter Number
9
Continue(y/n) ?
Enter Number
0
Continue(y/n) ?
Linklist is :
2 4 1 9 0
Length is 5