Created
July 13, 2013 17:45
-
-
Save zhuangya/5991558 to your computer and use it in GitHub Desktop.
implenent stack using queue
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
| 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