Class: LinkedList::Singly
- Inherits:
-
Object
- Object
- LinkedList::Singly
- Defined in:
- lib/linked_list_sourav.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add(data) ⇒ Object
adds new members.
-
#add_a(data) ⇒ Object
adds array members to the list.
-
#delete(data) ⇒ Object
deletes a single member.
- #edit(old_data, new_data) ⇒ Object
- #find(data) ⇒ Object
-
#head ⇒ Object
returns head.
-
#initialize(data = nil) ⇒ Singly
constructor
constructor.
- #no_of_nodes ⇒ Object
-
#parse ⇒ Object
parses all the members.
-
#to_a ⇒ Object
returns an array of all data.
-
#to_s ⇒ Object
returns all data in form of string.
Constructor Details
#initialize(data = nil) ⇒ Singly
constructor
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/linked_list_sourav.rb', line 37 def initialize(data = nil) # constructor if data.class == Array @head = Node.new(data[0]) data.each.with_index { |datum, index| self.add(datum) if index > 0} else @head = Node.new(data) end @count = 1 self end |
Instance Method Details
#add(data) ⇒ Object
adds new members
72 73 74 75 76 77 78 79 80 |
# File 'lib/linked_list_sourav.rb', line 72 def add(data) # adds new members node = @head while (node.forward != nil) node = node.forward end node.forward = Node.new(data) @count += 1 node.forward end |
#add_a(data) ⇒ Object
adds array members to the list
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/linked_list_sourav.rb', line 82 def add_a(data) # adds array members to the list node = @head while (node.forward != nil) node = node.forward end if data.respond_to? :each data.each { |datum| self.add(datum)} end self end |
#delete(data) ⇒ Object
deletes a single member
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/linked_list_sourav.rb', line 121 def delete(data) # deletes a single member node = @head deleted_node = '' if node.data === data @head = node.forward deleted_node = node @count -= 1 else node = @head while( node != nil && node.forward != nil && (node.forward).data != data) node = node.forward end if (node != nil) && (node.forward != nil) node.forward = (node.forward).forward @count -= 1 end deleted_node = node.forward end deleted_node = nil end |
#edit(old_data, new_data) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/linked_list_sourav.rb', line 93 def edit(old_data, new_data) node = find(old_data) if node node.data = new_data end end |
#find(data) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/linked_list_sourav.rb', line 60 def find(data) node = @head while (node != nil && node.data != data ) node = node.forward end node end |
#head ⇒ Object
returns head
48 49 50 |
# File 'lib/linked_list_sourav.rb', line 48 def head #returns head @head end |
#no_of_nodes ⇒ Object
68 69 70 |
# File 'lib/linked_list_sourav.rb', line 68 def no_of_nodes @count end |
#parse ⇒ Object
parses all the members
52 53 54 55 56 57 58 |
# File 'lib/linked_list_sourav.rb', line 52 def parse # parses all the members node = @head while (node != nil) puts node.data node = node.forward end end |
#to_a ⇒ Object
returns an array of all data
100 101 102 103 104 105 106 107 108 |
# File 'lib/linked_list_sourav.rb', line 100 def to_a # returns an array of all data node = @head array = Array.new while (node != nil) array << node.data node = node.forward end array end |
#to_s ⇒ Object
returns all data in form of string
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/linked_list_sourav.rb', line 110 def to_s # returns all data in form of string node = @head string = String.new(@head.data.to_s) node = node.forward while (node != nil) string << ", " << node.data.to_s node = node.forward end string end |