-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll_equal.c
40 lines (33 loc) · 881 Bytes
/
ll_equal.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
#include <stdio.h>
#include "node.h"
/* FIXME: this function is buggy. */
int ll_equal(const node* a, const node* b) {
while (a != NULL) {
if (a->val != b->val)
return 0;
a = a->next;
b = b->next;
}
/* lists are equal if a and b are both null */
return a == b;
}
// DO NOT EDIT BELOW THIS LINE
/* The main function exists just to test ll_equal.
There are two tests. The second one is by default
buggy. Please find the error and fix it! */
#ifndef TEST
int main(int argc, char** argv) {
int i;
node nodes[10];
for (i=0; i<10; i++) {
nodes[i].val = 0;
nodes[i].next = NULL;
}
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[3];
printf("equal test 1 result = %d\n", ll_equal(&nodes[0], &nodes[0]));
printf("equal test 2 result = %d\n", ll_equal(&nodes[0], &nodes[2]));
return 0;
}
#endif