Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Created June 6, 2018 16:37
Show Gist options
  • Save skalahonza/b0e1fe01bd6e6275a69a13ec4bdc07f2 to your computer and use it in GitHub Desktop.
Save skalahonza/b0e1fe01bd6e6275a69a13ec4bdc07f2 to your computer and use it in GitHub Desktop.
Queue implementation in scheme using 2 stacks.
(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