-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgLL2merge.cpp
More file actions
126 lines (120 loc) · 2.15 KB
/
sgLL2merge.cpp
File metadata and controls
126 lines (120 loc) · 2.15 KB
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
//merging LL of even and odd into one LL
#include<iostream>
#define null 0
using namespace std;
struct NatNo
{
int data;
NatNo *next;
};
NatNo *first,*temp,*ttemp,*p,*q,*r,*ptemp,*pttemp,
*ntemp,*nttemp,*efirst=null,*ofirst=null,*etemp=null,*otemp=null;
void init()
{
first=temp=ttemp=efirst=ofirst=null;
}
void createEfirst(int val)
{
efirst=new NatNo;
efirst->data=val;
efirst->next=null;
}
void createOfirst(int val)
{
ofirst=new NatNo;
ofirst->data=val;
ofirst->next=null;
}
void addnode(NatNo* head,int val)
{
temp=head;
while(temp->next!=null)
{
temp=temp->next;
}
ttemp=new NatNo;
ttemp->data=val;
ttemp->next=null;
temp->next=ttemp;
}
void disp(NatNo* head)
{
temp=head;
while(temp!=null)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void mergedLL()
{
etemp=efirst;
otemp=ofirst;
//first=null;
//temp=null;
while(etemp!=null&&otemp!=null)
{
ttemp=new NatNo;
if(etemp->data<otemp->data)
{
ttemp->data =etemp->data;
etemp=etemp->next;
}
else
{
ttemp->data=otemp->data;
otemp=otemp->next;
}
ttemp->next=null;
if(first==null)
{
first=ttemp;
temp=first;
}
else
{
temp->next=ttemp;
temp=ttemp;
}
}
//add remaining even
while(etemp!=null)
{
ttemp= new NatNo;
ttemp->data=etemp->data;
ttemp->next=null;
temp->next=ttemp;
temp=ttemp;
etemp=etemp->next;
}
//add rwmaining odd
while(otemp!=null)
{
ttemp=new NatNo;
ttemp->data=otemp->data;
ttemp->next=null;
temp->next=ttemp;
temp=ttemp;
otemp=otemp->next;
}
}
int main()
{
init();
createEfirst(2);
addnode(efirst,4);
addnode(efirst,6);
addnode(efirst,8);
cout<<"even LL: \n";
disp(efirst);
cout<<"odd LL: \n";
createOfirst(1);
addnode(ofirst,3);
addnode(ofirst,5);
addnode(ofirst,7);
addnode(ofirst,9);
disp(ofirst);
cout<<"merged LL: \n";
mergedLL();
disp(first);
}