-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQueue.Mod
More file actions
83 lines (69 loc) · 2.07 KB
/
Copy pathQueue.Mod
File metadata and controls
83 lines (69 loc) · 2.07 KB
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
(**
Queue.Mod - A FIFO (First In, First Out) queue implementation.
Provides classical queue operations with clear semantics using LinkedList as underlying storage.
Copyright (C) 2025
Released under The 3-Clause BSD License.
*)
MODULE Queue;
IMPORT LinkedList, Collections;
TYPE
(** Opaque pointer to a Queue *)
Queue* = POINTER TO QueueDesc;
QueueDesc = RECORD
list: LinkedList.List
END;
(** Constructor: Allocate and initialize a new queue. *)
PROCEDURE New*(): Queue;
VAR queue: Queue;
BEGIN
NEW(queue);
queue.list := LinkedList.New();
RETURN queue
END New;
(** Destructor: Free the queue. *)
PROCEDURE Free*(VAR queue: Queue);
BEGIN
IF queue # NIL THEN
LinkedList.Free(queue.list);
queue := NIL
END
END Free;
(** Enqueue an item to the rear of the queue. *)
PROCEDURE Enqueue*(queue: Queue; item: Collections.ItemPtr);
BEGIN
LinkedList.Append(queue.list, item)
END Enqueue;
(** Dequeue and return the front item from the queue. *)
PROCEDURE Dequeue*(queue: Queue; VAR result: Collections.ItemPtr);
BEGIN
LinkedList.RemoveFirst(queue.list, result)
END Dequeue;
(** Peek at the front item without removing it. *)
PROCEDURE Front*(queue: Queue; VAR result: Collections.ItemPtr): BOOLEAN;
VAR success: BOOLEAN;
BEGIN
success := LinkedList.GetAt(queue.list, 0, result);
IF ~success THEN result := NIL END;
RETURN success
END Front;
(** Get the number of items in the queue. *)
PROCEDURE Count*(queue: Queue): INTEGER;
BEGIN
RETURN LinkedList.Count(queue.list)
END Count;
(** Check if the queue is empty. *)
PROCEDURE IsEmpty*(queue: Queue): BOOLEAN;
BEGIN
RETURN LinkedList.IsEmpty(queue.list)
END IsEmpty;
(** Clear all items from the queue. *)
PROCEDURE Clear*(queue: Queue);
BEGIN
LinkedList.Clear(queue.list)
END Clear;
(** Apply a visitor procedure to each item in the queue (front to rear order). *)
PROCEDURE Foreach*(queue: Queue; visitor: Collections.VisitProc; VAR state: Collections.VisitorState);
BEGIN
LinkedList.Foreach(queue.list, visitor, state)
END Foreach;
END Queue.