-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStack.Mod
More file actions
88 lines (74 loc) · 2.19 KB
/
Copy pathStack.Mod
File metadata and controls
88 lines (74 loc) · 2.19 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
84
85
86
87
88
(**
Stack.Mod - A LIFO (Last In, First Out) stack implementation.
Provides classical stack operations with clear semantics using LinkedList as underlying storage.
Copyright (C) 2025
Released under The 3-Clause BSD License.
*)
MODULE Stack;
IMPORT LinkedList, Collections;
TYPE
(** Opaque pointer to a Stack *)
Stack* = POINTER TO StackDesc;
StackDesc = RECORD
list: LinkedList.List
END;
(** Constructor: Allocate and initialize a new stack. *)
PROCEDURE New*(): Stack;
VAR stack: Stack;
BEGIN
NEW(stack);
stack.list := LinkedList.New();
RETURN stack
END New;
(** Destructor: Free the stack. *)
PROCEDURE Free*(VAR stack: Stack);
BEGIN
IF stack # NIL THEN
LinkedList.Free(stack.list);
stack := NIL
END
END Free;
(** Push an item onto the stack. *)
PROCEDURE Push*(stack: Stack; item: Collections.ItemPtr);
BEGIN
IF ~LinkedList.InsertAt(stack.list, 0, item) THEN
(* Handle error - should not happen for valid stack *)
END
END Push;
(** Pop and return the top item from the stack. *)
PROCEDURE Pop*(stack: Stack; VAR result: Collections.ItemPtr);
BEGIN
LinkedList.RemoveFirst(stack.list, result)
END Pop;
(** Peek at the top item without removing it. Returns TRUE if successful *)
PROCEDURE Top*(stack: Stack; VAR result: Collections.ItemPtr): BOOLEAN;
VAR success: BOOLEAN;
BEGIN
success := LinkedList.GetAt(stack.list, 0, result);
RETURN success
END Top;
(** Return the number of items in the stack. *)
PROCEDURE Count*(stack: Stack): INTEGER;
VAR result: INTEGER;
BEGIN
result := LinkedList.Count(stack.list);
RETURN result
END Count;
(** Test if the stack is empty. *)
PROCEDURE IsEmpty*(stack: Stack): BOOLEAN;
VAR result: BOOLEAN;
BEGIN
result := LinkedList.IsEmpty(stack.list);
RETURN result
END IsEmpty;
(** Clear removes all elements from the stack. *)
PROCEDURE Clear*(stack: Stack);
BEGIN
LinkedList.Clear(stack.list)
END Clear;
(** Apply a procedure to each element in the stack from top to bottom. *)
PROCEDURE Foreach*(stack: Stack; visit: Collections.VisitProc; VAR state: Collections.VisitorState);
BEGIN
LinkedList.Foreach(stack.list, visit, state)
END Foreach;
END Stack.