Erlang數(shù)據(jù)結(jié)構(gòu)

  • Stack
  • Queue
  • Table
  • Trees
  • Graph
  • Union/Find

Stack

基于單個(gè)List的實(shí)現(xiàn)

% stack
-module(stack).
-export([emptyStack/0, stackEmpty/1, push/2, pop/1, top/1]).


% 生成空棧
emptyStack() -> [].

% 判斷是否為空
stackEmpty([]) -> true;
stackEmpty(_) -> false.

%
push(X, XS) -> [X|XS]. 

%
pop([]) -> error("pop from am empty stack");
pop(_|XS) -> XS.

%
top([]) -> error("top from an empty stack");
top([X|_]) -> X.

queue

用單個(gè)List實(shí)現(xiàn)

-module(queue).
-export([emptyQueue/0, queueEmpty/1, enqueue/2, dequeue/1, front/1, showQ/1]).

emptyQueue() -> [].

queueEmpty([]) -> true;
queueEmpty(_) -> false.

enqueue(X, Q) -> Q ++ [X].

dequeue([_|XS]) -> XS;
dequeue([]) -> error("dequeue : empty queue").

front([X|_]) -> X;
front([]) -> error("front: empty queue").

用一對List實(shí)現(xiàn)(或者說用列表的元組)

showQ({F, R}) -> F++lists:reverse(R).

queueEmpty({[], []}) -> true;
queueEmpty(_) -> false.

emptyQueue() -> {[], []}.

enqueue(X, {[], []}) -> {[X], []};
enqueue(Y, {XS, YS}) -> {XS, [Y|YS]}.

dequeue({[],[]}) -> error("dequeue:empty queue");
dequeue({[],YS}) -> {lists:nthtail(1, lists:reverse(YS)), []};
dequeue({[_|XS],YS}) -> {XS,YS}.

front({[],[]}) -> error("front:empty queue");
front({[],YS}) -> lists:last(YS);
front({[X|_],_}) -> X.

Table

-module(table).
-export([newTable/1, findTable/2, updTable/2]).
-export([newTableL/1, findTableL/2, updTableL/2]).


newTable(Assocs) ->
    lists:foldr(fun table:updTable/2, 
        fun(_) -> error("item not found in table") end,
        Assocs).

updTable({I,X}, F) -> 
    G = fun(J) ->
        case J =:= I of 
            true -> X;
            false -> F(J)
        end
    end,
    G.

newTableL(T) -> T.

findTableL([], _) -> error("item not found in table");
findTableL([{J,V}|_], I) when I =:= J -> V;
findTableL([_|R], I) -> findTableL(R, I).

updTableL(E, []) -> [E];
updTableL({I,_a}, [{J,_}|R]) when I =:= J -> [{I,_a}|R];
updTableL({I,_a}, [{J,_b}|R]) -> [{J,_a}|updTableL({I,_b}, R)].

Tree


-module(bintree).
-export([emptyTree/0, inTree/2, addTree/2, delTree/2]).
-export([buildTree/1, buildTree1/1, inorder/1]).


inTree(_, emptyBT) -> false;
inTree(V1, {V, _, _}) when V =:= V1 -> true;
inTree(V1, {V, LF, _}) when V1 < V -> inTree(V1, LF);
inTree(V1, {V, _, RT}) when  V1 > V -> inTree(V1, RT).


addTree(V1, emptyBT) -> {V1, emptyBT, emptyBT};
addTree(V1, {V, LF, RT}) when V1 =:= V -> {V, LF, RT};
addTree(V1, {V, LF, RT}) when V1 < V -> {V, addTree(V1, LF), RT};
addTree(V1, {V, LF, RT}) -> {V, LF, addTree(V1, RT)}.


buildTree1([]) -> emptyBT;
buildTree1(LF) -> 
    N = length(LF) div 2,
    L1 = lists:sublist(LF, N),
    [X|L2] = lists:nthtail(LF, N),
    {X, buildTree1(L1), buildTree(L2)}.

 
delTree(v1, emptyBT) -> eEmptyBT;



delTree(V1, {V, LF, emptyBT}) when V1 =:= V -> LF;
delTree(V1, {V, emptyBT, RT}) when V1 =:= V -> RT;
delTree(V1, {V, LF, RT}) when V1 < V -> {V, delTree(V1, LF), RT}; 
delTree(V1, {V, LF, RT}) when V1 > V -> {V, LF, delTree(V1, RT)};
delTree(V1, {V, LF, RT}) when V1 =:= V ->
    K = minTree(RT), {K, LF, delTree(K, RT)}.



minTree({V, emptyBT, _}) -> V;
minTree({ _, LF, _}) -> minTree(LF).
inorder(emptyBT) -> [];
inorder({V, LF, RT}) -> inorder(LF) ++ [V] ++ inorder(RT).

Union_find

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,936評論 0 33
  • LeetCode 刷題隨手記 - 第一部分 前 256 題(非會(huì)員),僅算法題,的吐槽 https://leetc...
    蕾娜漢默閱讀 18,431評論 2 36
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,897評論 18 399
  • 課程介紹 先修課:概率統(tǒng)計(jì),程序設(shè)計(jì)實(shí)習(xí),集合論與圖論 后續(xù)課:算法分析與設(shè)計(jì),編譯原理,操作系統(tǒng),數(shù)據(jù)庫概論,人...
    ShellyWhen閱讀 2,478評論 0 3
  • 容器的概念所謂STL容器,即是將最常運(yùn)用的一些數(shù)據(jù)結(jié)構(gòu)(data structures)實(shí)現(xiàn)出來。容器是指容納特定...
    飯飯H閱讀 446評論 0 0

友情鏈接更多精彩內(nèi)容