Created
June 6, 2018 16:37
-
-
Save skalahonza/b0e1fe01bd6e6275a69a13ec4bdc07f2 to your computer and use it in GitHub Desktop.
Queue implementation in scheme using 2 stacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define first car) | |
(define second cadr) | |
(define rest cdr) | |
(define (third list) (car (cddr list))) | |
(define fourth cadddr) | |
(define (push v st) | |
(cons v st)) | |
(define(pop st) | |
(cdr st)) | |
(define( inStackOutStack ist ost) | |
(list ist ost)) | |
; insert into in stack | |
(define (enqueue v q) | |
(list (push v (first q)) (second q)) | |
) | |
; dispatch and pop from outstack | |
(define (dequeue q) | |
(let* | |
( | |
(disp (dispatch q)) | |
(ins (first disp)) | |
(outs (second disp)) | |
) | |
;expression | |
(list ins (pop outs)) | |
) | |
) | |
; if out stack is empty -> reverse in stack and put it in out stack --> empty in stack | |
(define (dispatch q) | |
(let* | |
( | |
(ins (first q)) | |
(outs (second q)) | |
) | |
(cond | |
((null? outs) (list '() (reverse ins))) | |
(#t q) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment