Skip to content

Instantly share code, notes, and snippets.

@minostro
Last active January 1, 2017 23:00
Show Gist options
  • Save minostro/73eea58737720b99417f6bbfa185242a to your computer and use it in GitHub Desktop.
Save minostro/73eea58737720b99417f6bbfa185242a to your computer and use it in GitHub Desktop.
ADT
-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.
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
-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