-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_queue.pl
55 lines (43 loc) · 1.44 KB
/
stack_queue.pl
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
% A digression based on chapter 2 of O'Keefe dealing with implementing
% stacks and queues in Prolog. Related to the problem of search strategies
:- dynamic stack/1, queue/1.
% Creating an empty stack
stack([]).
% Implementing the two stack operations: push and pop
push(Item, stack(S)) :-
stack(S),
append([Item], S, NewS),
retract(stack(S)),
assertz(stack(NewS)).
pop(Item, stack(S)) :-
stack([Item|NewS]),
retract(stack(S)),
assertz(stack(NewS)).
% Creating an empty queue
queue([]).
% Implementing the two queue operations: enqueue and dequeue
enqueue(Item, queue(Q)) :-
queue(Q),
append(Q, [Item], NewQ),
retract(queue(Q)),
assertz(queue(NewQ)).
dequeue(Item, queue(Q)) :-
queue([Item|NewQ]),
retract(queue(Q)),
assertz(queue(NewQ)).
% Implementing the same data type (a queue) using two lists
empty_queue([]/[]).
% Enqueuing here means prepending to the back list
enqueue(Item, Front/Back, Front/[Item|Back]).
% Dequeuing here means popping the first item from the front list.
% If the front list is empty: we reverse the back list, return the first element from it
% and move the rest to the front adding a new empty back list
dequeue(Item, [Item|Front]/Back, Front/Back).
dequeue(Item, []/Back, Front/[]) :-
reverse(Back, [Item|Front]).
% Implementing a queue using a difference list
empty_q(L-L).
% Enqueuing
enqueue_q(Item, L-[Item|Z], L-Z).
% Dequeuing
dequeue_q(Item, [Item|T]-Z, T-Z).