File tree Expand file tree Collapse file tree 3 files changed +17
-22
lines changed Expand file tree Collapse file tree 3 files changed +17
-22
lines changed Original file line number Diff line number Diff line change @@ -6,29 +6,33 @@ def initialize
6
6
end
7
7
8
8
def enqueue ( element )
9
+ #Circular buffer method
9
10
#Check if queue is empty
10
11
if @front == -1 && @back == -1
11
12
@front = 0
12
13
@back = 0
13
14
end
15
+
14
16
#Check if queue is full
15
17
if ( ( @back + 1 ) % @store . length ) == @front
16
- raise ArgumentError . new ( "Error: queue is full" )
18
+ raise ArgumentError . new ( "Queue is full! " )
17
19
end
18
20
19
21
@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
21
23
@back = ( @back + 1 ) % @store . length
22
24
end
23
25
24
26
def dequeue
25
- # when queue is empty
26
27
if @front == @back
27
- raise ArgumentError . new ( "Error: queue is empty" )
28
+ raise ArgumentError . new ( "Queue is empty! " )
28
29
end
29
30
31
+ #Store the current first element in a temp
30
32
temp = @store [ @front ]
33
+ #Make the front element nil
31
34
@store [ @front ] = nil
35
+
32
36
@front = ( @front + 1 ) % @store . length
33
37
34
38
return temp
Original file line number Diff line number Diff line change
1
+ require_relative './linked_list.rb'
2
+
1
3
class Stack
2
4
def initialize
3
- @store = Array . new ( 10 )
4
- @front = -1
5
- @back = -1
5
+ @store = LinkedList . new
6
6
end
7
7
8
8
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
18
11
end
19
12
20
13
def pop
21
- raise NotImplementedError , "Not yet implemented"
14
+ return @store . remove_last if @store . get_first != nil
22
15
end
23
16
24
17
def empty?
25
- raise NotImplementedError , "Not yet implemented"
18
+ if @store . get_first == nil
19
+ return true
20
+ end
26
21
end
27
22
28
23
def to_s
Original file line number Diff line number Diff line change 16
16
end
17
17
18
18
it "pushes multiple somethings onto a Stack" do
19
- skip
20
19
s = Stack . new
21
20
s . push ( 10 )
22
21
s . push ( 20 )
25
24
end
26
25
27
26
it "starts the stack empty" do
28
- skip
29
27
s = Stack . new
30
28
s . empty? . must_equal true
31
29
end
32
30
33
31
it "removes something from the stack" do
34
- skip
35
32
s = Stack . new
36
33
s . push ( 5 )
37
34
removed = s . pop
40
37
end
41
38
42
39
it "removes the right something (LIFO)" do
43
- skip
44
40
s = Stack . new
45
41
s . push ( 5 )
46
42
s . push ( 3 )
You can’t perform that action at this time.
0 commit comments