Friday, January 9, 2015

Merge two Linked List in DSA




/*Write a program to attach two singly linked lists
*/

//INCLUDE HEADER FILES
#include<stdio.h>
#include<conio.h>

struct node//declaration of node structure
{
int data;//DATA FIELD
struct node *next;
};

struct list//declaration of list structure
{
struct node *head;
};


/*initilisation of the list*/
void init(struct list *l)
{
l->head=NULL;
}


/*create the new node*/
int create(struct node *p, int data)
{
if(p==NULL)//IF THE LIST IS EMPTY
return 1;

else
{
             /*ADJUST THE POINTERS*/
p->data=data;
p->next=NULL;
return 0;
}
}


/*for inserting the node*/
int insert(struct list *l,int data)
{
struct node *p,*q;
int x;

p=(struct node *)malloc(sizeof(struct node));//allocate memory to the new node

x=create(p,data);//call create function to create the new node

if(x)//if newnode couldn't be created
return x;


else if(l->head==NULL)//if the list is already empty
l->head=p;

else
{
q=l->head;

while(q->next!=NULL)//TILL THE END
q=q->next;

q->next=p;
}
return 0;
}

/*displays the list*/
int display(struct list *l)
{
struct node *q=l->head;

if(q==NULL)//if the list is empty
return 1;

else
{
printf("\n");

while(q!=NULL)//till the end
{
printf(" %d-> ",q->data);
q=q->next;
}
printf("NULL");
return 0;
}
}


/*attaches the second list to the first*/
int attach(struct list *l1,struct list *l2)
{
struct node *p;

p=l1->head;//p now points to first node

if((l1->head==NULL)&&(l2->head==NULL))
return 1;

else if(l1->head==NULL)
return 2;

else if(l2->head==NULL)
return 3;

else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=l2->head;
return 0;
}
}


//main function
int main()
{
int data,x,y,pos,ch;
struct list l1,l2;

      /*declare the head pointers of two lists */
l1.head=NULL;
l2.head=NULL;

clrscr();//clears the user screen

do
{
printf("\n Enter the data in the first linked list(-1 to stop) : ");
scanf("%d",&data);

if(data==-1)
break;

x=insert(&l1,data);//function call

if(x)
printf("\n Node can not be inserted ");

}while(data!=-1);

printf("\n The first linked list is: ");

x=display(&l1);//function call

if(x)
printf("\n List is empty ");

do
{
printf("\n Enter the data in the second linked list(-1 to stop) : ");
scanf("%d",&data);

if(data==-1)
break;


x=insert(&l2,data);//function call

if(x)
printf("\n Node can not be inserted ");

}while(data!=-1);

printf("\n The second linked list is: ");

x=display(&l2);//function call

if(x)
printf("\n List is empty ");


y=attach(&l1,&l2);//function call[attaches list]

if(y==1)
printf("\n Lists are empty ");

else if(y==2)
{
printf("\n First list is empty ");
printf("\n The list after attachment is : ");
display(&l2);
}

else if(y==3)
{
printf("\n Second list is empty ");
printf("\n The list after attachment is : ");
display(&l1);
}

else
{
printf("\n The attached linked list is : ");
display(&l1);
}

     /*free the memory occcupied by the linked lists*/
free(l1);
free(l2);

getch();
return 0;
}


/*
  OUTPUT

 Enter the data in the first linked list(-1 to stop) : 1

 Enter the data in the first linked list(-1 to stop) : 2

 Enter the data in the first linked list(-1 to stop) : 3

 Enter the data in the first linked list(-1 to stop) : -1

 The first linked list is:
 1->  2->  3-> NULL
 Enter the data in the second linked list(-1 to stop) : 4

 Enter the data in the second linked list(-1 to stop) : 5

 Enter the data in the second linked list(-1 to stop) : -1

 The second linked list is:
 4->  5-> NULL
 The attached linked list is :
 1->  2->  3->  4->  5-> NULL
*/





for more codes you Visit http://codesofprogramming.blogspot.in/

No comments:

Post a Comment