#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define newnode (struct node*) malloc(sizeof(struct node))
struct node
{
int data;
struct node *next;
};
struct node *create();
int main()
{
struct node *f;
int len;
f = NULL;
clrscr();
f = create();
len = calc_len(f);
printf("\n Length = %d",len);
getch();
return 0;
}
struct node *create()
{
struct node *f,*c,*p;
int data1;
f = NULL;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&data1);
while( data1 != 0 )
{
c = newnode;
if( c == NULL)
{
printf("\n Insuf. mem. ");
exit(0);
}
c->data = data1;
c->next = NULL;
if( f== NULL)
f = c;
else
p->next = c;
p = c;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&data1);
}
return(f);
}
int calc_len(struct node *f)
{
int len=0;
struct node *t;
if( f == NULL)
return(0);
t = f;
while( t != NULL )
{
len++;
t = t->next;
}
printf("Length = %d\n",len);
return(len);
}
Saturday, 28 April 2012
C Program to Create a Linked List and Find its Length
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment