Class: LinkedList
- Includes:
- Enumerable
- Defined in:
- lib/gamebox/lib/linked_list.rb
Defined Under Namespace
Classes: ListElem
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
- #append(e) ⇒ Object (also: #<<)
- #each ⇒ Object
- #each_element ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(enum = nil) ⇒ LinkedList
constructor
A new instance of LinkedList.
- #place(new_obj, location, elem) ⇒ Object
- #prepend(e) ⇒ Object (also: #>>)
-
#remove(e) ⇒ Object
(also: #-)
only removed the first instance of e.
-
#shift ⇒ Object
pop off the first item.
- #size ⇒ Object
- #to_s ⇒ Object
-
#unshift ⇒ Object
pop off the last item.
Constructor Details
#initialize(enum = nil) ⇒ LinkedList
Returns a new instance of LinkedList.
7 8 9 10 11 12 13 |
# File 'lib/gamebox/lib/linked_list.rb', line 7 def initialize(enum = nil) @head = @tail = ListElem.new @head.next = @head @head.prev = @head append enum unless enum.nil? end |
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
5 6 7 |
# File 'lib/gamebox/lib/linked_list.rb', line 5 def head @head end |
#tail ⇒ Object
Returns the value of attribute tail.
5 6 7 |
# File 'lib/gamebox/lib/linked_list.rb', line 5 def tail @tail end |
Instance Method Details
#append(e) ⇒ Object Also known as: <<
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gamebox/lib/linked_list.rb', line 53 def append(e) # puts "appending #{e[0][0]},#{e[0][1]}" tmp = ListElem.new tmp.obj = e tmp.prev = @tail.prev tmp.next = @tail tmp.prev.next = tmp tmp.next.prev = tmp # puts to_s self end |
#each ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/gamebox/lib/linked_list.rb', line 98 def each i = @head.next while @tail != i yield i.obj i = i.next end end |
#each_element ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/gamebox/lib/linked_list.rb', line 107 def each_element i = @head.next while @tail != i yield i i = i.next end end |
#empty? ⇒ Boolean
94 95 96 |
# File 'lib/gamebox/lib/linked_list.rb', line 94 def empty?() @tail.prev == @head end |
#place(new_obj, location, elem) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gamebox/lib/linked_list.rb', line 33 def place(new_obj, location, elem) # puts "placing #{new_obj[0][0]},#{new_obj[0][1]} #{location} #{elem.obj[0][0]},#{elem.obj[0][1]}" tmp = ListElem.new tmp.obj = new_obj case location when :before tmp.next = elem tmp.prev = elem.prev elem.prev.next = tmp elem.prev = tmp when :after tmp.prev = elem tmp.next = elem.next elem.next.prev = tmp elem.next = tmp end # puts to_s tmp end |
#prepend(e) ⇒ Object Also known as: >>
66 67 68 69 70 71 72 73 74 |
# File 'lib/gamebox/lib/linked_list.rb', line 66 def prepend(e) tmp = ListElem.new tmp.obj = e tmp.prev = @head tmp.next = @head.next tmp.prev.next = tmp tmp.next.prev = tmp self end |
#remove(e) ⇒ Object Also known as: -
only removed the first instance of e
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gamebox/lib/linked_list.rb', line 78 def remove(e) i = @head.next while @tail != i if i.obj == e # remove it i.prev.next = i.next i.next.prev = i.prev return i.obj end i = i.next end i end |
#shift ⇒ Object
pop off the first item
16 17 18 19 20 21 22 23 |
# File 'lib/gamebox/lib/linked_list.rb', line 16 def shift tmp = @head.next @head.next = tmp.next tmp.next.prev = @head #puts "SHIFTING [#{tmp.obj.x},#{tmp.obj.y}]" # puts to_s return tmp.obj end |
#size ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/gamebox/lib/linked_list.rb', line 128 def size size = 0 i = @head.next while @tail != i i = i.next size += 1 end size end |
#to_s ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gamebox/lib/linked_list.rb', line 116 def to_s str = "LinkedList #{self.object_id} [" i = @head.next while @tail != i str += "#{i.obj[0]}-#{i.obj[1]}," i = i.next end str += "]" str end |
#unshift ⇒ Object
pop off the last item
26 27 28 29 30 31 |
# File 'lib/gamebox/lib/linked_list.rb', line 26 def unshift tmp = @tail.prev @tail.prev = @tail.prev.prev @tail.next = @tail return tmp.obj end |