Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node* head=NULL;
struct node *cn()
{
struct node* n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
void createnode() //Function to create Linked list.
{
int i,n,x;
struct node * temp,*newnode;
printf("Enter size of linked list :\n");
scanf("%d",&n);
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
head=cn();
head->data=x;
head->next=NULL;
temp=head;
}
else
{
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
}
}
void count()
{
struct node *temp;
temp=head;
int i=0,even=0,odd=0;
while(temp!=NULL)
{
if(temp->data!=0)
{
i++;
}
if(temp->data%2==0 && temp->data!=0)
{
even++;
}
else if (temp->data%2==1)
{
odd++;
}
temp=temp->next;
}
printf("Number of non zero elements: %d\n",i);
printf("Number of even elements: %d\n",even);
printf("Number of odd elements: %d\n",odd);
}
void display()
{
struct node * temp;
temp=head;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
while(temp->next!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}
void main()
{
createnode();
printf("Linked list.\n");
display();
count();
}
0 Comments