(defun append1 (list1 list2) (cond ((null list1) list2) (t (cons (car list1) (append1 (cdr list1) list2))))) (defun rev1 (list) (cond ((null list) nil) (t (append (rev1 (cdr list)) (cons (car list) nil)))))Also recall that the list function returns a list of it's arguments - that is, one cons cell for each argument, where the car points to the argument, and the cdr points to the next cons cell (except for the last cdr, which is nil). Draw the cons cells that get created in processing the following expressions. Make sure to label which cells are pointed at by X and Y.
(setq X (append1 (list 'a 'b 'c) (list 'd 'e 'f))) (setq Y (rev1 X))
1 A 2 B 3 C 4 3,0 5 2,4 6 1,5 7 D 8 E 9 F 10 9,0 11 8,10 12 7,11 13 1,14 14 2,15 15 3,12 16 9,0 17 8,0 18 9,17 19 7,0 20 8,19 21 9,20 22 3,0 23 7,22 24 8,23 25 9,24 26 2,0 27 3,26 28 7,27 29 8,28 30 9,29 31 1,0 32 2,31 33 3,32 34 7,33 35 8,34 36 9,35
In: a b c d e f g Out: h i j Access: a In: a b c d e f g Out: h i j Access: c In: c a b d e f g Out: h i j Access: d In: d c a b e f g Out: h i j Access: i In: i d c a b e f Out: h j g Access: b In: b i d c a e f Out: h j g Access: c In: c b i d a e f Out: h j g Access: h In: h c b i d a e Out: j g f Access: g In: g h c b i d a Out: j f e Access: j In: j g h c b i d Out: f e a Access: i In: i j g h c b d Out: f e aSo, the pages sent out were g,f,e, and a.
In: a b c d e f g Out: h i j Pointer: a Access: a In: a* b c d e f g Out: h i j Pointer: a Access: c In: a* b c* d e f g Out: h i j Pointer: a Access: d In: a* b c* d* e f g Out: h i j Pointer: a Access: i In: a i c* d* e f g Out: h b j Pointer: c Access: b In: a i c d b f g Out: h e j Pointer: f Access: c In: a i c* d b f g Out: h e j Pointer: f Access: h In: a i c* d b h g Out: f e j Pointer: g Access: g In: a i c* d b h g* Out: f e j Pointer: g Access: j In: j i c* d b h g Out: f e a Pointer: i Access: i In: j i* c d b h g Out: f e a Pointer: iSo, the pages sent out were b,e,f,a.
1 12 3 8 4 9 Initial First Pass: 1 - 12 <-> 3 <-> 8 <-> 4 <-> 9 1 3 8 4 9 12* Second Pass:1 - 3 - 8 <-> 4 - 9 12* 1 3 4 8 9* 12* Third Pass:1 - 3 - 4 - 8* 9* 12* 1 3 4 8 9* 12*
Node: 1 2 3 4 5 6 7 8 9 10 11 Heap: Add: 3 Heap: 3 Add: 8 Heap: 8 3 Add: 7 Heap: 8 3 7 Add: 4 Heap: 8 4 7 3 Add: 5 Heap: 8 5 7 3 4 Add: 9 Heap: 9 5 8 3 4 7 Add: 10 Heap: 10 5 9 3 4 7 8 Add: 12 Heap: 12 10 9 5 4 7 8 3 Add: 2 Heap: 12 10 9 5 4 7 8 3 2 Add:8 Heap: 12 10 9 5 8 7 8 3 2 4 Add:6 Heap: 12 10 9 5 8 7 8 3 2 4 6Other heaps are possible, e.g., inserting all the items then checking all the nodes (going up and down when necessary) to fix the heap property.
Node: 1 2 3 4 5 6 7 8 9 10 11 Heap: 12 10 9 5 8 7 8 3 2 4 6 Exchange: 6 10 9 5 8 7 8 3 2 4 |12 Rebuild: 10 8 9 5 6 7 8 3 2 4 Exchange: 4 8 9 5 6 7 8 3 2 |10 Rebuild: 9 8 8 5 6 7 4 3 2
Index: 1 2 3 4 5 6 7 counter: 4 1 3 2 1 1 1 Position: 0 4 5 8 9 10 11 Final Sort: 1 1 1 1 2 3 3 3 4 4 5 6 7