CynicRus
2012-07-02 03:57:21
Just simply implementation, i did it just for fun. I do not think that someone would use in practice, but nevertheless - it works.
Stack:
Test script:
Stack:
Type TElement = record//element in stack
color,dtm: integer;
end;
type TStack = record
elements: array of TElement;//elements in stack
size: Integer;//stack current size
end;
//Procedures for stack working
procedure stack_init(var st: TStack);
begin
st.size := 0;
end;
function stack_size(const st: TStack): Integer;
begin
result := st.size;
end;
function stack_empty(const st: TStack): Boolean;
begin
if (st.size = 0) then result:= true else result:=false;
end;
procedure stack_push(var st: TStack; const value: Telement);
begin
SetLength(st.elements,Length(st.elements)+1);
//move(value, st.elements[st.size], sizeof(value));
//WriteLn(ToStr(st.size));
// WriteLn(ToStr(length(st.elements)));
st.elements[st.size]:=value;
Inc(st.size);
end;
procedure stack_pop(var st: TStack; var value: Telement);
begin
if st.size > 0 then
begin
st.elements[st.size]:=value;
Dec(st.size);
end else WriteLn('Cannot pop element');
end;
//end working with stack
Test script:
var
CustomStack: TStack;
Procedure Input(count: integer);
var
e: TElement;
i: Integer;
begin
for i:=1 to count do
begin
e.color:=random(10);
e.dtm:=random(20);
stack_push(CustomStack,e)
end;
end;
Procedure Output();
var
i: integer;
begin
WriteLn('Array stack realisation in SIMBA');
for i := 1 to CustomStack.size-1 do
begin
WriteLn('Element color field');
WriteLn(ToStr(CustomStack.elements[i].color));
WriteLn('Element dtm field');
WriteLn(ToStr(CustomStack.elements[i].dtm));
end;
end;
begin
stack_init(CustomStack);
input(5);
output;
end.
Test output:
Compiled successfully in 16 ms.
Array stack realisation in SIMBA
Element color field
7
Element dtm field
0
Element color field
3
Element dtm field
12
Element color field
0
Element dtm field
6
Element color field
9
Element dtm field
13
Successfully executed.