RubyDataStructures 0.0.1 → 0.0.2
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 +27 -1
- data/lib/RubyDataStructures/multi_dimensional_array.rb +2 -3
- data/lib/RubyDataStructures/queue_as_array.rb +3 -7
- data/lib/RubyDataStructures/singly_linked_list/element.rb +16 -0
- data/lib/RubyDataStructures/singly_linked_list.rb +59 -0
- data/lib/RubyDataStructures/stack_as_array.rb +4 -7
- data/lib/RubyDataStructures/version.rb +1 -1
- data/lib/RubyDataStructures.rb +3 -1
- data/test/RubyDataStructureTest.rb +3 -1
- data/test/singly_linked_list_element_test.rb +42 -0
- data/test/singly_linked_list_test.rb +67 -0
- metadata +7 -3
data/RELEASE_NOTES
CHANGED
@@ -1,5 +1,31 @@
|
|
1
|
-
= Announce:
|
1
|
+
= Announce: Ruby DataStructures Release 0.0.2
|
2
|
+
Date Mar 5, 2011
|
3
|
+
Release 0.0.2 adds new features.
|
4
|
+
|
5
|
+
New features:
|
6
|
+
|
7
|
+
* SinglyLinkedList
|
8
|
+
|
9
|
+
Deprecation Notices:
|
2
10
|
|
11
|
+
* None
|
12
|
+
|
13
|
+
Bugs Fixed:
|
14
|
+
|
15
|
+
* None
|
16
|
+
|
17
|
+
Other Changes Include:
|
18
|
+
|
19
|
+
* None
|
20
|
+
|
21
|
+
== Thanks
|
22
|
+
|
23
|
+
-- Satyaram B V
|
24
|
+
|
25
|
+
===========================================
|
26
|
+
|
27
|
+
= Announce: RubyDatStructures Release 0.0.1
|
28
|
+
Date: Mar 4, 2011
|
3
29
|
Release 0.0.1 makes the project public.
|
4
30
|
|
5
31
|
$ gem install RubyDataStructures
|
@@ -21,9 +21,8 @@ class RubyDataStructures::MultiDimensionalArray
|
|
21
21
|
offset = 0
|
22
22
|
|
23
23
|
(0..(@dimensions.length - 1)).each do |i|
|
24
|
-
|
25
|
-
|
26
|
-
end
|
24
|
+
|
25
|
+
raise IndexError if indices[i] < 0 || indices[i] >= @dimensions[i]
|
27
26
|
|
28
27
|
offset += @factors[i]*indices[i]
|
29
28
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class QueueAsArray
|
1
|
+
class RubyDataStructures::QueueAsArray
|
2
2
|
# Initializes a stack of size +size+
|
3
3
|
# The value of +head+ for a new stack is +nil+
|
4
4
|
# The value of +tail+ for a new stack is +0+
|
@@ -19,9 +19,7 @@ class QueueAsArray
|
|
19
19
|
|
20
20
|
# The queue is enqueued with +element+
|
21
21
|
def enqueue(element)
|
22
|
-
if self.full?
|
23
|
-
raise "Queue Overflow - The queue is full"
|
24
|
-
end
|
22
|
+
raise "Queue Overflow - The queue is full" if self.full?
|
25
23
|
|
26
24
|
@array[@tail] = element
|
27
25
|
|
@@ -38,9 +36,7 @@ class QueueAsArray
|
|
38
36
|
|
39
37
|
# The queue is dequeued
|
40
38
|
def dequeue
|
41
|
-
if self.empty?
|
42
|
-
raise "Queue Underflow - The queue is empty"
|
43
|
-
end
|
39
|
+
raise "Queue Underflow - The queue is empty" if self.empty?
|
44
40
|
|
45
41
|
x = @array[@head]
|
46
42
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
class RubyDataStructures::SinglyLinkedList::Element
|
5
|
+
attr_accessor :key
|
6
|
+
attr_accessor :next
|
7
|
+
|
8
|
+
# Initializes an Element of SinglyLinkedList
|
9
|
+
# Arguments:
|
10
|
+
# *list* => Instance of SinglyLinkedList to which the element belongs to
|
11
|
+
# *key* => Value to be stored in the element.
|
12
|
+
def initialize(list, key)
|
13
|
+
@list = list
|
14
|
+
@key = key
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class RubyDataStructures::SinglyLinkedList
|
2
|
+
# Initializes the SinglyLinkedList
|
3
|
+
def initialize
|
4
|
+
@sentinal = RubyDataStructures::SinglyLinkedList::Element.new(self, nil)
|
5
|
+
self.reset
|
6
|
+
end
|
7
|
+
|
8
|
+
def head
|
9
|
+
@sentinal.next
|
10
|
+
end
|
11
|
+
|
12
|
+
def empty?
|
13
|
+
self.head == @sentinal
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset
|
17
|
+
@sentinal.next = @sentinal
|
18
|
+
end
|
19
|
+
|
20
|
+
# Splices an element onto the front of the linked list
|
21
|
+
# Arguments:
|
22
|
+
# *item* => Value to store in the element to be inserted
|
23
|
+
def insert(item)
|
24
|
+
element = RubyDataStructures::SinglyLinkedList::Element.new(self, item)
|
25
|
+
element.next = @sentinal.next
|
26
|
+
@sentinal.next = element
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the first element with a given key
|
30
|
+
# Arguments:
|
31
|
+
# *key* => Key of the element to be searched for
|
32
|
+
def search(key)
|
33
|
+
element = self.head
|
34
|
+
while (element != @sentinal) && (element.key != key)
|
35
|
+
element = element.next
|
36
|
+
end
|
37
|
+
|
38
|
+
return element
|
39
|
+
end
|
40
|
+
|
41
|
+
# Removes the first element with a given key from the linked list
|
42
|
+
# Arguments:
|
43
|
+
# *key* => Key of the element to be removed
|
44
|
+
def delete(key)
|
45
|
+
element = self.head
|
46
|
+
prev_element = @sentinal
|
47
|
+
|
48
|
+
while (element != @sentinal) && (element.key != key)
|
49
|
+
prev_element = element
|
50
|
+
element = element.next
|
51
|
+
end
|
52
|
+
|
53
|
+
if element == @sentinal
|
54
|
+
raise "Argument Error - No element with the key found"
|
55
|
+
else
|
56
|
+
prev_element.next = element.next
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class StackAsArray
|
1
|
+
class RubyDataStructures::StackAsArray
|
2
2
|
# Initializes a stack of size +size+
|
3
3
|
# The value of +top+ for an empty stack is +nil+
|
4
4
|
def initialize(size = 1)
|
@@ -22,9 +22,8 @@ class StackAsArray
|
|
22
22
|
|
23
23
|
# Pushes an element +element+ into the stack
|
24
24
|
def push(element)
|
25
|
-
|
26
|
-
|
27
|
-
end
|
25
|
+
|
26
|
+
raise "Stack Overflow - The stack is full" if self.full?
|
28
27
|
|
29
28
|
if self.empty?
|
30
29
|
@top = 0
|
@@ -37,9 +36,7 @@ class StackAsArray
|
|
37
36
|
|
38
37
|
# Pops the stack
|
39
38
|
def pop
|
40
|
-
if self.empty?
|
41
|
-
raise "Stack Underflow - The stack is empty"
|
42
|
-
end
|
39
|
+
raise "Stack Underflow - The stack is empty" if self.empty?
|
43
40
|
|
44
41
|
x = @array[@top]
|
45
42
|
if self.singleton?
|
data/lib/RubyDataStructures.rb
CHANGED
@@ -4,4 +4,6 @@ require 'RubyDataStructures/ruby_data_structures'
|
|
4
4
|
# All RubyDataStructures
|
5
5
|
require 'RubyDataStructures/multi_dimensional_array'
|
6
6
|
require 'RubyDataStructures/stack_as_array'
|
7
|
-
require 'RubyDataStructures/queue_as_array'
|
7
|
+
require 'RubyDataStructures/queue_as_array'
|
8
|
+
require 'RubyDataStructures/singly_linked_list'
|
9
|
+
require 'RubyDataStructures/singly_linked_list/element'
|
@@ -6,4 +6,6 @@ require 'ruby-debug'
|
|
6
6
|
|
7
7
|
require 'test/multi_dimensional_array_test'
|
8
8
|
require 'test/stack_as_array_test'
|
9
|
-
require 'test/queue_as_array_test'
|
9
|
+
require 'test/queue_as_array_test'
|
10
|
+
require 'test/singly_linked_list_test'
|
11
|
+
require 'test/singly_linked_list_element_test'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'RubyDataStructures'
|
8
|
+
|
9
|
+
class SinglyLinkedListElementTest < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@list = RubyDataStructures::SinglyLinkedList.new
|
12
|
+
@element = RubyDataStructures::SinglyLinkedList::Element.new(@list, 9)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_initialize
|
16
|
+
assert_equal 9, @element.key
|
17
|
+
assert_nil @element.next
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_key
|
21
|
+
assert_equal 9, @element.key
|
22
|
+
|
23
|
+
@element.key = 3
|
24
|
+
assert_equal 3, @element.key
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_next
|
28
|
+
e1 = @element
|
29
|
+
e2 = RubyDataStructures::SinglyLinkedList::Element.new(@list, 9)
|
30
|
+
assert_nil e1.next
|
31
|
+
assert_nil e2.next
|
32
|
+
|
33
|
+
e1.next = e2
|
34
|
+
assert_equal e2, e1.next
|
35
|
+
assert_nil e2.next
|
36
|
+
|
37
|
+
e2.next = e1
|
38
|
+
assert_equal e2, e1.next
|
39
|
+
assert_equal e1, e2.next
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'RubyDataStructures'
|
8
|
+
|
9
|
+
class SinglyLinkedListTest < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@list = RubyDataStructures::SinglyLinkedList.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_initialize
|
15
|
+
assert_nil @list.head.key
|
16
|
+
assert @list.empty?
|
17
|
+
end
|
18
|
+
|
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
|
+
def test_singly_linked_list
|
30
|
+
assert @list.empty?
|
31
|
+
assert_nil @list.head.key
|
32
|
+
assert_nil @list.search(7).key
|
33
|
+
assert_nil @list.search(9).key
|
34
|
+
|
35
|
+
@list.insert(7)
|
36
|
+
assert !@list.empty?
|
37
|
+
assert_equal 7, @list.head.key
|
38
|
+
assert_equal 7, @list.search(7).key
|
39
|
+
assert_nil @list.search(9).key
|
40
|
+
|
41
|
+
@list.insert(9)
|
42
|
+
assert !@list.empty?
|
43
|
+
assert_equal 9, @list.head.key
|
44
|
+
assert_equal 7, @list.search(7).key
|
45
|
+
assert_equal 9, @list.search(9).key
|
46
|
+
|
47
|
+
@list.delete(7)
|
48
|
+
assert !@list.empty?
|
49
|
+
assert_equal 9, @list.head.key
|
50
|
+
assert_nil @list.search(7).key
|
51
|
+
assert_equal 9, @list.search(9).key
|
52
|
+
|
53
|
+
assert_raise RuntimeError, "Argument Error - No element with the key found" do
|
54
|
+
@list.delete(7)
|
55
|
+
end
|
56
|
+
assert !@list.empty?
|
57
|
+
assert_equal 9, @list.head.key
|
58
|
+
assert_nil @list.search(7).key
|
59
|
+
assert_equal 9, @list.search(9).key
|
60
|
+
|
61
|
+
@list.reset
|
62
|
+
assert @list.empty?
|
63
|
+
assert_nil @list.head.key
|
64
|
+
assert_nil @list.search(7).key
|
65
|
+
assert_nil @list.search(9).key
|
66
|
+
end
|
67
|
+
end
|
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Satyaram B V
|
@@ -40,11 +40,15 @@ files:
|
|
40
40
|
- lib/RubyDataStructures/multi_dimensional_array.rb
|
41
41
|
- lib/RubyDataStructures/queue_as_array.rb
|
42
42
|
- lib/RubyDataStructures/ruby_data_structures.rb
|
43
|
+
- lib/RubyDataStructures/singly_linked_list.rb
|
44
|
+
- lib/RubyDataStructures/singly_linked_list/element.rb
|
43
45
|
- lib/RubyDataStructures/stack_as_array.rb
|
44
46
|
- lib/RubyDataStructures/version.rb
|
45
47
|
- test/RubyDataStructureTest.rb
|
46
48
|
- test/multi_dimensional_array_test.rb
|
47
49
|
- test/queue_as_array_test.rb
|
50
|
+
- test/singly_linked_list_element_test.rb
|
51
|
+
- test/singly_linked_list_test.rb
|
48
52
|
- test/stack_as_array_test.rb
|
49
53
|
has_rdoc: true
|
50
54
|
homepage: http://bvsatyaram.com
|