Skip to content

Instantly share code, notes, and snippets.

@zhuangya
Created July 13, 2013 17:45
Show Gist options
  • Select an option

  • Save zhuangya/5991558 to your computer and use it in GitHub Desktop.

Select an option

Save zhuangya/5991558 to your computer and use it in GitHub Desktop.
implenent stack using queue
class Queue
constructor: (@value)->
@value = @value or []
enqueue: (what) ->
@value.push what
dequeue: ->
stash = []
ret = @value[0]
for i in [0...@value.length - 1]
stash[i] = @value[i + 1] if @value[i + 1]
@value = stash
ret
getLength: ->
@value.length
class Stack extends Queue
constructor: (@value) ->
super([])
push: (what) ->
@enqueue what
pop: ->
stash = new Queue
for i in [0...@getLength() - 1]
stash.enqueue @dequeue()
@value = stash.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment