-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.pas
85 lines (78 loc) · 1.55 KB
/
Queue.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
uses crt;
type
PNode = ^Node;
Node = record
data: integer;
next: PNode;
end;
Queue = record
head, tail: PNode;
end;
var
expr: string;
Q: Queue;
number, err: integer;
procedure PushTail(var Q: Queue; x: integer);
var NewNode: PNode;
begin
New(NewNode);
NewNode^.data := x;
NewNode^.next := nil;
if Q.tail <> nil then Q.tail^.next := NewNode;
Q.tail := NewNode;
if Q.head = nil then Q.head := Q.tail;
end;
function Pop (var Q: Queue): integer;
var top: PNode;
begin
if Q.head = nil then begin
result := MaxInt;
exit;
end;
top := Q.head;
Result := top^.data;
Q.head := top^.next;
if Q.head = nil then Q.tail := nil;
Dispose(top);
end;
function isEmpty(Q: Queue): Boolean;
begin
Result := (Q.head = nil);
end;
procedure show(Q: Queue);//Вывод данных из стека, начиная с нижнего элемента
var
i: integer;
begin
i := 0;
while Q.head <> nil do
begin
print(Q.head^.data);
Q.head := Q.head^.next;
i += 1;
end;
end;
begin
Q.tail:= nil;
Q.head:= nil;
while true do
begin
readln(expr);
if (expr[1] = '+') then
begin
delete(expr, 1, 1);
val(expr, number, err);
if (expr = '') then begin clrscr; continue; end; //Исключает случай, если введен просто плюс
pushtail(Q, number)
end
else if (expr[1] = '-') then
begin
pop(Q);
if isEmpty(Q) then
begin
writeln('Очередь пуста');
break;
end
end else break;
end;
show(Q);;
end.