/*write a c program to create two linked list and concatenate one list
at the end of another*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start1=NULL,*start2=NULL,*temp1=NULL,*temp2=NULL;
void linked_list1();
void linked_list2();
void concate();
void display();
void main()
{
clrscr();
linked_list1();
linked_list2();
concate();
display();
getch();
}
void linked_list1()
{
char ch='y';
while(ch=='y'||ch=='Y')
{
if(start1==NULL)
{
start1=malloc(sizeof(struct node));
printf("\nenter the number for first linked list:-");
scanf("%d",&start1->data);
start1->next=NULL;
temp1=start1;
}
else
{
temp1->next=malloc(sizeof(struct node));
printf("\nenter the element for first linked list:-");
scanf("%d",&temp1->next->data);
temp1->next->next=NULL;
temp1=temp1->next;
}
printf("\ndo you want to enter data in first linked list(Y|N):-");
scanf("%s",&ch);
}
}
void linked_list2()
{
char ch='y';
while(ch=='y'||ch=='Y')
{
if(start2==NULL)
{
start2=malloc(sizeof(struct node));
printf("\nenter the number for second lisked list:-");
scanf("%d",&start2->data);
start2->next=NULL;
temp2=start2;
}
else
{
temp2->next=malloc(sizeof(struct node));
printf("\nenter the element for second linked list:-");
scanf("%d",&temp2->next->data);
temp2->next->next=NULL;
temp2=temp2->next;
}
printf("\ndo you want to enter data in the second linked list(Y|N):-");
scanf("%s",&ch);
}
}
void concate()
{
struct node *t2=start2;
while(t2!=NULL)
{
temp1->next=malloc(sizeof(struct node));
temp1->next->data=t2->data;
temp1->next->next=NULL;
temp1=temp1->next;
t2=t2->next;
}
}
void display()
{
struct node *t=start1;
printf("\n\nthe concatenation of two linked list is:-\n");
while(t!=NULL)
{
printf("\n%d",t->data);
t=t->next;
}
}
/*
enter the number for first linked list:-1
do you want to enter data in first linked list(Y|N):-y
enter the element for first linked list:-7
do you want to enter data in first linked list(Y|N):-n
enter the number for second lisked list:-6
do you want to enter data in the second linked list(Y|N):-y
enter the element for second linked list:-9
do you want to enter data in the second linked list(Y|N):-y
enter the element for second linked list:-7
do you want to enter data in the second linked list(Y|N):-n
the concatenation of two linked list is:-
1
7
6
9
7
*/
No comments:
Post a Comment