Sie sind auf Seite 1von 4

#include<stdio.

h>
#include<conio.h>
typedef struct list
{
int data;
struct list * next;
}list;
void init(list **h)
{
*h = NULL;
}
void create(list **h)//sorts and creates node
{
list *temp, *i;
temp = (list *) malloc(sizeof(list));
if(*h == NULL)//to make temp the first node
{
if(temp != NULL) // malloc is successfull
{
printf("\nEnter Data For First Node ");
scanf("%d", &temp->data);
temp->next = temp;
*h = temp;
}
else
printf("\nMemory Allocation Failed ");
}
else //to make temp successor nodes
{
if(temp != NULL)
{
printf("\nEnter Value For Node ");
scanf("%d", &temp->data);
i = *h;
if(temp->data < (*h)->data)// when new node is less than
first node
{
temp->next = *h;
*h = temp;
//traverse to last node of list so
//that we change its next to new head node
//and list remains circular
for(i = (*h)->next; i->next!= (*h)->next; i = i-
>next);
i->next = *h;
}
else// when new node is greater than or equal to first n
ode
{
for(i = *h; temp->data >= i->next->data && i-
>next!=*h; i= i->next);
if(i->next == *h ) //add at end
{
i->next = temp;
temp->next = *h;
}
else
{
temp->next = i->next ;
i->next = temp;
}
}
}
else
printf("\nMemory Allocation Failed ");
}
}
void disp(list *h)
{
list *temp;
temp = h;
do
{
printf("\n%d", temp->data);
temp = temp->next;
}while(temp != h);
}
void freelist(list **head)
{
list *temp, *temp1 = *head;
do
{
temp = *head;
*head = (*head)->next;
free(temp);
}while(*head != temp1);
*head = NULL;
}

list * uni(list *l1, list *l2 )


{
list *temp1, *temp2, *l3, *node, *temp3;
int flag = 1;
init(&l3);
// copy l1 to l3
temp1 = l1;
do
{
node = (list *) malloc(sizeof(list));
if(node != NULL)
{
node->data = temp1->data;
if(l3 == NULL) //first node
{
l3 = node;
node->next = l3;
}
else // successor nodes
{
for(temp2 = l3;temp2->next!=l3; temp2 = temp2->n
ext);
temp2->next = node;
node->next = l3;
}
temp1 = temp1->next;
}
else
{
printf("\nMemory Allocation Failed ");
break;
}
}while(temp1 != l1);
//finished copying list1
//checking and then copying list2 to list3
temp2 = l2;
do
{
temp1 = l1;
flag = 1;
do
{
if(temp1->data == temp2->data)
{
flag= 0;
break;
}
temp1 = temp1->next;
}while(temp1!= l1 && temp1->data <= temp2->data);
if(flag == 1)
{
node = (list *) malloc(sizeof(list));
if(node !=NULL)
{
node->data = temp2->data;
for(temp3 = l3;temp3->next!=l3; temp3 = temp3->n
ext);
temp3->next = node;
node->next = l3;
}
else
{
printf("\nAllocation Failed") ;
break;
}
}
temp2 = temp2->next;
}while(temp2 != l2);
return l3;
}

void main()
{
int ch;
list *head1, *head2, *head3;
init(&head1);
init(&head2);
init(&head3);
clrscr();
do
{
printf("\nTo Create list1:1");
printf("\nTo display list1 :2");
printf("\nTo Create list2:3");
printf("\nTo display list2 :4");
printf("\nUnion of list1 and list2 : 5");
printf("\nTo display list3 :6");
printf("\nTo Exit :7\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create(&head1);
break;
case 2:
disp(head1);
break;
case 3:
create(&head2);
break;
case 4:
disp(head2);
break;
case 5:
freelist(&head3);
head3 = uni(head1,head2);
disp(head3);
}
}while(ch!=6);
}

Das könnte Ihnen auch gefallen