forked from LTstrange/datastruct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastruct_3_2.cpp
89 lines (82 loc) · 1.58 KB
/
datastruct_3_2.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
#include <stdio.h>
#include <malloc.h>
typedef struct Node{
int data;
struct Node* next;
}Node, *LinkList;
void InitList(LinkList *L){
*L = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL;
}
void CreateFromTail(int count, LinkList L){
Node *s;
int i=0;
while(count&&i!=EOF){
scanf("%d", &i);
s = (LinkList)malloc(sizeof(Node));
s->data = i;
L->next = s;
L = s;
count--;
}
}
void show(LinkList L){
LinkList s = L;
while (s->next){
printf("%d ", s->next->data);
s = s->next;
}
}
LinkList Merge(LinkList LA, LinkList LB){
LinkList pa, pb, pc;
LinkList LC, r, s;
pa = LA->next;
pb = LB->next;
LC = LA;
LC->next=NULL;r=LC;
while (pa != NULL && pb != NULL){
if (pa->data <= pb->data){
r->next=pa;
r = pa;
pa = pa->next;
}
else{
r->next = pb;
r = pb;
pb = pb->next;
}
}
if (pa)
r->next=pa;
else
r->next=pb;
free(LB);
pc = LC->next;
int flag = 0;
while(pc && pc->next)
{
flag=0;
if(pc->data == pc->next->data)
{
s = pc->next;
pc->next = s->next;
flag = 1;
}
if(!flag)
pc = pc->next;
}
return LC;
}
int main(){
int i=0;
LinkList LA, LB, LC;
InitList(&LA);
InitList(&LB);
scanf("%d", &i);
CreateFromTail(i, LA);
scanf("%d", &i);
CreateFromTail(i, LB);
LC = Merge(LA, LB);
show(LC);
return 0;
}