我自己写了一个堆栈,
type TStack=class
private
size:Integer;
container:array [1..20] of Integer;
public
constructor Create();
function Push(aNode:Integer):boolean;
function Pop(out aNode:Integer):boolean;
end;
implementationconstructor TStack.Create();
begin
size:=0;
end;function TStack.Push(aNode:Integer):boolean;
begin
if(size>=20) then
begin
Exception.Create('Stack is full!');
result:=false;
end
else
begin
container[size]:=aNode;
Inc(size);
result:=True;
end;end;function TStack.Pop(out aNode:Integer):boolean;
begin
if (size<=0) then
begin
Exception.Create('Stack is empty');
result:=False;
end
else
begin
aNode:=container[size];
dec(size);
result:=True;
end; end;
测试代码如下,运行后点击pop,push就会出现内存冲突,请问怎么回事?
var
Form1: TForm1;
S1:TStack;
procedure TForm1.Button1Click(Sender: TObject);
begin
S1.Push(StrToInt(Edit1.Text));
end;procedure TForm1.Button2Click(Sender: TObject);
var
msg:String;
node:Integer;
begin
S1.Pop(node);
msg:=IntTostr(node);
Showmessage(msg);
end;
type TStack=class
private
size:Integer;
container:array [1..20] of Integer;
public
constructor Create();
function Push(aNode:Integer):boolean;
function Pop(out aNode:Integer):boolean;
end;
implementationconstructor TStack.Create();
begin
size:=0;
end;function TStack.Push(aNode:Integer):boolean;
begin
if(size>=20) then
begin
Exception.Create('Stack is full!');
result:=false;
end
else
begin
container[size]:=aNode;
Inc(size);
result:=True;
end;end;function TStack.Pop(out aNode:Integer):boolean;
begin
if (size<=0) then
begin
Exception.Create('Stack is empty');
result:=False;
end
else
begin
aNode:=container[size];
dec(size);
result:=True;
end; end;
测试代码如下,运行后点击pop,push就会出现内存冲突,请问怎么回事?
var
Form1: TForm1;
S1:TStack;
procedure TForm1.Button1Click(Sender: TObject);
begin
S1.Push(StrToInt(Edit1.Text));
end;procedure TForm1.Button2Click(Sender: TObject);
var
msg:String;
node:Integer;
begin
S1.Pop(node);
msg:=IntTostr(node);
Showmessage(msg);
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货