package dllist; import basic; public class DLNode extends basic.Object { local d_left, d_right; local d_data; method init( data, left = @nil, right = @nil) { d_data = data; d_left = @nil; d_right = @nil; [self set_left left]; [self set_right right]; } method set_data(data) { d_data = data; } method get_data() { return d_data; } method left() { return d_left; } method set_left( node, recurse = @true ) { d_left = node; if (d_left && recurse) [d_left set_right self, @false]; } method right() { return d_right; } method set_right( node, recurse = @true ) { d_right = node; if (d_right && recurse) [d_right set_left self, @false]; } method walk( func, mode ) { if (mode == #preorder) { func( d_data ); if (d_left) [d_left walk func, mode]; if (d_right) [d_right walk func, mode]; } else if (mode == #postorder) { if (d_left) [d_left walk func, mode]; if (d_right) [d_right walk func, mode]; func( d_data ); } else if (mode == #inorder) { if (d_left) [d_left walk func, mode]; func( d_data ); if (d_right) [d_right walk func, mode]; } else if (mode == #left) { func( d_data ); if (d_left) [d_left walk func, mode]; } else if (mode == #right) { func( d_data ); if (d_right) [d_right walk func, mode]; } } } public class DLList extends basic.Object { local d_head; method init() { d_head = @nil; } method prepend( value ) { local node = [DLNode new value, @nil, d_head]; d_head = node; } method append( value ) { local tail; tail = [self tail]; local node = [DLNode new value, tail, @nil]; if (! d_head) d_head = node; } method head() { return d_head; } method tail() { local tail, next; // search tail tail = d_head; while (tail && (next = [tail right])) tail = next; return tail; } method walk( func, mode ) { if (d_head) [d_head walk func, mode]; } } private mylist = [DLList new]; [mylist append 5]; [mylist append "Hello"]; [mylist append 6.6]; [mylist append "World"]; [mylist append 1]; [mylist append 2]; [mylist append 3]; private function print_val( el ) { basic.print( el, '\n' ); } basic.print("\n*** From left to right...\n"); [mylist walk print_val, #right]; basic.print("\n*** ... and from right to left...\n"); [[mylist tail] walk print_val, #left];