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
66 lines (59 loc) · 1.13 KB
/
queue.rb
File metadata and controls
66 lines (59 loc) · 1.13 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
class Queue
def initialize
@store = Array.new(10)
@front = @rear = -1
@size = 0
@capacity = 10
end
def enqueue(element)
if @size == @capacity
return
else
if @front == -1
@front = 0
end
@rear = (@rear + 1) % @capacity
@store[@rear] = element
@size += 1
end
end
def dequeue
if empty?
return
elsif @front == @rear
removed_element = @store[@front]
@front = -1
@rear = -1
@size -= 1
return removed_element
else
removed_element = @store[@front]
@front = (@front + 1) % @capacity
@size -= 1
return removed_element
end
end
def front
raise NotImplementedError, "Not yet implemented"
end
def size
raise NotImplementedError, "Not yet implemented"
end
def empty?
if @size == 0
return true
else
return false
end
end
def to_s
if @size == 0
return [].to_s
elsif @front <= @rear
return @store[@front..@rear].to_s
else
new_array = @store[@front .. @capacity -1] + @store[0..@rear]
return new_array.to_s
end
end
end