with Ada.Text_IO; use Ada.Text_IO; package body Stack is procedure Push (This : in out Stack; E : in Elem) is begin This.Len := This.Len + 1; Stack_Vectors.Append (This.V, E); end Push; procedure Pop (This : in out Stack; E : out Elem) is begin E := Stack_Vectors.Last_Element (This.V); Stack_Vectors.Delete_Last (This.V); This.Len := This.Len - 1; end Pop; function Pop (This : in out Stack) return Elem is Element : Elem; begin This.Pop (Element); return Element; end Pop; -- pop until E is found procedure Pop_Until (This : in out Stack; E : in Elem) is Element : Elem; Nb_Deleted_Item : Ada.Containers.Count_Type := 0; Length : Count_Type := This.Len; Found : Boolean := False; End_Of_Stack : Boolean := False; Pop_Cursor : Stack_Vectors.Cursor := Stack_Vectors.Last (This.V); use Ada.Containers; begin while not Found and not End_Of_Stack loop if Length = 0 then End_Of_Stack := True; else Nb_Deleted_Item := Nb_Deleted_Item + 1; Length := Length - 1; Element := Stack_Vectors.Element (Pop_Cursor); if (E = Element) then Found := True; end if; Pop_Cursor := Stack_Vectors.Previous (Pop_Cursor); end if; end loop; if (Found) then Stack_Vectors.Delete_Last (This.V, Nb_Deleted_Item); This.Len := This.Len - Count_Type (Nb_Deleted_Item); else Put ("Pop_Until Error:"); Print (E); Put (" Stack :"); Print_All (This => This); Put_Line (""); end if; end Pop_Until; procedure Pop (This : in out Stack) is begin Stack_Vectors.Delete_Last (This.V); This.Len := This.Len - 1; end Pop; procedure Print_All (This : in Stack) is begin if (This.Len > 0) then for I in 1 .. This.Len - 1 loop Print (Stack_Vectors.Element (This.V, I)); Put (":"); end loop; Print (Stack_Vectors.Element (This.V, This.Len)); Put_Line (""); else Put_Line ("Empty Stack"); end if; end Print_All; function Length (This : in Stack) return Count_Type is begin return This.Len; end Length; function Is_Full (This : in out Stack) return Boolean is begin return This.Len = Size; end Is_Full; function Is_Empty (This : in out Stack) return Boolean is begin return This.Len = 0; end Is_Empty; end Stack;