F How To Count The Number Of Nodes Having +Ve And –Ve Integers In a Single Link List | CodeTheta

How To Count The Number Of Nodes Having +Ve And –Ve Integers In a Single Link List

May 10, 2014

In this program you can see that we can find out the positive and negative integers in a single link list it is a part of data structure programming. Program uses malloc function to create memory allocation.read the code and implement in to your own machine.


/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct link
{
int data;
struct link *next;
} node;
node *ins_beg(node*, int);
void count_nodes(node *h);
void display(node*);
void main()
{
int x, ch;
node *head=NULL;
while(1)
{
printf("/* http://native-code.blogspot.com */");
printf("\n1-->Insert at first\n2-->Display\n3-->Count\n4-->EXIT\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter data of the node:\n");
scanf("%d", &x);
head=ins_beg(head, x);
break;
case 2:
display(head);
printf("\n");
break;
case 3:
count_nodes(head);
break;
case 4:
default:
printf("Wrong choice.\n");
}
}
getch();
}
node *ins_beg(node *h, int x)
{
node *temp, *r=h;
temp=(node*) malloc (sizeof(node));
temp->data=x;
temp->next=NULL;
if(h==NULL)
{
h=temp;
}
else
{
temp->next=h;
h=temp;
}
return(h);
}
void display( node *h)
{
node *r=h;
while(r!=NULL)
{
printf("%d-->", r->data);
r=r->next;
}
printf("NULL");
}
void count_nodes(node *h)
{
int pos,neg;
node *p=h;
pos=neg=0;
while(p!=NULL)
{
if(p->data<0)
{
neg+=1;
}
else
{
pos+=1;
}
p=p->next;
}
printf("\nPositive node count:%d", pos);
printf("\nNegative node count:%d", neg);
}

/* http://native-code.blogspot.com */

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment