File tree 3 files changed +36
-6
lines changed
3 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -40,3 +40,18 @@ void list_remove(struct list_node *item)
40
40
item -> prev = NULL ;
41
41
item -> next = NULL ;
42
42
}
43
+
44
+ void list_move (struct list_node * dst , struct list_node * src )
45
+ {
46
+ if (list_is_empty (src )) {
47
+ dst -> next = dst -> prev = dst ;
48
+ } else {
49
+ dst -> next = src -> next ;
50
+ dst -> prev = src -> prev ;
51
+
52
+ dst -> next -> prev = dst ;
53
+ dst -> prev -> next = dst ;
54
+
55
+ src -> next = src -> prev = src ;
56
+ }
57
+ }
Original file line number Diff line number Diff line change @@ -54,4 +54,7 @@ void list_insert(struct list_node *item, struct list_node *after);
54
54
/* Remove a node from its containing list. */
55
55
void list_remove (struct list_node * item );
56
56
57
+ /* Move the contents of one list to another in constant time. */
58
+ void list_move (struct list_node * dst , struct list_node * src );
59
+
57
60
#endif
Original file line number Diff line number Diff line change @@ -63,13 +63,13 @@ static void test_remove(int mask)
63
63
list_remove (& recs [i ].node );
64
64
}
65
65
66
- static void test_verify (int mask )
66
+ static void test_verify (struct list_node * l , int mask )
67
67
{
68
68
int even_count = (mask & 2 ) ? (N / 2 ) : 0 ;
69
69
int odd_count = (mask & 1 ) ? (N / 2 ) : 0 ;
70
70
struct list_node * n ;
71
71
72
- for (n = lst . next ; n != & lst ; n = n -> next ) {
72
+ for (n = l -> next ; n != l ; n = n -> next ) {
73
73
struct record * r = (struct record * )n ;
74
74
75
75
if (r -> v & 1 )
@@ -84,16 +84,28 @@ static void test_verify(int mask)
84
84
85
85
int main (void )
86
86
{
87
+ struct list_node copy ;
88
+
87
89
test_init ();
88
90
89
91
test_start_odd ();
90
- test_verify (1 );
92
+ test_verify (& lst , 1 );
91
93
test_add_evens ();
92
- test_verify (3 );
94
+ test_verify (& lst , 3 );
95
+
96
+ list_move (& copy , & lst );
97
+ test_verify (& copy , 3 );
98
+ assert (list_is_empty (& lst ));
99
+
100
+ list_move (& lst , & copy );
101
+ test_verify (& lst , 3 );
102
+ assert (list_is_empty (& copy ));
103
+
93
104
test_remove (1 );
94
- test_verify (2 );
105
+ test_verify (& lst , 2 );
106
+
95
107
test_remove (2 );
96
- test_verify (0 );
108
+ test_verify (& lst , 0 );
97
109
98
110
return 0 ;
99
111
}
You can’t perform that action at this time.
0 commit comments