home *** CD-ROM | disk | FTP | other *** search
- Unit GenStack;
- {$R-,O+,V-}
-
- { Unit GenStack written by Eric C. Wentz Last Mod Date 6/28/89 }
-
- { FlexStacks are simple FlexArray based stacks using the DataType }
- { defined in Unit GenArray. Here we START to see some of the benefits }
- { of the GenArray Object, but the full benefit will not be seen until }
- { the next "Generation" of descendants are defined, in Unit Stacks. }
-
- INTERFACE
-
- Uses Crt,GenArray;
-
- Type
- FlexStack = Object(FlexArray) {Dynamically allocated Stack}
-
- CurTop : Word; {Index to Top of Stack}
-
- Procedure Create;
-
- Function Full : Boolean;
- Function Depth : Word;
- Function Empty : Boolean;
-
- Procedure Copy (F : FlexStack);
- Procedure ReSize (Num : Word); {Grow (or Shrink) by Num elements}
-
- {NOTE: It is an Error to ReSize a FlexStack to Zero (or Negative)}
-
- Procedure Push (Var El; Size : Word); {Size of El MUST match ElementSize}
- Procedure Pop (Var El; Size : Word);
- Procedure Top (Var El; Size : Word); {TOP does not POP the Stack}
-
- (* Applicable inherited procedures and functions: *)
-
- { Procedure Init (MaxElements,ElementSize); }
- { Procedure Destroy; }
- { Function MaxSize : Word; }
- { Function ElemSize : Word; }
-
- End;
-
- IMPLEMENTATION
-
- Procedure StackError (Num : Byte);
- Begin
- WriteLn;
- Write ('FlexStack ERROR: ');
- Case Num of
- 0 : WriteLn ('Attempted PUSH onto Full FlexStack.');
- 1 : WriteLn ('Attempted POP or TOP from Empty FlexStack.');
- End;
- WriteLn ('**** PROGRAM TERMINATED ****');
- WriteLn;
- Write ('Press <Return> to Continue.... ');
- ReadLn;
- HALT (0)
- End;
-
- Procedure FlexStack.Create;
- Begin
- CurTop := 0;
- FlexArray.Create
- End;
-
- Function FlexStack.Full : Boolean;
- Begin
- Full := CurTop = MaxSize
- End;
-
- Function FlexStack.Empty : Boolean;
- Begin
- Empty := CurTop = 0
- End;
-
- Function FlexStack.Depth : Word;
- Begin
- Depth := CurTop
- End;
-
- Procedure FlexStack.Copy (F : FlexStack);
- Begin
- FlexArray.Copy (F);
- CurTop := F.CurTop
- End;
-
- Procedure FlexStack.ReSize (Num : Word);
- Begin
- FlexArray.ReSize (MaxSize + Num)
- End;
-
- Procedure FlexStack.Push (Var El; Size : Word);
- Begin
- If Full Then StackError (0);
- Accept (El,CurTop,Size);
- CurTop := CurTop + 1
- End;
-
- Procedure FlexStack.Pop (Var El; Size : Word);
- Begin
- If Empty Then StackError (1);
- CurTop := CurTop - 1;
- Retrieve (El,CurTop,Size)
- End;
-
- Procedure FlexStack.Top (Var El; Size : Word);
- Begin
- If Empty Then StackError (1);
- Retrieve (El,CurTop-1,Size)
- End;
-
- BEGIN
- END.