unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 =
class
(TForm)
Memo1: TMemo;
Button1: TButton;
procedure
FormCreate(Sender: TObject);
procedure
Button1Click(Sender: TObject);
private
public
end
;
var
Form1: TForm1;
S:TStack;
implementation
{$R *.dfm}
procedure
TForm1
.
FormCreate(Sender: TObject);
begin
S:=TStack
.
Create;
end
;
procedure
TForm1
.
Button1Click(Sender: TObject);
begin
memo1
.
Lines
.
Add(
pansichar
(S));
end
;
end
.
unit
Unit2;
interface
uses
SysUtils, Dialogs;
const
StackEmpty=
nil
;
type
T=
integer
;
TStackEl=^StackEl;
StackEl=
record
el:T;
prev:TStackEl;
end
;
TStack=
class
private
StackTop:TStackEl;
public
Constructor
Create;
Function
EmptyStack:
boolean
;
procedure
AddToStack(elem:T);
procedure
DelFromStack;
function
LastOfStack:T;
end
;
implementation
Constructor
TStack
.
Create;
begin
Inherited
Create;
end
;
Function
TStack
.
EmptyStack;
begin
EmptyStack:=StackTop=StackEmpty;
end
;
procedure
TStack
.
AddToStack;
var
tmp:TStackEl;
begin
new(tmp);
tmp^.el:=elem;
tmp^.prev:=StackTop;
StackTop:=tmp;
end
;
procedure
TStack
.
DelFromStack;
var
help:TStackEl;
begin
if
StackTop<>StackEmpty
then
begin
help:=StackTop;
StackTop:=StackTop^.prev;
dispose(help);
end
else
showmessage (
'error stack pust'
);
end
;
function
TStack
.
LastOfStack;
begin
if
StackTop<>StackEmpty
then
result:=StackTop^.el
else
showmessage(
'error1 stack pust'
);
end
;
end
.