-- File stacks.adb -- Body for a simple stack package, implemented as a linked list -- The stack is intended to be used in lab4, to help implement an -- infix to postfix algorithm -- Stack elements are characters package body Stacks is -- constructors procedure Copy (Source : in Stack; Destination : in out Stack) is from, to: Stack; begin from := Source; if Source = null then Destination := null; else Destination := new Node' (Datum => Source.Datum, Tail => null); from := Source.Tail; to := Destination; while from /= null loop to.Tail := new Node' (Datum => from.Datum, Tail => null); to := to.Tail; from := from.Tail; end loop; end if; end Copy; procedure Clear (AStack : in out Stack) is begin AStack := null; end Clear; procedure Push (Item : in Character; OnStack : in out Stack) is begin OnStack := new Node' (Datum => Item, Tail => OnStack); end Push; procedure Pop (AStack : in out Stack) is begin if AStack = null then raise underflow; else AStack := AStack.Tail; end if; end Pop; -- selectors function Equal (Left, Right : in Stack) return Boolean is lindex, rindex: Stack; begin lindex := Left; rindex := Right; while lindex /= null loop if lindex.Datum /= rindex.Datum then return False; else lindex := lindex.Tail; rindex := rindex.Tail; end if; end loop; return (rindex = null); exception when Constraint_Error => return False; end Equal; function Depth (OfStack : in Stack) return Natural is count: integer := 0; index: Stack; begin index := OfStack; while index /= null loop index := index.Tail; count := count + 1; end loop; return count; end Depth; function Empty (AStack : in Stack) return Boolean is begin return (AStack = null); end Empty; function Top (OfStack : in Stack) return Character is begin if OfStack = null then raise underflow; else return (OfStack.Datum); end if; end Top; end Stacks;