Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* head1=NULL;
struct node* head2=NULL;
void traverse(struct node** head);
struct node* cn()
{
struct node * n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
void createnode(int arr[],int n)
{
int i;
struct node * newnode1,*temp1,*newnode2,*temp2;
for(i=0;i<n;i++)
{
if(arr[i]>1)
{
if(head1==NULL)
{
newnode1=cn();
newnode1->data=arr[i];
newnode1->next=NULL;
head1=newnode1;
temp1=head1;
}
else
{
newnode1=cn();
newnode1->data=arr[i];
newnode1->next=NULL;
temp1->next=newnode1;
temp1=newnode1;
}
}
else
{
if(head2==NULL)
{
newnode2=cn();
newnode2->data=arr[i];
newnode2->next=NULL;
head2=newnode2;
temp2=head2;
}
else
{
newnode2=cn();
newnode2->data=arr[i];
newnode2->next=NULL;
temp2->next=newnode2;
temp2=newnode2;
}
}
}
}
void traverse(struct node** head) //Function to traverse Linked list.
{
struct node * temp3;
temp3=*head;
while(temp3!=NULL)
{
printf("%d\n",temp3->data);
temp3=temp3->next;
}
}
int main()
{
int arr[100],i,n;
printf("Enter number of elements.\n");
scanf("%d",&n);
printf("Enter non-zero elements for Linked list.\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
createnode(arr,n);
printf("List 1(positive elements)\n");
traverse(&head1);
printf("List 2(negative elements)\n");
traverse(&head2);
}
0 Comments