Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {$mode objfpc}
- program Stack_Class;
- uses sysutils;
- type generic TStack<T> = class(TObject)
- private
- const MINIMUM = 32;
- var data : array of T;
- top : word;
- public
- function Pop : T;
- procedure Push(value : T);
- function IsEmpty : boolean;
- constructor Create;
- constructor Create(limit : word); overload;
- end;
- constructor TStack.Create();
- begin
- self.Create(self.MINIMUM);
- end;
- constructor TStack.Create(limit : word); overload;
- begin
- if limit <= 0 then limit := self.MINIMUM;
- SetLength(self.data, limit);
- self.top := 0;
- end;
- procedure TStack.Push(value : T);
- begin
- inc(self.top);
- if self.top > length(self.data) then SetLength(self.data, Length(self.data)*2);
- self.data[self.top-1] := value;
- end;
- function TStack.Pop : T;
- begin
- Pop := self.data[self.top-1];
- dec(self.top);
- end;
- function TStack.IsEmpty : boolean;
- begin
- IsEmpty := (self.top = 0);
- end;
- type TIntegerStack = specialize TStack<Integer>;
- var s : TIntegerStack;
- i : integer;
- begin
- try
- s := TIntegerStack.Create;
- for i in [1..10] do s.Push(i);
- while not s.IsEmpty do write(s.Pop, ', ');
- writeln('FIM');
- s.Free;
- except
- on ex:exception do begin
- writeln; writeln(upcase(ex.classname), ': ', ex.message);
- end;
- end;
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment