Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be added to .gitignore

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Local File",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/main.rb"
},
null
]
}
50 changes: 44 additions & 6 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
class Queue

MAX_SIZE = 20
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@queue = []
@size = 20
@front = -1
@rear = -1

end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"

if ((@front == 0 && @rear == @size - 1) || (@rear == @front && @front != -1)
raise "queue is full"
elsif (@front == -1)
@front = @rear = 0
@queue[@rear] = element
else
@rear += 1
@queue[@rear] = element
Comment on lines +20 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lets the rear wrap around

Suggested change
@rear += 1
@queue[@rear] = element
@rear = (@rear + 1) % @store.length
@queue[@rear] = element

end

end

def dequeue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Not yet implemented"
if (@front == -1)
raise "queue is empty"
end

result = @queue[@front]
@queue[@front] = nil

#if the queue is now empty
if (@front == @rear)
@front = -1
@rear = -1
elsif (@front == @size-1)
@front = 0
else
@front = @front + 1
end
Comment on lines +38 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will let front wrap around without having to make the elif-else.

Suggested change
elsif (@front == @size-1)
@front = 0
else
@front = @front + 1
end
else
@front = (@front + 1) % @store.length
end


return result
end

def front
Expand All @@ -22,10 +53,17 @@ def size
end

def empty?
raise NotImplementedError, "Not yet implemented"
@front == -1
end

def to_s
return @store.to_s
list = []
index = @front
while index <= @rear
list.push(@queue[index])
index = (index + 1) % @store.length
end

return list.to_s
end
end
12 changes: 8 additions & 4 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
class Stack

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store.add_first(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
return nil if self.empty?

item = @store.remove_first

return item
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @store.empty?
end

def to_s
Expand Down