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

completed classroom exercise #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions lib/Queue.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
class Queue
def initialize
@store_q = Array.new
end

def enqueue(element)
@store_q.push(element)
end

def dequeue
return @store_q.shift
end

def front
return @store_q.first
end

def size
return @store_q.length
end

def empty?
return size == 0
end

def to_s
return @store_q.to_s
end
end
11 changes: 11 additions & 0 deletions lib/Stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ def initialize
end

def push(element)
# @store << element
# @store.unshift(element)
@store.push(element)
#.unshift adds an element to the front of the array
#all three above do same thing
end

def pop
return @store.pop
end

def top
return @store.last
end

def size
return @store.length
end

def empty?
return size == 0
# return @store.length == 0
# basically both do the same thing
end

def to_s
Expand Down
14 changes: 7 additions & 7 deletions specs/test-queue-implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
end

it "adds something to an empty Queue" do
skip
# skip
q = Queue.new
q.enqueue(10)
q.to_s.must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
# skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -26,14 +26,14 @@
end

it "starts the size of a Queue at 0" do
skip
# skip
q = Queue.new
q.size.must_equal 0
q.empty?.must_equal true
end

it "removes something from the Queue" do
skip
# skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -43,7 +43,7 @@
end

it "removes the right something (LIFO)" do
skip
# skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -55,7 +55,7 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
# skip
q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -70,7 +70,7 @@
end

it "returns the front element in the Queue" do
skip
# skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
14 changes: 7 additions & 7 deletions specs/test-stack-implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
end

it "pushes something onto a empty Stack" do
skip
# skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
end

it "pushes multiple somethings onto a Stack" do
skip
# skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,14 +26,14 @@
end

it "starts the size of a Stack at 0" do
skip
# skip
s = Stack.new
s.size.must_equal 0
s.empty?.must_equal true
end

it "removes something from the stack" do
skip
# skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -43,7 +43,7 @@
end

it "removes the right something (LIFO)" do
skip
# skip
s = Stack.new
s.push(5)
s.push(3)
Expand All @@ -55,7 +55,7 @@
end

it "properly adjusts the size with pushing and poping" do
skip
# skip
s = Stack.new
s.empty?.must_equal true
s.push(-1)
Expand All @@ -70,7 +70,7 @@
end

it "returns the top element on the stack" do
skip
# skip
s = Stack.new
s.push(40)
s.push(22)
Expand Down