forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concatenate.c
133 lines (118 loc) · 2.2 KB
/
Concatenate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
}*head1, *head2, *l1, *l2, *last;
void createlist1(int n);
void createlist2(int m);
void display();
void concat();
int main()
{
int n;
printf("Enter the number of nodes for first list\n");
scanf("%d",&n);
createlist1(n);
printf("Enter the number of nodes for second list\n");
scanf("%d",&n);
createlist2(n);
display();
concat();
return 0;
}
void createlist1(int n)
{
struct node *new;
l1=(struct node*)malloc(sizeof(struct node));
if(l1==NULL)
printf("MEMORY NOT ALLOCATED\n");
else
{
printf("Enter the data for 1st node: ");
scanf("%d",&l1->data);
l1->next=NULL;
l1->prev=NULL;
head1=l1;
for(int i=0;i<n-1;i++)
{
new=(struct node*)malloc(sizeof(struct node));
if(new==NULL)
printf("MEMORY NOT ALLOCATED\n");
else
{
printf("Enter the data: ");
scanf("%d",&new->data);
new->prev=l1;
l1->next=new;
l1=l1->next;
}
}
last=l1;
}
}
void createlist2(int m)
{
struct node *new;
l2=(struct node*)malloc(sizeof(struct node));
if(l2==NULL)
printf("MEMORY NOT ALLOCATED\n");
else
{
printf("Enter the data for 1st node: ");
scanf("%d",&l2->data);
l2->next=NULL;
l2->prev=NULL;
head2=l2;
for(int i=0;i<m-1;i++)
{
new=(struct node*)malloc(sizeof(struct node));
if(new==NULL)
printf("MEMORY NOT ALLOCATED\n");
else
{
printf("Enter the data: ");
scanf("%d",&new->data);
new->prev=l2;
l2->next=new;
l2=l2->next;
}
}
}
}
void concat()
{
struct node *ptr, *tmp;
ptr=head1;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next= head2;
ptr->prev=last;
tmp=head1;
printf("The concatenated list:\n");
while(tmp!=NULL)
{
printf("%d\n",tmp->data);
tmp=tmp->next;
}
}
void display()
{
struct node *tmp, *temp;
tmp=head1;
printf("\nList 1:\n");
while(tmp!=NULL)
{
printf("%d\n",tmp->data);
tmp=tmp->next;
}
printf("\nList 2:\n");
temp=head2;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}