Commit fa2bc82
Changed files (2)
lib
data_structures
spec
data_structures
lib/data_structures/linked_list_stack.rb
@@ -1,6 +1,6 @@
class LinkedListStack
def initialize
- @head = NullNode.new
+ @head = Node.new(nil)
end
def push(item)
@@ -10,6 +10,10 @@ class LinkedListStack
def pop
@head.pop
end
+
+ def count
+ 0
+ end
end
class Node
@@ -20,10 +24,7 @@ class Node
end
def push(item)
- if @next
- @next.push(item)
- end
-
+ @next.push(item) if @next
@next = Node.new(item)
end
@@ -43,17 +44,3 @@ class Node
@next == nil
end
end
-
-class NullNode
- def push(item)
- if @next
- @next.push(item)
- else
- @next = Node.new(item)
- end
- end
-
- def pop
- return @next.pop if @next
- end
-end
spec/data_structures/linked_list_stack_spec.rb
@@ -11,21 +11,26 @@ describe LinkedListStack do
end
end
- context "when a single item is pushed on to the stack" do
+ context "when a multiple items are pushed on to the stack" do
+ let(:n) { 10 }
+
+ before :each do
+ (1..n).each do |n|
+ sut.push(n)
+ end
+ end
it "should pop the last item pushed on to the stack" do
- sut.push(1)
- sut.push(2)
- result = sut.pop
- result.should == 2
+ sut.pop.should == n
end
it "should pop off each item in reverse order of how they were put on" do
- (1..10).each do |n|
- sut.push(n)
- end
- (10..1).each do |n|
+ (n..1).each do |n|
sut.pop.should == n
end
end
+
+ it "should have the correct number of items" do
+ sut.count.should == n
+ end
end
end