From 727e3c59346da4f91284b34b4c18f2e0ba155e53 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 9 Aug 2025 16:03:28 +0200 Subject: Initial commit --- stack/src/stack.adb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 stack/src/stack.adb (limited to 'stack/src/stack.adb') diff --git a/stack/src/stack.adb b/stack/src/stack.adb new file mode 100644 index 0000000..4dc8fb1 --- /dev/null +++ b/stack/src/stack.adb @@ -0,0 +1,31 @@ +with Ada.Unchecked_Deallocation; + +package body Stack is + procedure Free is new Ada.Unchecked_Deallocation (Node, Node_Access); + + procedure Push (S : in out Stack; Val : T) is + New_Top : Node_Access := new Node; + begin + New_Top.Val := Val; + New_Top.Bottom := S.Top; + S.Top := New_Top; + end Push; + + function Pop (S : in out Stack; Val : out T) return Boolean is + Old_Top : Node_Access := S.Top; + begin + if Old_Top /= null then + Val := Old_Top.Val; + S.Top := Old_Top.Bottom; + Free (Old_Top); + return True; + else + return False; + end if; + end Pop; + + function Empty (S : Stack) return Boolean is + begin + return S.Top = null; + end Empty; +end Stack; -- cgit v1.2.3