forked from R0HITKUMAR/webDevUsingGSheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedListUnion.c
85 lines (80 loc) · 1.74 KB
/
linkedListUnion.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
#include<stdio.h>
#include<stdlib.h>
#include "linkedList.h"
AscendingInsertion(struct node **START,int x)
{
struct node *p,*q;
p=(*START);
q=NULL;
while (p!=NULL && x >= p->info)
{
q=p;
p=p->next;
}
if (q==NULL)
InsBeg(&(*START),x);
else
InsAft(&(*START),q->info,x);
}
Union(struct node **LIST1,struct node **LIST2)
{
struct node *LIST3=NULL;
struct node *p,*q;
p=LIST1;
q=LIST2;
while (p!=NULL && q!=NULL)
{
if (p->info < q->info)
{
InsEnd(&LIST3,p->info);
p=p->next;
}
else if (p->info > q->info)
{
InsEnd(&LIST3,q->info);
q=q->next;
}
else
{
InsEnd(&LIST3,p->info);
p=p->next;
q=q->next;
}
}
while (p!=NULL)
{
InsEnd(&LIST3,p->info);
p=p->next;
}
while (q!=NULL)
{
InsEnd(&LIST3,q->info);
q=q->next;
}
return LIST3;
}
void main ()
{
struct node *LIST1,*LIST2,*LIST3;
LIST1=NULL;
LIST2=NULL;
LIST3=NULL;
AscendingInsertion(&LIST1,30);
AscendingInsertion(&LIST1,40);
AscendingInsertion(&LIST1,45);
AscendingInsertion(&LIST1,10);
AscendingInsertion(&LIST1,20);
printf("List 1:\t ");
Traverse(&LIST1);
AscendingInsertion(&LIST2,35);
AscendingInsertion(&LIST2,40);
AscendingInsertion(&LIST2,55);
AscendingInsertion(&LIST2,5);
AscendingInsertion(&LIST2,10);
AscendingInsertion(&LIST2,25);
printf("\nList 2:\t ");
Traverse(&LIST2);
printf("\nUnion :");
LIST3=Union(LIST1,LIST2);
Traverse(&LIST3);
}