Class: LinkedList
- Inherits:
-
Object
- Object
- LinkedList
- Includes:
- Enumerable
- Defined in:
- lib/bibliogem/LinkedList.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
Instance Method Summary collapse
- #each ⇒ Object
- #erase(position) ⇒ Object
-
#initialize(value = nil) ⇒ LinkedList
constructor
A new instance of LinkedList.
- #insert(position, *values) ⇒ Object
- #pop_back ⇒ Object
- #pop_front ⇒ Object
- #push_back(value) ⇒ Object
- #push_front(value) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value = nil) ⇒ LinkedList
Returns a new instance of LinkedList.
9 10 11 |
# File 'lib/bibliogem/LinkedList.rb', line 9 def initialize(value=nil) @head = Node.new(nil, value, nil) end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
7 8 9 |
# File 'lib/bibliogem/LinkedList.rb', line 7 def head @head end |
Instance Method Details
#each ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/bibliogem/LinkedList.rb', line 98 def each current = @head until current == nil yield (current[:value]) current=current[:next] end end |
#erase(position) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bibliogem/LinkedList.rb', line 54 def erase(position) if position > size return nil end if position == 0 if @head[:next].nil? @head = nil else @head = @head[:next] end else current = @head for i in 0..position-1 current = current[:next] end current = current[:prev] current[:next] = (current[:next])[:next] end end |
#insert(position, *values) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bibliogem/LinkedList.rb', line 37 def insert(position, *values) current = @head if position == 0 for value in values.reverse @head = Node.new(nil, value, @head) end else for i in 1..position-1 current = current[:next] end for value in values.reverse current[:next] = Node.new(current, value, current[:next]) end end self end |
#pop_back ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/bibliogem/LinkedList.rb', line 21 def pop_back current = @head until current[:next] == nil current = current[:next] end aux = current[:value] erase(size-1) return aux end |
#pop_front ⇒ Object
31 32 33 34 35 |
# File 'lib/bibliogem/LinkedList.rb', line 31 def pop_front aux = @head[:value] erase(0) return aux end |
#push_back(value) ⇒ Object
13 14 15 |
# File 'lib/bibliogem/LinkedList.rb', line 13 def push_back value insert(size, value) end |
#push_front(value) ⇒ Object
17 18 19 |
# File 'lib/bibliogem/LinkedList.rb', line 17 def push_front value insert(0, value) end |
#size ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bibliogem/LinkedList.rb', line 74 def size if @head == nil or @head[:value] == nil return 0 end counter = 1 current = @head until current[:next] == nil counter+=1 current=current[:next] end counter end |
#to_s ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bibliogem/LinkedList.rb', line 87 def to_s current = @head to_s_array = [] until current[:next] == nil to_s_array.push(current[:value]) current=current[:next] end to_s_array.push(current[:value]) p "[#{to_s_array.join(', ')}]" end |