20)Write a C program to read an interger two create a two list to all positive number are in one list and negative number are in another order.


#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *createposneg(NODE *,int);
void display(NODE *);
NODE *findlast(NODE *);
NODE *alloc(int);
void main()
{
NODE *list1=NULL,*list2=NULL;
int i,n,val;
clrscr();
printf(“\nEnter the number of elements .”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the %d element: “,i);
scanf(“%d”,&val);
if(val>0)
list1=createposneg(list1,val);
else if(val<0)
list2=createposneg(list2,val);
}
printf(“\npositive Linked list : “);
if(list1==NULL)
printf(“\nNO positive integers entered.”);
else
display(list1);
printf(“\nNegative Linked list : “);
if(list2==NULL)
printf(“\nNO positive integers entered.”);
else
display(list2);
getch();
}
void display(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf(“%d->”,ptr->data);
printf(“NULL”);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
NODE *alloc(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=NULL;
return temp;
}
NODE *createposneg(NODE *list,int val)
{
NODE *ptr,*temp,*prev;
temp=alloc(val);
if(list==NULL)
{
list=temp;
}
else
{
for(ptr=list,prev=list;ptr!=NULL && temp->data>ptr->data;prev=ptr,ptr=ptr->next);
if(ptr==list && prev==list)
{
temp->next=list;
list=temp;
}
else
{
temp->next=prev->next;
prev->next=temp;
}
}
return(list);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s