-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathqueue.cr
More file actions
44 lines (35 loc) · 747 Bytes
/
queue.cr
File metadata and controls
44 lines (35 loc) · 747 Bytes
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
class Queue(T)
# The items in the queue.
@queue : Array(T)
# Creates a new empty queue.
def initialize
@queue = Array(T).new
end
# Pushes the given *item* onto the queue and returns the size of the queue.
def enqueue(item : T)
@queue << item
self.size
end
# Removes the first item in the queue (at index 0).
def dequeue : T
@queue.shift
end
# Returns the first item in the queue (at index 0).
def front : T
@queue[0]
end
# Returns the number of items in the queue.
def size : Int32
@queue.size
end
end
def queue_example
queue = Queue(Int32).new
queue.enqueue(4)
queue.enqueue(5)
queue.enqueue(9)
puts queue.dequeue
puts queue.size
puts queue.front
end
queue_example