-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacks.pas
executable file
·78 lines (67 loc) · 1.98 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
unit stacks;
interface
const
maxBoardSize = 8;
type
TCell = (cQueen, cUnderAttack, cFree);
TBoard = array[1..maxBoardSize, 1..maxBoardSize] of TCell;
PStack= ^TStack;
TStack = record
x: byte;
CurrAbsBoardPos: integer; //òåêóùåå ïîëîæåíèå ôåðçÿ â ñòðîêå ïðèìåíèòåëüíî ê îòðèñîâêå
board: TBoard;
next: PStack;
end;
procedure CreateStack (var top:pStack);
procedure ClearStack (var top: PStack);
procedure PushStack (var top: pStack; x: byte; board: TBoard; CurrAbsBoardPos: integer);
procedure PopStack (var top: pStack; var x: byte; var board: TBoard; var CurrAbsBoardPos: integer);
//function StackElements (var top: pStack): integer;
implementation
procedure CreateStack (var top: pStack);
{ñîçäàíèå ïóñòîãî ñòåêà}
begin
if top <> nil then
ClearStack (top)
else
top:= nil;
end;
procedure PushStack (var top: pStack; x: byte; board: TBoard; CurrAbsBoardPos: integer);
{äîáàâëåíèå ýëåìåíòà â âåðõ ñòåêà}
var
temp: pStack;
begin
new (temp); {âûäåëÿåì ïàìÿòü äëÿ íîâîãî ýëåìåíòà}
temp^.x:= x; {ïðèñâàèâàåì ýëåìåíòó èíôîðìàöèþ}
temp^.board:= board;
temp^.CurrAbsBoardPos:= CurrAbsBoardPos;
temp^.next:= top; {"ñòàâèì" íîâûé ýëåìåíò â âåðõ ñòåêà}
top:= temp; {ñîõðàíÿåì ñòåê ñ íîâûì ýëåìåíòîì}
end;
procedure PopStack (var top: pStack; var x: byte; var board: TBoard; var CurrAbsBoardPos: integer);
{èçúÿòèå ýëåìåíòà èç âåðøèíû ñòåêà}
var
temp: pStack;
begin
if top <> nil then {åñëè ñòåê íå ïóñò, òî}
begin
temp:= top^.next; {ñîõðàíÿåì ñòåê, íà÷èíàÿ ñî âòîðîãî ýëåìåíòà îò âåðøèíû}
x:= top^.x; {ñ÷èòûâàåì èíôîðìàöèþ}
CurrAbsBoardPos:= top^.CurrAbsBoardPos;
board:= top^.board;
dispose (top); {î÷èùàåì ïàìÿòü îò ïåðâîãî ýëåìåíòà}
top:= temp; {çàïèñûâàåì íîâûé ñòåê â top}
end;
end;
procedure ClearStack (var top: PStack);
var
temp: PStack;
begin
while top <> nil do
begin
temp:= top;
top:= top^.next;
dispose (temp);
end;
end;
end.