forked from wg135/data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosslink.c
141 lines (106 loc) · 2.08 KB
/
crosslink.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#define random(x) (rand()%x)
typedef struct node *node_ptr;
typedef int element_type;
struct node{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
LIST init_list();
void insert_list(element_type element, LIST L);
void print_list(LIST L);
void delet_list(LIST L);
void union_check(LIST L1, LIST L2);
int main(int argc, char *argv[]){
int i;
LIST L1, L2, L3;
position tmp1, tmp2, tmp3;
L1 = init_list();
L2 = init_list();
L3 = init_list();
for(i=0; i<10; i++)
insert_list(1, L1);
for(i=0; i<10; i++)
insert_list(2, L2);
for(i=0; i<10; i++)
insert_list(3, L3);
tmp3 = L3->next;
tmp1 = L1;
tmp2 = L2;
for(i=0; i<10; i++){
tmp1 = tmp1->next;
tmp2 = tmp2->next;
}
tmp1->next = tmp3;
tmp2->next = tmp3;
print_list(L1);
print_list(L2);
union_check(L1, L2);
delet_list(L1);
delet_list(L2);
}
LIST init_list(){
LIST L;
L = (position)malloc(sizeof(struct node));
if(L == NULL){
printf("inti list malloc error\n");
exit(1);
}
else
L->next = NULL;
return L;
}
void insert_list(element_type element, LIST L){
position tmp;
tmp = (position)malloc(sizeof(struct node));
if(tmp == NULL){
printf("new mode malloc error\n");
exit(1);
}
else{
tmp->element = element;
tmp->next = L->next;
L->next = tmp;
}
}
void print_list(LIST L){
position tmp;
tmp = L->next;
while(tmp != NULL){
printf("%d ", tmp->element);
tmp = tmp->next;
}
printf("\n");
}
void delet_list(LIST L){
position tmp, p;
p = L;
while(p != NULL){
tmp = p->next;
free(p);
p = tmp;
}
}
void union_check(LIST L1, LIST L2){
position tmp1, tmp2;
if((L1->next == NULL)||(L2->next == NULL)){
printf("empty list\n");
exit(1);
}
tmp1 = L1;
tmp2 = L2;
while(tmp1->next != NULL){
tmp1 = tmp1->next;
}
while(tmp2->next != NULL){
tmp2 = tmp2->next;
}
if(tmp1 == tmp2){
printf("L1 and L2 are union\n");
}
else
printf("L1 and L2 are not union\n");
}