unit Stack;

{ Implementiert einen dynamischen Stack.
 Copyright (c) 8/99 by Sebastian Koppehel }

interface

type
   PStk	=  ^TStk;
   TStk	= record
	     Data : Pointer;
	     Next : PStk;
	  end;	  

procedure PushStack(var Stk : PStk; Element : Pointer);
function PopStack(var Stk : PStk) : Pointer;
function TopStack(Stk : PStk) : Pointer;
function StackEmpty(Stk : PStk) : Boolean;

implementation

procedure PushStack(var Stk : PStk; Element : Pointer);
var
   NewStk : PStk;
begin
   New(NewStk);
   NewStk^.Data := Element;
   NewStk^.Next := Stk;
   Stk := NewStk;
end;

function PopStack(var Stk : PStk) : Pointer;
var
   OldTop : PStk;
begin
   OldTop := Stk;
   PopStack := OldTop^.Data;
   Stk := OldTop^.Next;
   Dispose(OldTop);
end;

function TopStack(Stk : PStk) : Pointer;
begin
   TopStack := Stk^.Data;
end;

function StackEmpty(Stk	: PStk) : Boolean;
begin
   StackEmpty := Stk = nil;
end;
   
end.

