aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/paulo-custodio/basic/ch-2.bas
blob: c1d8f62d5038d2b220b9590fd7a3c98d76956084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
' Challenge 095
'
' TASK #2 › Demo Stack
' Submitted by: Mohammad S Anwar
' Write a script to demonstrate Stack operations like below:
'
' push($n) - add $n to the stack
' pop() - remove the top element
' top() - get the top element
' min() - return the minimum element
'
' Example:
' my $stack = Stack->new;
' $stack->push(2);
' $stack->push(-1);
' $stack->push(0);
' $stack->pop;       # removes 0
' print $stack->top; # prints -1
' $stack->push(0);
' print $stack->min; # prints -1

dim shared stack() as integer

sub push(n as integer)
    redim preserve stack(ubound(stack)+1)
    stack(ubound(stack)) = n
end sub

sub pop()
    redim preserve stack(ubound(stack)-1)
end sub

function top() as integer
    top = stack(ubound(stack))
end function

function min() as integer
    dim i as integer, m as integer
    m = stack(lbound(stack))
    for i=lbound(stack)+1 to ubound(stack)
        if stack(i) < m then m = stack(i)
    next i
    min = m
end function


                        ' my $stack = Stack->new;
push(2)                 ' $stack->push(2);
push(-1)                ' $stack->push(-1);
push(0)                 ' $stack->push(0);
pop()                   ' $stack->pop;       # removes 0
print trim(str(top()))  ' print $stack->top; # prints -1
push(0)                 ' $stack->push(0);
print trim(str(min()))  ' print $stack->min; # prints -1