-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAnswer27.cpp
70 lines (62 loc) · 854 Bytes
/
Answer27.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
//Q27) Merge two sorted Linked List.
// only the functional implementation
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
}*first=NULL,*second=NULL,*third=NULL;
void Display(struct Node *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void Merge(struct Node *p,struct Node *q)
{
struct Node *last;
if(p->data < q->data)
{
third=last=p;
p=p->next;
third->next=NULL;
}
else
{
third=last=q;
q=q->next;
third->next=NULL;
}
while(p && q)
{
if(p->data < q->data)
{
last->next=p;
last=p;
p=p->next;
last->next=NULL;
}
else
{
last->next=q;
last=q;
q=q->next;
last->next=NULL;
}
}
if(p)last->next=p;
if(q)last->next=q;
}
int main()
{
int A[]={10,20,40,50,60};
int B[]={15,18,25,30,55};
create(A,5);
create2(B,5);
Merge(frist,second);
Display(third);
return 0;
}