-
Notifications
You must be signed in to change notification settings - Fork 0
/
marge_two_list.cpp
147 lines (146 loc) · 3.48 KB
/
marge_two_list.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @file marge_two_list.cpp(unsorted)
* @author Abu Bakar Siddik(City UNiversity 53-Batch,Dhaka)
* @date 2021-10-24
*/
// One Way:
#include <stdio.h>
struct node{
int data;
struct node* next;
};
int main()
{
struct node *First,*Second,*Marged,*tmp,*tmp2,*tmp3;
int x;
First = new node;
printf("Enter The Value of First Linked List : ");
scanf("%d",&First->data);
printf("Enter the next value : ");
scanf("%d",&x);
tmp = First;
Marged = First;
tmp3 = Marged;
while(x){
tmp2 = new node;
tmp2->data = x;
tmp3->next = tmp2;
tmp3 = tmp->next;
tmp->next = tmp2;
tmp = tmp->next;
printf("Enter the next value: ");
scanf("%d",&x);
}
tmp->next = NULL;
tmp = First;
printf("First List : ");
while(tmp){
printf("%d ",tmp->data);
tmp =tmp->next;
}
printf("\n");
Second = new node;
printf("Enter The Value of Second Linked List : ");
scanf("%d",&Second->data);
printf("Enter the next value : ");
scanf("%d",&x);
tmp = Second;
while(x){
tmp2 = new node;
tmp2->data = x;
tmp3->next = tmp2;
tmp3 = tmp->next;
tmp->next = tmp2;
tmp = tmp->next;
printf("Enter the next value: ");
scanf("%d",&x);
}
tmp->next = NULL;
tmp3->next = NULL;
tmp = Second;
printf("Second List : ");
while(tmp){
printf("%d ",tmp->data);
tmp =tmp->next;
}
printf("\n");
tmp = Marged;
printf("Marged List : ");
while(tmp){
printf("%d ",tmp->data);
tmp = tmp->next;
}
printf("\n");
return 0;
}
// Another Way:
#include <stdio.h>
struct node{
int data;
struct node* next;
};
int main()
{
struct node *First,*Second,*tmp,*tmp2,*tmp3;
int x;
First = new node;
printf("Enter The Value of First Linked List : ");
scanf("%d",&First->data);
printf("Enter the next value : ");
scanf("%d",&x);
tmp = First;
while(x){
tmp2 = new node;
tmp2->data = x;
tmp->next = tmp2;
tmp = tmp->next;
printf("Enter the next value: ");
scanf("%d",&x);
}
tmp->next = NULL;
Second = new node;
printf("Enter The Value of Second Linked List : ");
scanf("%d",&Second->data);
printf("Enter the next value : ");
scanf("%d",&x);
tmp3 = Second;
while(x){
tmp2 = new node;
tmp2->data = x;
tmp3->next = tmp2;
tmp3 = tmp3->next;
printf("Enter the next value: ");
scanf("%d",&x);
}
tmp3->next = NULL;
tmp = First;
printf("First List : ");
while(tmp){
printf("%d ",tmp->data);
tmp =tmp->next;
}
printf("\n");
tmp3 = Second;
printf("Second List : ");
while(tmp3){
printf("%d ",tmp3->data);
tmp3 =tmp3->next;
}
printf("\n");
// Margining Start From Here..
struct node *help;
help = First;
while(help->next != NULL){ // Just Going to the last node of first list.
help = help->next;
}
help->next = Second;// Then connect the last node of first list with the first node of second list...
// Ends here..
printf("Marged List : ");
tmp2 = First;
while(tmp2){
printf("%d ",tmp2->data);
tmp2 = tmp2->next;
}
printf("\n");
return 0;
}