j.next = i.next i.next.prev = j j.prev = i i.next = jOtherwise we would also have to search for i, and/or point the end marker to j.
Stack: Operation: push a Stack: a Operation: push b Stack: a b Operation: push c Stack: a b c Operation: pop Stack: a b Operation: push d Stack: a b d Operation: pop Stack: a b Operation: pop Stack: a Operation: push e Stack: a e
Queue: Operation: insert a Queue: a Operation: insert b Queue: a b Operation: insert c Queue: a b c Operation: remove Queue: b c Operation: insert d Queue: b c d Operation: remove Queue: c d Operation: remove Queue: d Operation: insert e Queue: d e
a < b, a < c, d < b, e < c, a < e, a <
d
a 0 b c d e b 2 c 2 d 1 b e 1 cSo, a must be first followed by d or e. B can be anywhere after d, and c could be anywhere after e. Thus one possible order is a d e b c.
| |
(A B 6) (A C 3) (B C 3) (B E 6) (C E 7) (C D 2) (E D 3)
Connected Nodes: A Edges: Unconnected: B C D E New Edge: AC Connected Nodes: A,C Edges: AC Unconnected: B D E New Edge: CD Connected Nodes: A,C,D Edges: AC,CD Unconnected: B E New Edge: BC Connected Nodes: A,B,C,D Edges: AC,CD,BC Unconnected: E New Edge: DE Connected Nodes: A,B,C,D,E Edges: AC,CD,BC,DE Unconnected:
A B C D E A 6 3 B 6 3 6 C 3 3 2 7 D 2 3 E 6 7 2Alternatively, rather than an array with one space for each possible edge, whether it exists or not, could have had a list of pairs of vertex and weight
_____ | | | ||_||_| | | v v a b
_____ __ __ __ __ | | ---->| | ---->| | /| ||_|__| ||_|__| ||_|/_| | | | v v v a b c
_____ __ __ | | ---->| | /| ||_|__| ||_|/_| | | v v a b
(defun mystery (list) (cond ((null list) nil) ((null (cdr list)) list) (t (cons (car list) (append (mystery (cdr list)) (cons (car list) nil))))))
(mystery '(a b c))
(a b c b a)
(setq X (mystery (list 1 3 5 6)))
1 #6,0 2 #5,1 3 #3,2 4 #1,3 5 #5,0 6 #6,5 7 #5,6 8 #3,0 9 #5,8 10 #6,9 11 #5,10 12 #3,11 13 #1,0 14 #3,13 15 #5,14 16 #6,15 17 #5,16 18 #3,17 19 #1,18
insertionsort
which takes an
arbitary list of numbers and returns a sorted list by repeatedly calling the
second: insert
, which takes an item and a sorted list, and
returns a new list with the item included in it's proper location.
(defun insert (item list) (cond ((or (null list) (< item (car list))) (cons item list)) (t (cons (car list) (insert item (cdr list)))))) (defun insertion-sort (list) (cond ((null list) nil) (t (insert (car list) (insertion-sort (cdr list)))))) ;alternatively: (defun insertion-sort2 (list) (insert-help list nil)) (defun insert-help (unsorted sorted) (cond ((null unsorted) sorted) (t (insert-help (cdr unsorted) (insert (car unsorted) sorted)))))
Chicago, (10, 40) (the root) Miami, (90, 100)If you are looking for items inside the box centered at Miami with horizontal extent 100 and vertical extent 60, which quadrants of Chicago can you avoid searching?