Last active
January 1, 2017 23:00
-
-
Save minostro/73eea58737720b99417f6bbfa185242a to your computer and use it in GitHub Desktop.
ADT
This file contains 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
-module(queue_adt). | |
-export([empty_queue/0, | |
enqueue/2, | |
dequeue/1, | |
current/1]). | |
empty_queue() -> | |
{[], []}. | |
enqueue({[Value], []}, Element) -> | |
{[Element], [Value]}; | |
enqueue({In, Out}, Element) -> | |
{[Element | In], Out}. | |
dequeue({[], []}) -> | |
{error, queue_empty}; | |
dequeue({[_Head], []}) -> | |
{[], []}; | |
dequeue({In, [_Head | [Next | Rest]]}) -> | |
{In, [Next | Rest]}; | |
dequeue({In, [_Head | _Rest]}) -> | |
{[], lists:reverse(In)}. | |
current({[], []}) -> | |
{error, queue_empty}; | |
current({[Current], []}) -> | |
Current; | |
current({_In, [Current | _Rest]}) -> | |
Current. |
This file contains 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
module QueueADT | |
def self.empty_queue() | |
{in: [], out: []} | |
end | |
def self.enqueue(queue, element) | |
{in: queue[:in] + [element], out: queue[:out]} | |
end | |
def self.dequeue(queue) | |
out = queue[:out] | |
if out.empty? | |
out = queue[:in].reverse | |
end | |
[out.pop, {in: [], out: out}] | |
end | |
end |
This file contains 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
-module(queue_adt). | |
-export([empty_queue/0, | |
enqueue/2, | |
dequeue/1, | |
current/1]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment