--
-- This is the body of the linked integer stack package.
--
-- This is needed for the memory free operation.
with Ada.Unchecked_Deallocation;
use Ada;
package body IntStackPkg is
-- Here's the deallocation function for nodes.
procedure Free is new Unchecked_Deallocation(StackNode, NodePtr);
-- Push onto the stack.
procedure Push(S: in out IntStack; I: Integer) is
begin
S := new StackNode'(I, NodePtr(S));
end Push;
-- Remove from the stack.
procedure Pop(S: in out IntStack; I: out Integer) is
OldTop: NodePtr;
begin
if S /= null then
-- Unlink the top node.
OldTop := NodePtr(S);
S := IntStack(S.Next);
-- Get the data out of it.
I := OldTop.Data;
-- Say bye, little node.
Free(OldTop);
end if;
end Pop;
-- Find out what's on the top.
function Top(S: IntStack) return Integer is
begin
if S = null then
return 0;
else
return S.Data;
end if;
end Top;
-- So, are we empty yet?
function IsEmpty(S: IntStack) return Boolean is
begin
return S = null;
end IsEmpty;
-- Remove all memory from the stack.
procedure Clear(S: in out IntStack) is
Zombie, -- Ptr to node about to be deleted.
Scan: NodePtr; -- List scanner.
begin
Scan := NodePtr(S);
while Scan /= null loop
-- Advance the pointer and delete the old node.
Zombie := Scan;
Scan := Scan.Next;
Free(Zombie);
end loop;
S := null;
end Clear;
end IntStackPkg;