Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Priority-queue/PicoLisp/priority-queue-1.l
Normal file
18
Task/Priority-queue/PicoLisp/priority-queue-1.l
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Insert item into priority queue
|
||||
(de insertPQ (Queue Prio Item)
|
||||
(idx Queue (cons Prio Item) T) )
|
||||
|
||||
# Remove and return top item from priority queue
|
||||
(de removePQ (Queue)
|
||||
(cdar (idx Queue (peekPQ Queue) NIL)) )
|
||||
|
||||
# Find top element in priority queue
|
||||
(de peekPQ (Queue)
|
||||
(let V (val Queue)
|
||||
(while (cadr V)
|
||||
(setq V @) )
|
||||
(car V) ) )
|
||||
|
||||
# Merge second queue into first
|
||||
(de mergePQ (Queue1 Queue2)
|
||||
(balance Queue1 (sort (conc (idx Queue1) (idx Queue2)))) )
|
||||
18
Task/Priority-queue/PicoLisp/priority-queue-2.l
Normal file
18
Task/Priority-queue/PicoLisp/priority-queue-2.l
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Two priority queues
|
||||
(off Pq1 Pq2)
|
||||
|
||||
# Insert into first queue
|
||||
(insertPQ 'Pq1 3 '(Clear drains))
|
||||
(insertPQ 'Pq1 4 '(Feed cat))
|
||||
|
||||
# Insert into second queue
|
||||
(insertPQ 'Pq2 5 '(Make tea))
|
||||
(insertPQ 'Pq2 1 '(Solve RC tasks))
|
||||
(insertPQ 'Pq2 2 '(Tax return))
|
||||
|
||||
# Merge second into first queue
|
||||
(mergePQ 'Pq1 'Pq2)
|
||||
|
||||
# Remove and print all items from first queue
|
||||
(while Pq1
|
||||
(println (removePQ 'Pq1)) )
|
||||
23
Task/Priority-queue/PicoLisp/priority-queue-3.l
Normal file
23
Task/Priority-queue/PicoLisp/priority-queue-3.l
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(de heap-first (H) (car H))
|
||||
|
||||
(de heap-merge (H1 H2)
|
||||
(cond
|
||||
((= H1 NIL) H2)
|
||||
((= H2 NIL) H1)
|
||||
((< (car H1) (car H2))
|
||||
(cons (car H1) (cons H2 (cdr H1))))
|
||||
(T
|
||||
(cons (car H2) (cons H1 (cdr H2))))))
|
||||
|
||||
(de heap-insert (Item Heap)
|
||||
(heap-merge (list Item) Heap))
|
||||
|
||||
(de "merge-pairs" (H)
|
||||
(if (= (cdr H) NIL)
|
||||
(car H) # also handles NIL (H = NIL -> NIL)
|
||||
(heap-merge
|
||||
(heap-merge (car H) (cadr H))
|
||||
("merge-pairs" (cddr H)))))
|
||||
|
||||
(de heap-rest (H)
|
||||
("merge-pairs" (cdr H)))
|
||||
15
Task/Priority-queue/PicoLisp/priority-queue-4.l
Normal file
15
Task/Priority-queue/PicoLisp/priority-queue-4.l
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(setq H NIL)
|
||||
(for
|
||||
Task '(
|
||||
(3 . "Clear drains.")
|
||||
(4 . "Feed cat.")
|
||||
(5 . "Make tea.")
|
||||
(1 . "Solve RC tasks.")
|
||||
(2 . "Tax Return."))
|
||||
(setq H (heap-insert Task H)))
|
||||
|
||||
(while H
|
||||
(prinl (caar H) ". " (cdar H))
|
||||
(setq H (heap-rest H)))
|
||||
|
||||
(bye)
|
||||
Loading…
Add table
Add a link
Reference in a new issue