-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuIntStack.pas
More file actions
87 lines (63 loc) · 1.76 KB
/
uIntStack.pas
File metadata and controls
87 lines (63 loc) · 1.76 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
unit uIntStack;
// This source is distributed under Apache 2.0
// Copyright (C) 2019-2021 Herbert M Sauro
// Author Contact Information:
// email: hsauro@gmail.com
// This is a lighweight integer stack. Implemented to ensure that
// the code can be used with Delphi versions that dodn't have
// generics
interface
Uses Classes, SysUtils;
const
MAX_STACK_ENTRIES = 100;
type
TStack = record
data : array of integer;
stackPtr : integer;
maxSize : integer;
end;
procedure create (var stack : TStack; n : integer);
procedure push (var stack : TStack; value : integer);
function pop (var stack : TStack) : integer;
function peek (var stack : TStack) : integer;
function getCount (var stack : TStack) : integer;
procedure clear (var stack : TStack);
function getSize (var stack : TStack) : integer;
implementation
procedure create (var stack : TStack; n : integer);
begin
stack.maxSize := n;
stack.stackPtr := -1;
setLength (stack.data, n);
end;
function getSize (var stack : TStack) : integer;
begin
result := stack.maxSize;
end;
procedure clear (var stack : TStack);
begin
stack.stackPtr := -1;
end;
function getCount (var stack : TStack) : integer;
begin
result := stack.stackPtr + 1;
end;
procedure push (var stack : TStack; value : integer);
begin
if stack.stackPtr = stack.maxSize then
raise Exception.Create('Integer stack overflow error.');
inc (stack.stackPtr);
stack.data[stack.stackPtr] := value;
end;
function pop (var stack : TStack) : integer;
begin
if stack.stackPtr = -1 then
raise Exception.Create('Integer stack underflow error.');
result := stack.data[stack.stackPtr];
dec (stack.stackPtr);
end;
function peek (var stack : TStack) : integer;
begin
result := stack.data[stack.stackPtr];
end;
end.