forked from AdaGold/stacks-queues
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathqueue.rb
More file actions
48 lines (41 loc) · 898 Bytes
/
queue.rb
File metadata and controls
48 lines (41 loc) · 898 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
45
46
47
48
class Queue
QUEUE_SIZE = 20
def initialize
# @store = ..
@store = Array.new(QUEUE_SIZE)
@front = @rear = -1
end
def enqueue(element)
if @front == -1 # Q is empty
@rear = 1
@front = 0
@store[@front] = element
elsif @front == @rear
raise Error, "Q Full"
else # not empty
new_rear = (@rear + 1) % QUEUE_SIZE
@store[@rear] = element
@rear = new_rear
end
end
def dequeue
raise Error, "Q Empty" if @front == -1
value = @store[@front]
@store[@front] = nil
@front = (@front + 1) % QUEUE_SIZE
@front = @rear = -1 if @front == @rear
return value
end
def front
raise NotImplementedError, "Not yet implemented"
end
def size
raise NotImplementedError, "Not yet implemented"
end
def empty?
return @rear == -1
end
def to_s
return @store[@front...@rear].to_s
end
end