-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp_ll.c
86 lines (62 loc) · 1.28 KB
/
comp_ll.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
/*
* Linked list comparison
*/
#include "common.h"
struct node *head1 = NULL;
struct node *head2 = NULL;
void add(struct node *head, int val) {
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->val = val;
tmp->next = head->next;
head->next = tmp;
}
void print(struct node *head) {
struct node *tmp = head->next;
while (tmp) {
printf("%d > ", tmp->val);
tmp = tmp->next;
}
printf(" NULL\n");
}
int compare(void) {
struct node *tmp1 = head1->next;
struct node *tmp2 = head2->next;
while (tmp1 && tmp2) {
if (tmp1->val != tmp2->val)
return FALSE;
tmp1 = tmp1->next;
tmp2 = tmp2->next;
}
if (!tmp1 && !tmp2)
return TRUE;
else
return FALSE;
}
int main(void) {
head1 = (struct node *)malloc(sizeof(struct node));
head2 = (struct node *)malloc(sizeof(struct node));
head1->next = NULL;
head2->next = NULL;
add(head1, 1);
add(head2, 1);
add(head1, 2);
add(head2, 2);
add(head1, 3);
add(head2, 3);
add(head1, 4);
add(head2, 4);
print(head1);
print(head2);
printf("compare = %d\n", compare());
add(head1, 5);
add(head2, 6);
printf("compare = %d\n", compare());
add(head1, 7);
printf("compare = %d\n", compare());
add(head2, 8);
add(head2, 9);
printf("compare = %d\n", compare());
print(head1);
print(head2);
return 0;
}