-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.jack
70 lines (60 loc) · 1.64 KB
/
Queue.jack
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
// Copyright (c) 2018, Bill Mei
// A simple FIFO queue, implemented as a doubly-linked list
// The values stored can only be integers.
class Queue {
field Node head, tail;
constructor Queue new() {
let head = null;
let tail = null;
return this;
}
// The queue is empty if both head and tail don't point to a Node
method boolean isEmpty() {
return (head = null) & (tail = null);
}
// Adds a new element to the front of the queue
method void enqueue(int value) {
var Node node;
let node = Node.new(value);
do node.setNext(head);
// If there was a next node, link it's previous back to the new node
if (head) {
do head.setPrev(node);
} else {
// If this is the first element, point the tail to it too.
let tail = node;
}
let head = node;
return;
}
// Removes the last element of the queue and returns it
method int dequeue() {
var Node lastNode;
var int value;
let lastNode = tail;
if (tail) {
let value = tail.getValue();
do tail.dispose();
let tail = lastNode.getPrev();
// If this is the last node, also dereference head.
if (tail = head) {
let head = null;
}
return value;
} else {
// The linked list was empty
return null;
}
}
method void dispose() {
var Node runner, lastNode;
let runner = head;
while (runner) {
let lastNode = runner;
let runner = lastNode.getNext();
do lastNode.dispose();
}
do Memory.deAlloc(this);
return;
}
}