Een stack napraviti sa 4 broja i onda vrijednosti jednu po jednu iz stack-a ucitati i isprintati.
Ovdje treba da koristim sljedecu proceduru:
Code:
unit untStack;
interface
type
TStackNodePtr = ^TStackNode;
TStackNode = record
iValue: Integer;
snpNext: TStackNodePtr
end;
procedure Push (iNewValue: Integer);
{ This procedure pushes value iNewValue on the stack.
When there is no more free memory space, the exception
EOutOfMemory will be raised.
pre: -
post: Value iNewValue on top of stack or exception EOutOfMemory is raised}
function Pop: Integer;
{ This function pops the top-most value from the stack and returns its value.
pre: Stack is not empty
post: Pop = former topmost value of stack
and this value has been removed from the stack }
function IsEmpty: Boolean;
{ This function returns TRUE if and only if the stack is empty.
pre: -
post: IsEmpty = TRUE <==> stack is empty}
implementation
var
snpTop: TStackNodePtr = NIL;
procedure Push (iNewValue: Integer);
var
snpNewNode: TStackNodePtr;
begin { Push }
New (snpNewNode); { may raise EOutOfMemory exception }
snpNewNode^.iValue := iNewValue;
snpNewNode^.snpNext := snpTop;
snpTop := snpNewNode
end { Push };
function Pop: Integer;
var
snpOldNode: TStackNodePtr;
begin { Pop }
snpOldNode := snpTop;
snpTop := snpTop^.snpNext;
result := snpOldNode^.iValue;
Dispose (snpOldNode)
end { Pop };
function IsEmpty: Boolean;
begin { IsEmpty }
result := snpTop = NIL
end { IsEmpty };
end.
unit untStack;
interface
type
TStackNodePtr = ^TStackNode;
TStackNode = record
iValue: Integer;
snpNext: TStackNodePtr
end;
procedure Push (iNewValue: Integer);
{ This procedure pushes value iNewValue on the stack.
When there is no more free memory space, the exception
EOutOfMemory will be raised.
pre: -
post: Value iNewValue on top of stack or exception EOutOfMemory is raised}
function Pop: Integer;
{ This function pops the top-most value from the stack and returns its value.
pre: Stack is not empty
post: Pop = former topmost value of stack
and this value has been removed from the stack }
function IsEmpty: Boolean;
{ This function returns TRUE if and only if the stack is empty.
pre: -
post: IsEmpty = TRUE <==> stack is empty}
implementation
var
snpTop: TStackNodePtr = NIL;
procedure Push (iNewValue: Integer);
var
snpNewNode: TStackNodePtr;
begin { Push }
New (snpNewNode); { may raise EOutOfMemory exception }
snpNewNode^.iValue := iNewValue;
snpNewNode^.snpNext := snpTop;
snpTop := snpNewNode
end { Push };
function Pop: Integer;
var
snpOldNode: TStackNodePtr;
begin { Pop }
snpOldNode := snpTop;
snpTop := snpTop^.snpNext;
result := snpOldNode^.iValue;
Dispose (snpOldNode)
end { Pop };
function IsEmpty: Boolean;
begin { IsEmpty }
result := snpTop = NIL
end { IsEmpty };
end.
Dali neko ima neki slican primjer?
Unaprijed hvala.