Skip to content

Commit a879d00

Browse files
committed
Finished stacks questions and tests passing, updated methods
1 parent 6dcf176 commit a879d00

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

lib/queue.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@ def initialize
66
end
77

88
def enqueue(element)
9+
#Circular buffer method
910
#Check if queue is empty
1011
if @front == -1 && @back == -1
1112
@front = 0
1213
@back = 0
1314
end
15+
1416
#Check if queue is full
1517
if ((@back + 1) % @store.length) == @front
16-
raise ArgumentError.new("Error: queue is full")
18+
raise ArgumentError.new("Queue is full!")
1719
end
1820

1921
@store[@back] = element
20-
#Make it wrap around when it reaches the end & do the modulo to not go over the end of the array
22+
#Make it wrap around when it reaches the end & use modulo to not go over the end
2123
@back = (@back + 1) % @store.length
2224
end
2325

2426
def dequeue
25-
# when queue is empty
2627
if @front == @back
27-
raise ArgumentError.new("Error: queue is empty")
28+
raise ArgumentError.new("Queue is empty!")
2829
end
2930

31+
#Store the current first element in a temp
3032
temp = @store[@front]
33+
#Make the front element nil
3134
@store[@front] = nil
35+
3236
@front = (@front + 1) % @store.length
3337

3438
return temp

lib/stack.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1+
require_relative './linked_list.rb'
2+
13
class Stack
24
def initialize
3-
@store = Array.new(10)
4-
@front = -1
5-
@back = -1
5+
@store = LinkedList.new
66
end
77

88
def push(element)
9-
#if top position is the last of position of stack, means stack is full .
10-
if @front == @store.length - 1
11-
raise ArgumentError.new("Stack is full, overflow condition!")
12-
else
13-
#Increment top position
14-
@front = @front + 1;
15-
#Insert element on the incremented position
16-
@store[@front] = element
17-
end
9+
@store.add_last(element)
10+
return @store
1811
end
1912

2013
def pop
21-
raise NotImplementedError, "Not yet implemented"
14+
return @store.remove_last if @store.get_first != nil
2215
end
2316

2417
def empty?
25-
raise NotImplementedError, "Not yet implemented"
18+
if @store.get_first == nil
19+
return true
20+
end
2621
end
2722

2823
def to_s

test/stack_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
end
1717

1818
it "pushes multiple somethings onto a Stack" do
19-
skip
2019
s = Stack.new
2120
s.push(10)
2221
s.push(20)
@@ -25,13 +24,11 @@
2524
end
2625

2726
it "starts the stack empty" do
28-
skip
2927
s = Stack.new
3028
s.empty?.must_equal true
3129
end
3230

3331
it "removes something from the stack" do
34-
skip
3532
s = Stack.new
3633
s.push(5)
3734
removed = s.pop
@@ -40,7 +37,6 @@
4037
end
4138

4239
it "removes the right something (LIFO)" do
43-
skip
4440
s = Stack.new
4541
s.push(5)
4642
s.push(3)

0 commit comments

Comments
 (0)