Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSA Dojo queue exercises #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
queue passing
tmikeschu committed Nov 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 8510fe3251dac83f32b0851e493e284be00b4b0c
46 changes: 45 additions & 1 deletion queues/lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
class Queue
attr_reader :count

end
def initialize
@head = Node.new(:head)
@tail = Node.new(:tail, @head)
@count = 0
end

def enqueue(x)
@count += 1
node = Node.new(x)
node.right = @tail
left = @tail.left
left.right = node
@tail.left = node
end

def dequeue
@count -= 1
popped = @head.right
@head.right = popped.right
popped.val
end

def peek
@head.right.val
end

def last
@tail.left.val
end

def empty
@count == 0
end
end

class Node
attr_accessor :val, :left, :right

def initialize(val, left = nil, right = nil)
@val = val
@left = left
@right = right
end
end
36 changes: 32 additions & 4 deletions queues/test/queue_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest'
require 'minitest/autorun'
require './lib/queue.rb'
require "minitest"
require "minitest/autorun"
require "./lib/queue.rb"

class StackTest < Minitest::Test
def test_it_enqueues
@@ -21,4 +21,32 @@ def test_it_dequeues
assert_equal("C", queue.dequeue)
end

end
def test_it_peeks
queue = Queue.new
queue.enqueue("A")
queue.enqueue("B")
queue.enqueue("C")
assert_equal("A", queue.peek)
queue.dequeue
assert_equal("B", queue.peek)
end

def test_it_gets_last
queue = Queue.new
queue.enqueue("A")
queue.enqueue("B")
queue.enqueue("C")
assert_equal("C", queue.last)
queue.dequeue
assert_equal("C", queue.last)
end

def test_it_gets_empty
queue = Queue.new
assert_equal(true, queue.empty)
queue.enqueue("A")
assert_equal(false, queue.empty)
queue.dequeue
assert_equal(true, queue.empty)
end
end