-
Notifications
You must be signed in to change notification settings - Fork 0
/
week6-9.c
67 lines (59 loc) · 1.26 KB
/
week6-9.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
}*head;
struct node * newnode(int value){
struct node * temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}
void InsertAtBeg(struct node ** head,int x){
struct node * temp=newnode(x);
temp->next=*head;
*head=temp;
}
void print(struct node * root){
while(root!=NULL){
printf("%d ",root->data);
root=root->next;
}
}
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
int main(){
head=NULL;
struct node * res=NULL;
struct node * a=NULL;
struct node * b=NULL;
InsertAtBeg(&a,15);
InsertAtBeg(&a,10);
InsertAtBeg(&a,5);
InsertAtBeg(&b,20);
InsertAtBeg(&b,3);
InsertAtBeg(&b,2);
res=SortedMerge(a,b);
print(res);
return 0;
}