SRL Archive - Research & Development Lounge

Implementation an stack with using array in simba


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:

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.

Main

2012-07-02 06:13:11

Lolol nice :) I always though this was quite fun, thankfully pascal's array function are just such hacks that we don't need this lawl

But who knows maybe it'll be a interesting way to dynamically store walking data nor something :)

Janilabo

2012-07-02 06:20:11

Interesting stuff, Cynic!
Nice to see you keep contributing your stuff to the community. :p

masterBB

2012-07-02 06:23:53

Now create a linked list, the mother of all stacks! If you want to take that challenge, don't forget to set the interpreter to lape.

CynicRus

2012-07-02 08:07:36

So, just list simple in simba with pascal script interpreter-)))

type TListNode = record
name: string;
x,y: integer;
end;
type TList = record
elems: array of TListNode;
last: integer;
end;
//procedures for list
procedure make_null(var L: TList);
begin
L.last:=0
end; {make_null}

procedure insert(var L: TList; x: TListNode; i:integer);
var p: integer;
begin
SetLength(L.elems,Length(L.elems)+2);
if L.last = 0 then
begin
L.elems[L.last]:=x;
L.last:=L.last+1;
exit;
end;
for p:=L.last downto i do
L.elems[p+1]:=L.elems[p];
L.elems[i]:=x;
L.last:=L.last+1;
end; {insert}
function is_empty(var L: TList):boolean;
begin
if L.last=0 then result:=true else result:=false;
end; {is_empty}

function is_valid(var L: TList;i:integer):boolean;
begin
if (1<=i) and (i<=L.last) then result:= true
else result:= false;
end; {is_valid}
procedure delete(var L: TList; i:integer);
var p: integer;
begin
for p:=i to L.last-1 do
L.elems[p]:=L.elems[p+1];
L.last:=L.last-1;
end; {delete}
function is_present(var L: TList; x: TListNode):boolean;
var found:boolean;
p:integer;
begin
found:= false;
p:=1;
while not found and (p <= L.last) do
begin
if x = L.elems[p] then
found:=true;
p:= p+1
end;
result:=found;
end; {is_present}
{endlist}
function Randomstring(strLen: Integer): string;
var
str: string;
begin
str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ';
Result := '';
repeat
Result := Result + str[Random(Length(str)) + 1];
until (Length(Result) = strLen)
end;