-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.rb
46 lines (37 loc) · 833 Bytes
/
Queue.rb
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
#Implementing Queue (FIFO - First-In-First-Out)
# Using a SinglyLinkedList to keep track of the internal workings of Queue
load 'SinglyLinkedList.rb'
class Queue
def initialize
@list = SinglyLinkedList.new
end
#enqueue method adds item to the end of the queue. alias is <push>
def enqueue(number)
@list.push(number)
end
#alias for enqueue method
def push(number)
@list.push(number)
end
#dequeue method removes item from the queue alias is <pop>
def dequeue
return @list.shift.value
end
#alias for dequeue method
def pop
return @list.shift.value
end
end
queue = Queue.new
queue.enqueue(3)
queue.enqueue(5)
p queue.dequeue
#=> 3
queue.enqueue(2)
queue.enqueue(7)
p queue.dequeue
#=>5
p queue.dequeue
#=>2
p queue.dequeue
#=> 7