-
Notifications
You must be signed in to change notification settings - Fork 0
/
STACKS.PAS
134 lines (115 loc) · 2.64 KB
/
STACKS.PAS
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Unit Stacks;
Interface
Uses
MyObj;
Const
StackLimit = 10;
Type
LStack = Object
constructor init;
destructor done; virtual;
function pop : longint; virtual;
procedure push(x : longint); virtual;
function empty : boolean; virtual;
function count : longint; virtual;
procedure clear; virtual;
private
Top : Word;
Data : Array[0..StackLimit] of Longint;
End;
OStack = Object
constructor init;
destructor done; virtual;
function top : PBaseObject; virtual;
function pop : PBaseObject; virtual;
procedure push(x : PBaseObject); virtual;
function empty : boolean; virtual;
function count : longint; virtual;
procedure clear; virtual;
private
TopP : Word;
Data : Array[0..StackLimit] of PBaseObject;
End;
Implementation
constructor LStack.Init;
begin
Top := 0;
Data[0] := 0;
end;
function LStack.Pop : Longint;
begin
Pop := Data[Top];
If Top > 0 then Dec(Top);
end;
procedure LStack.Push(x : longint);
begin
If Top < StackLimit then inc(Top);
Data[Top] := x;
end;
function LStack.Empty : Boolean;
begin
Empty := Top = 0;
end;
function LStack.Count : Longint;
begin
Count := Top;
end;
procedure LStack.Clear;
begin
Top := 0;
end;
destructor LStack.Done;
begin
Self.Clear;
end;
constructor OStack.init;
begin
fillchar(Data,SizeOf(Data),#0);
TopP := 0;
end;
destructor OStack.done;
begin
self.clear;
end;
function OStack.top : PBaseObject;
begin
If TopP <> 0 then Top := Data[TopP]
else Top := Nil;
end;
function OStack.pop : PBaseObject;
begin
If TopP <> 0 then
begin
pop := Data[TopP];
Dec(TopP);
end
else
pop := Nil;
end;
procedure OStack.push(x : PBaseObject);
begin
If TopP < StackLimit then
begin
Inc(TopP);
Data[TopP] := X;
end;
end;
function OStack.empty : boolean;
begin
empty := TopP = 0;
end;
function OStack.count : longint;
begin
Count := TopP;
end;
procedure OStack.clear;
begin
while topp > 0 do
begin
If Data[TopP] <> nil then
Data[TopP]^.Done;
Dec(TopP);
end;
end;
begin
end.