Friday, 22 June 2012

write a c program to create a singly linked list and count the total number of node in it


/*write a c program to create a singly linked list and count
the total number of node in it*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL,*temp=NULL;
void linked_list();
void main()
{
clrscr();
linked_list();
getch();
}
void linked_list()
{
char ch='y';
int count=0;
while(ch=='y'||ch=='Y')
{
if(start==NULL)
{
start=malloc(sizeof(struct node));
printf("\nenter the number for linked list:-");
scanf("%d",&start->data);
start->next=NULL;
temp=start;
count++;
}
else
{
temp->next=malloc(sizeof(struct node));
printf("\nenter the element for linked list:-");
scanf("%d",&temp->next->data);
temp->next->next=NULL;
temp=temp->next;
count++;
}
printf("\ndo you want to enter data in the linked list(Y|N):-");
scanf("%s",&ch);
}
printf("\nthe total number of node in singly linked list are:-%d",count);
}
/*
enter the number for linked list:-8

do you want to enter data in the linked list(Y|N):-y

enter the element for linked list:-8

do you want to enter data in the linked list(Y|N):-y

enter the element for linked list:-3

do you want to enter data in the linked list(Y|N):-n

the total number of node in singly linked list are:-3
*/

No comments: