RubyDataStructures 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/RELEASE_NOTES CHANGED
@@ -1,3 +1,28 @@
1
+ = Announce: Ruby DataStructures Release 0.0.3
2
+ Date Mar 5, 2011
3
+ Release 0.0.3 adds new features.
4
+
5
+ New features:
6
+
7
+ * DoublyLinkedList
8
+
9
+ Deprecation Notices:
10
+
11
+ * None
12
+
13
+ Bugs Fixed:
14
+
15
+ * None
16
+
17
+ Other Changes Include:
18
+
19
+ * Fixed typo: sentinal => sentinel
20
+
21
+ == Thanks
22
+
23
+ -- Satyaram B V
24
+
25
+ ===========================================
1
26
  = Announce: Ruby DataStructures Release 0.0.2
2
27
  Date Mar 5, 2011
3
28
  Release 0.0.2 adds new features.
@@ -0,0 +1,3 @@
1
+ class RubyDataStructures::DoublyLinkedList::Element < RubyDataStructures::SinglyLinkedList::Element
2
+ attr_accessor :previous
3
+ end
@@ -0,0 +1,44 @@
1
+ class RubyDataStructures::DoublyLinkedList < RubyDataStructures::SinglyLinkedList
2
+
3
+ # Overrides the +initialize+ method in SinglyLinkedList
4
+ # Initializes the DoublyLinkedList
5
+ def initialize
6
+ @sentinel = RubyDataStructures::DoublyLinkedList::Element.new(self, nil)
7
+ self.reset
8
+ end
9
+
10
+ # Extends the +reset+ method in SinglyLinkedList to also set +previous+
11
+ # attribute to the sentinel
12
+ def reset
13
+ super
14
+ @sentinel.previous = @sentinel
15
+ end
16
+
17
+ # Returns the last element of the linked list
18
+ def tail
19
+ @sentinel.previous
20
+ end
21
+
22
+ # Overrides the +insert+ method in SinglyLinkedList to also set/update
23
+ # +previous+ attributes to the elements
24
+ def insert(item)
25
+ element = RubyDataStructures::DoublyLinkedList::Element.new(self, item)
26
+ element.next = @sentinel.next
27
+ @sentinel.next = element
28
+ element.next.previous = element
29
+ element.previous = @sentinel
30
+ end
31
+
32
+ # Overrides the +delete+ method in SinglyLinkedList to optimize the
33
+ # implementation.
34
+ def delete(key)
35
+ element = search(key)
36
+
37
+ if element == @sentinel
38
+ raise "Argument Error - No element with the key found"
39
+ else
40
+ element.previous.next = element.next
41
+ element.next.previous = element.previous
42
+ end
43
+ end
44
+ end
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  class RubyDataStructures::SinglyLinkedList::Element
5
2
  attr_accessor :key
6
3
  attr_accessor :next
@@ -1,20 +1,20 @@
1
- class RubyDataStructures::SinglyLinkedList
1
+ class RubyDataStructures::SinglyLinkedList
2
2
  # Initializes the SinglyLinkedList
3
3
  def initialize
4
- @sentinal = RubyDataStructures::SinglyLinkedList::Element.new(self, nil)
4
+ @sentinel = RubyDataStructures::SinglyLinkedList::Element.new(self, nil)
5
5
  self.reset
6
6
  end
7
7
 
8
8
  def head
9
- @sentinal.next
9
+ @sentinel.next
10
10
  end
11
11
 
12
12
  def empty?
13
- self.head == @sentinal
13
+ self.head == @sentinel
14
14
  end
15
15
 
16
16
  def reset
17
- @sentinal.next = @sentinal
17
+ @sentinel.next = @sentinel
18
18
  end
19
19
 
20
20
  # Splices an element onto the front of the linked list
@@ -22,8 +22,8 @@ class RubyDataStructures::SinglyLinkedList
22
22
  # *item* => Value to store in the element to be inserted
23
23
  def insert(item)
24
24
  element = RubyDataStructures::SinglyLinkedList::Element.new(self, item)
25
- element.next = @sentinal.next
26
- @sentinal.next = element
25
+ element.next = @sentinel.next
26
+ @sentinel.next = element
27
27
  end
28
28
 
29
29
  # Returns the first element with a given key
@@ -31,7 +31,7 @@ class RubyDataStructures::SinglyLinkedList
31
31
  # *key* => Key of the element to be searched for
32
32
  def search(key)
33
33
  element = self.head
34
- while (element != @sentinal) && (element.key != key)
34
+ while (element != @sentinel) && (element.key != key)
35
35
  element = element.next
36
36
  end
37
37
 
@@ -43,14 +43,14 @@ class RubyDataStructures::SinglyLinkedList
43
43
  # *key* => Key of the element to be removed
44
44
  def delete(key)
45
45
  element = self.head
46
- prev_element = @sentinal
46
+ prev_element = @sentinel
47
47
 
48
- while (element != @sentinal) && (element.key != key)
48
+ while (element != @sentinel) && (element.key != key)
49
49
  prev_element = element
50
50
  element = element.next
51
51
  end
52
52
 
53
- if element == @sentinal
53
+ if element == @sentinel
54
54
  raise "Argument Error - No element with the key found"
55
55
  else
56
56
  prev_element.next = element.next
@@ -1,3 +1,3 @@
1
1
  module Rubydatastructures
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -6,4 +6,6 @@ require 'RubyDataStructures/multi_dimensional_array'
6
6
  require 'RubyDataStructures/stack_as_array'
7
7
  require 'RubyDataStructures/queue_as_array'
8
8
  require 'RubyDataStructures/singly_linked_list'
9
- require 'RubyDataStructures/singly_linked_list/element'
9
+ require 'RubyDataStructures/singly_linked_list/element'
10
+ require 'RubyDataStructures/doubly_linked_list'
11
+ require 'RubyDataStructures/doubly_linked_list/element'
@@ -8,4 +8,6 @@ require 'test/multi_dimensional_array_test'
8
8
  require 'test/stack_as_array_test'
9
9
  require 'test/queue_as_array_test'
10
10
  require 'test/singly_linked_list_test'
11
- require 'test/singly_linked_list_element_test'
11
+ require 'test/singly_linked_list_element_test'
12
+ require 'test/doubly_linked_list_test'
13
+ require 'test/doubly_linked_list_element_test'
@@ -0,0 +1,39 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'RubyDataStructures'
5
+
6
+ class DoublyLinkedListElementTest < Test::Unit::TestCase
7
+ def setup
8
+ @list = RubyDataStructures::SinglyLinkedList.new
9
+ @element = RubyDataStructures::SinglyLinkedList::Element.new(@list, 9)
10
+ end
11
+
12
+ def test_initialize
13
+ assert_equal 9, @element.key
14
+ assert_nil @element.next
15
+ end
16
+
17
+ def test_key
18
+ assert_equal 9, @element.key
19
+
20
+ @element.key = 3
21
+ assert_equal 3, @element.key
22
+ end
23
+
24
+ def test_next
25
+ e1 = @element
26
+ e2 = RubyDataStructures::SinglyLinkedList::Element.new(@list, 9)
27
+ assert_nil e1.next
28
+ assert_nil e2.next
29
+
30
+ e1.next = e2
31
+ assert_equal e2, e1.next
32
+ assert_nil e2.next
33
+
34
+ e2.next = e1
35
+ assert_equal e2, e1.next
36
+ assert_equal e1, e2.next
37
+
38
+ end
39
+ end
@@ -0,0 +1,61 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'RubyDataStructures'
5
+
6
+ class DoublyLinkedListTest < Test::Unit::TestCase
7
+ def setup
8
+ @list = RubyDataStructures::DoublyLinkedList.new
9
+ end
10
+
11
+ def test_initialize
12
+ assert_nil @list.head.key
13
+ assert_nil @list.tail.key
14
+ assert @list.empty?
15
+ end
16
+
17
+ def test_singly_linked_list
18
+ assert @list.empty?
19
+ assert_nil @list.head.key
20
+ assert_nil @list.tail.key
21
+ assert_nil @list.search(7).key
22
+ assert_nil @list.search(9).key
23
+
24
+ @list.insert(7)
25
+ assert !@list.empty?
26
+ assert_equal 7, @list.head.key
27
+ assert_equal 7, @list.tail.key
28
+ assert_equal 7, @list.search(7).key
29
+ assert_nil @list.search(9).key
30
+
31
+ @list.insert(9)
32
+ assert !@list.empty?
33
+ assert_equal 9, @list.head.key
34
+ assert_equal 7, @list.tail.key
35
+ assert_equal 7, @list.search(7).key
36
+ assert_equal 9, @list.search(9).key
37
+
38
+ @list.delete(7)
39
+ assert !@list.empty?
40
+ assert_equal 9, @list.head.key
41
+ assert_equal 9, @list.tail.key
42
+ assert_nil @list.search(7).key
43
+ assert_equal 9, @list.search(9).key
44
+
45
+ assert_raise RuntimeError, "Argument Error - No element with the key found" do
46
+ @list.delete(7)
47
+ end
48
+ assert !@list.empty?
49
+ assert_equal 9, @list.head.key
50
+ assert_equal 9, @list.tail.key
51
+ assert_nil @list.search(7).key
52
+ assert_equal 9, @list.search(9).key
53
+
54
+ @list.reset
55
+ assert @list.empty?
56
+ assert_nil @list.head.key
57
+ assert_nil @list.tail.key
58
+ assert_nil @list.search(7).key
59
+ assert_nil @list.search(9).key
60
+ end
61
+ end
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
2
 
6
3
  require 'test/unit'
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
2
 
6
3
  require 'test/unit'
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
2
 
6
3
  require 'test/unit'
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
2
 
6
3
  require 'test/unit'
@@ -16,16 +13,6 @@ class SinglyLinkedListTest < Test::Unit::TestCase
16
13
  assert @list.empty?
17
14
  end
18
15
 
19
- def test_insert
20
- assert_nil @list.head.key
21
-
22
- @list.insert(7)
23
- assert_equal 7, @list.head.key
24
-
25
- @list.insert(9)
26
- assert_equal 9, @list.head.key
27
- end
28
-
29
16
  def test_singly_linked_list
30
17
  assert @list.empty?
31
18
  assert_nil @list.head.key
@@ -1,6 +1,3 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
1
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
2
 
6
3
  require 'test/unit'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RubyDataStructures
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Satyaram B V
@@ -37,6 +37,8 @@ files:
37
37
  - Rakefile
38
38
  - RubyDataStructures.gemspec
39
39
  - lib/RubyDataStructures.rb
40
+ - lib/RubyDataStructures/doubly_linked_list.rb
41
+ - lib/RubyDataStructures/doubly_linked_list/element.rb
40
42
  - lib/RubyDataStructures/multi_dimensional_array.rb
41
43
  - lib/RubyDataStructures/queue_as_array.rb
42
44
  - lib/RubyDataStructures/ruby_data_structures.rb
@@ -45,6 +47,8 @@ files:
45
47
  - lib/RubyDataStructures/stack_as_array.rb
46
48
  - lib/RubyDataStructures/version.rb
47
49
  - test/RubyDataStructureTest.rb
50
+ - test/doubly_linked_list_element_test.rb
51
+ - test/doubly_linked_list_test.rb
48
52
  - test/multi_dimensional_array_test.rb
49
53
  - test/queue_as_array_test.rb
50
54
  - test/singly_linked_list_element_test.rb