-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.pas
94 lines (81 loc) · 1.56 KB
/
Stack.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
unit Stack;
interface
type
list = ^node;
node = record
data: integer;
next: list;
end;
procedure Push(data: integer; var stack: list);
procedure PushTwo(data1, data2: integer; var stack: list);
function Pop(var stack: list): integer;
function Top(stack: list): integer;
procedure Show(stack: list);
function Empty(stack: list): boolean;
function Count(stack: list): integer;
procedure Clear(var stack: list);
procedure Delete(var stack: list);
implementation
procedure Push(data: integer; var stack: list);
var
temp: list;
begin
New(temp);
temp^.data := data;
temp^.next := stack;
stack := temp;
end;
procedure PushTwo(data1, data2: integer; var stack: list);
begin
Push(data2, stack);
Push(data1, stack);
end;
function Pop(var stack: list): integer;
var
temp: list;
begin
result := stack^.data;
temp := stack;
stack := stack^.next;
Dispose(temp);
end;
function Top(stack: list): integer;
begin
result := stack^.data;
end;
procedure Show(stack: list);
begin
while (not Empty(stack)) do
begin
write(stack^.data, ' ');
stack := stack^.next;
end;
end;
function Empty(stack: list): boolean;
begin
if (stack = nil) then
result := true;
end;
function Count(stack: list): integer;
begin
if (not Empty(stack)) then
begin
while (not Empty(stack)) do
begin
stack := stack^.next;
Inc(Result);
end;
end else
Result := 0;
end;
procedure Clear(var stack: list);
begin
while (not Empty(stack)) do
Pop(stack);
end;
procedure Delete(var stack: list);
begin
Clear(stack);
Dispose(stack);
end;
end.