Write a C Program to find the number of data in a linked list



Sumit Kar, Sorting, C, Timus Rak, C Program



#include <stdio.h>
#define newnode (struct node*)malloc(sizeof(struct node))

typedef struct node
{
int data;
struct node *next;
} node;

node *create_list();

void main()
{
node *f;
int len;
f = NULL;

f = create_list();
len = find_len(f);
printf("\n length = %d",len);
} // main

node *create_list()
{
node *f,*c,*p;
int tdata;
f = NULL;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&tdata);
while( tdata != 0 )
{
c = newnode;
if( c == NULL)
{
printf("\n Insuf. mem. ");
exit(0);
}
c->data = tdata;
c->next = NULL;
if( f== NULL)
f = c;
else
p->next = c;
p = c;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&tdata);
} //while
return(f);
} // create list

int find_len(node *f)
{
int len=0;
node *t;
if( f == NULL)
return(0);
t = f;
while( t != NULL )
{
len++;
t = t->next;
}
return(len);
}

/*
Summary: Linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list. After creating the linked list ,the length of linked list is calculated.
*/


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C