You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm a simple FIFO queue i.e., first in first out structure. I support basic collection protocol and in addition enqueue and dequeue as in Scala.
3
-
My basic support of collection API should be reviewd and probably improved (should check atomic queue protocol).
4
-
5
-
2
+
I'm a simple FIFO queue i.e., first in first out structure. I support basic collection protocol with efficient O(1) add and remove operations using a singly linked list.
6
3
"
7
4
Class {
8
5
#name : #CTQueue,
9
6
#superclass : #Object,
10
7
#instVars : [
11
-
'elements'
8
+
'head',
9
+
'tail',
10
+
'size'
12
11
],
13
12
#category : #'Containers-Queue'
14
13
}
15
14
16
15
{ #category : #adding }
17
16
CTQueue>>add: anElement [
18
-
"Add an element to the receiver. Note that the addition makes sure that when iterating over the receiver added first element are accessed first."
19
-
20
-
elements addLast: anElement.
17
+
"Add an element to the end of the queue (FIFO order)."
18
+
| newNode |
19
+
newNode :=CTQueueNodenewvalue: anElement.
20
+
head ifNil: [
21
+
head := newNode.
22
+
tail := newNode.
23
+
] ifNotNil: [
24
+
tail next: newNode.
25
+
tail := newNode.
26
+
].
27
+
size := size +1.
21
28
^ anElement
22
29
]
23
30
24
31
{ #category : #adding }
25
32
CTQueue>>addAll: aCollection [
26
-
"Add the elements contained in the argument to the receiver. Note that the addition makes sure that when iterating over the receiver added first element are accessed first."
27
-
28
-
elements addAllLast: aCollection.
33
+
"Add all elements from aCollection to the end of the queue."
34
+
aCollection do: [ :each | selfadd: each ].
29
35
^ aCollection
30
36
]
31
37
32
38
{ #category : #iterating }
33
39
CTQueue>>do: aBlock [
34
-
"iterates the elements of the receiver starting first by first added elements."
35
-
36
-
elements do: aBlock
40
+
"Iterate over elements in FIFO order."
41
+
| current |
42
+
current := head.
43
+
[ current notNil ] whileTrue: [
44
+
aBlock value: current value.
45
+
current := current next.
46
+
]
37
47
]
38
48
39
49
{ #category : #testing }
40
50
CTQueue>>includes: anElement [
41
-
42
-
^ elements includes: anElement
51
+
"Check if anElement exists in the queue."
52
+
| current |
53
+
current := head.
54
+
[ current notNil ] whileTrue: [
55
+
current value = anElement ifTrue: [ ^true ].
56
+
current := current next.
57
+
].
58
+
^false
43
59
]
44
60
45
61
{ #category : #initialization }
46
62
CTQueue>> initialize [
63
+
"Initialize an empty queue."
47
64
super initialize.
48
-
elements :=OrderedCollectionnew.
65
+
head :=nil.
66
+
tail :=nil.
67
+
size :=0.
49
68
]
50
69
51
70
{ #category : #testing }
52
71
CTQueue>> isEmpty [
53
-
54
-
^elements isEmpty
72
+
"Return true if the queue is empty."
73
+
^head isNil
55
74
]
56
75
57
-
{ #category : #removing }
58
-
CTQueue>> remove [
59
-
"Return the older element of the receiver.."
60
-
61
-
^ elements ifEmpty: [ nil ] ifNotEmpty: [ elements removeFirst ].
76
+
{ #category : #accessing }
77
+
CTQueue>> peek [
78
+
"Return the front element without removing it, or nil if empty."
79
+
^ head ifNil: [ nil ] ifNotNil: [ head value ]
62
80
]
63
81
64
82
{ #category : #removing }
65
-
CTQueue>>removeIfNone: aBlock [
66
-
"Return the older element of the receiver.."
67
-
elements ifEmpty: [ ^ aBlock value ].
68
-
^ elements removeFirst
83
+
CTQueue>> poll [
84
+
"Return and remove the front element, or nil if empty."
85
+
^self remove
69
86
]
70
87
71
88
{ #category : #removing }
72
-
CTQueue>> poll [
73
-
"Returns and removes the front element, or nil if empty."
74
-
^ elements ifEmpty: [ nil ] ifNotEmpty: [ elements removeFirst ].
89
+
CTQueue>> remove [
90
+
"Return and remove the oldest element, or nil if empty."
91
+
| value |
92
+
head ifNil: [ ^nil ].
93
+
value := head value.
94
+
head := head next.
95
+
head ifNil: [ tail :=nil ].
96
+
size := size -1.
97
+
^ value
75
98
]
76
99
77
-
{ #category : #accessing }
78
-
CTQueue>> peek [
79
-
^ elements ifEmpty: [ nil ] ifNotEmpty: [ elements first ].
100
+
{ #category : #removing }
101
+
CTQueue>>removeIfNone: aBlock [
102
+
"Return and remove the oldest element, or evaluate aBlock if empty."
0 commit comments