-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueCommand.pas
171 lines (155 loc) · 4.64 KB
/
QueueCommand.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
unit QueueCommand;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DataTypes;
// ïðîöåäóðà ñîçäàíèÿ î÷åðåäè
procedure CreateQueue(var Work: TPointerQueue);
// ïðîöåäóðà ñîçäàíèÿ íîâîãî óçëà
procedure makeQueue(var Work: TQueue; x: TItemQ);
// ôóíêöèÿ âîçâðàùàåò ÈÑÒÈÍÓ åñëè î÷åðåäü ïóñòàÿ, è ËÎÆÜ â ïðîòèâíîì ñëó÷àå
function isEmptyQueue(Work: TPointerQueue): boolean;
// ïðîöåäóðà äîáîâëåíèå óçëà â î÷åðåäü
procedure InsertQueue(var Work: TPointerQueue; x: TItemQ);
// ïðîöåäóðà óäàëåíèÿ óçëà èç î÷åðåäè
procedure DeleteQueue(var Work: TPointerQueue; var x: TItemQ);
// ïðîöåäóðà ïå÷àòè ñîäåðæèìîãî î÷åðåäè
// ïðîöåäóðà ñîçäàþùàÿ êîïèþ î÷åðåäè
procedure CopyQueue(var SourceQueue: TPointerQueue; var DuplicateQueue: TPointerQueue);
// ïðîöåäóðà î÷èñòêà î÷åðåäè
Procedure ClearQueue(var work: TPointerQueue);
//ôóíêöèÿ ïîäñ÷¸òà êîë-âà ýë-â â î÷åðåäè
function CountElQueue(work: TPointerQueue): byte;
implementation
// ïðîöåäóðà ñîçäàíèÿ î÷åðåäè
procedure CreateQueue(var Work: TPointerQueue);
begin
// îáíóëÿåì ññûëêó íà ãîëîâó î÷åðåäè
Work.Head:=nil;
// îáíóëÿåì ññûëêó íà õâîñò î÷åðåäè
Work.Tail:=nil;
end;
// ïðîöåäóðà ñîçäàíèÿ íîâîãî óçëà
procedure makeQueue(var Work: TQueue; x: TItemQ);
begin
// âûäåëÿåì ïàìÿòü ïîä íîâûé óçåë
New(Work);
// çàïèñûâàåì èíôîðìàöèþ â ïîëå óçëà
Work^.Data:=x;
// îáíóëÿåì ññûëêó
Work^.Next:=nil;
end;
// ôóíêöèÿ âîçâðàùàåò ÈÑÒÈÍÓ åñëè î÷åðåäü ïóñòàÿ, è ËÎÆÜ â ïðîòèâíîì ñëó÷àå
function isEmptyQueue(Work: TPointerQueue): boolean;
begin
Result:=Work.Head=nil;
end;
// ïðîöåäóðà äîáîâëåíèå óçëà â î÷åðåäü
procedure InsertQueue(var Work: TPointerQueue; x: TItemQ);
var Work2: TQueue;
begin
// ñîçäàåì íîâûé óçåë
makeQueue(Work2,x);
// ïðîâåðàåì, ïóñòàÿ ëè î÷åðåäü
if (isEmptyQueue(Work)) then
begin
// åñëè î÷åðåäü ïóñòàÿ òî
// ãîëîâó è õâîñò ññûëàåì íà íîâûé, âûøå ñîçäàííûé óçåë
Work.Head:=Work2;
Work.Tail:=Work2;
end
else
begin
// åñëè î÷åðåäü íå ïóñòàÿ òî
// ïðèâÿçûâàåì ýòîò óçåë ê õâîñòó î÷åðåäè
// ïðèâÿçûâàåì íîâûé óçåë ê î÷åðåäè
Work.Tail^.Next:=Work2;
// ñìåùàåì óêàçàòåëü íà íîâûé õâîñò î÷åðåäè
Work.Tail:=Work2;
end;
end;
{procedure InsertQueueAscending(var Work: TPointerQueue; x: TItemQ);
var TempWork, workPred: TQueue;
begin
empWork:=Work.head;
while (x<>nil) do
begin
workPred:=tempWork;
if StrToInt(x^.Edit.Text)>StrToInt(TempWork.data^.edit.text) then
TempWork:=TempWork^.Next
else
begin
if TempWork= work.Head then
TempWork^.Next:=work;
work:=TempWork;
end;
end;
end; }
// ïðîöåäóðà óäàëåíèÿ óçëà èç î÷åðåäè
procedure DeleteQueue(var Work: TPointerQueue; var x: TItemQ);
var Work2: TQueue;
begin
if not(isEmptyQueue(Work)) then
begin
{ // åñëè â î÷åðåäè òîëüêî 1 ýëåìåíò
// î÷èùàåì õâîñò
if work.head=work.tail then
work.tail:=nil; }
// åñëè î÷åðåäü íå ïóñòàÿ
// çàïîìèíàì óäàëÿåìûé óçåë
Work2:=Work.Head;
// ñìåùàåì óêàçàòåëü íà íîâóþ ãîëîâó î÷åðåäè
Work.Head:=Work.Head^.Next;
// îòâÿçûâàåì óäàëÿåìûé óçåë îò î÷åðåäè
Work2^.Next:=nil;
// çàïîìèíàåì èíôîðìàöèþ èç óäîëÿåìîãî óçëà
x:=Work2^.Data;
// îñâîáîæäàåì ïàìÿòü çàíèìàåìàþ óäàëÿåìûì óçëîì
Dispose(Work2);
end
else
begin
// åñëè î÷åðåäü ïóñòàÿ
// î÷èùàåì óêàçàòåëè íà ãîëîâó è õâîñò
Work.Head:=nil;
Work.Tail:=nil;
end;
end;
// ïðîöåäóðà ñîçäàþùàÿ êîïèþ î÷åðåäè
procedure CopyQueue(var SourceQueue: TPointerQueue; var DuplicateQueue: TPointerQueue);
var TempQueue: TPointerQueue; // âðåìåííàÿ î÷åðåäü
temp: TItemQ;
begin
// âðåìåííîé î÷åðåäè ïðèñâàèâàåì èñõîäíóþ î÷åðåäü
TempQueue:=SourceQueue;
// èíèöèàëèçèðóåì äâå î÷åðåäè â êîòîðûõ áóäóò õðàíèòñÿ èñõîäíûé è êîïèÿ î÷åðåäè
CreateQueue(SourceQueue);
CreateQueue(DuplicateQueue);
// ïðîõîäèì ïî î÷åðåäè è ñîäåðæèìîå ïîìåùàåì â äâå î÷åðåäè
while not(isEmptyQueue(TempQueue)) do
begin
// óäàëÿåì óçåë èç î÷åðåäè
DeleteQueue(TempQueue,temp);
// ïîìåùàåì óäàëåííûé ýëåìåíò â äâå î÷åðåäè
InsertQueue(SourceQueue,temp);
InsertQueue(DuplicateQueue, temp);
end;
end;
Procedure ClearQueue(var work: TPointerQueue);
var temp:TItemQ;
begin
while not(isEmptyQueue(work)) do
DeleteQueue(work,temp);
end;
function CountElQueue(work: TPointerQueue): byte;
var tempNode: TQueue;
begin
result:=0;
tempNode:=work.Head;
while tempNode<>nil do
begin
inc(result);
tempNode:=tempNode^.Next;
end;
end;
end.