Friday, 22 June 2012

write a c program to count all non-zero element,odd element and even element in singly linked list


/*write a c program to count all non-zero element,odd element
and even element in singly linked list*/
#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 display();
void main()
{
clrscr();
linked_list();
display();
getch();
}
void linked_list()
{
char ch='y';
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;
}
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;
}
printf("\ndo you want to enter data in the linked list(Y|N):-");
scanf("%s",&ch);
}
}
void display()
{
int nzero=0,odd=0,even=0;
struct node *t=start;
while(t!=NULL)
{
if(t->data!=0)
nzero++;
if(t->data%2==0)
even++;
else
odd++;
t=t->next;
}
printf("\nnon zero element are:-%d",nzero);
printf("\nodd elements are:-%d",odd);
printf("\neven elements are:-%d",even);
}
/*
enter the number for linked list:-6                                            
                                                                               
do you want to enter data in the linked list(Y|N):-y                          
                                                                               
enter the element for linked list:-7                                          
                                                                               
do you want to enter data in the linked list(Y|N):-y
                                                                               
enter the element for linked list:-9                                          
                                                                               
do you want to enter data in the linked list(Y|N):-y                          
                                                                               
enter the element for linked list:-2                                          
                                                                               
do you want to enter data in the linked list(Y|N):-n                          
                                                                               
non zero element are:-4                                                        
odd elements are:-2
even elements are:-2
*/

No comments: