Class: DataStructList::SimpleLinkedList
- Inherits:
-
Object
- Object
- DataStructList::SimpleLinkedList
- Defined in:
- lib/data_struct_list.rb
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
Instance Method Summary collapse
- #find(id) ⇒ Object
-
#initialize ⇒ SimpleLinkedList
constructor
A new instance of SimpleLinkedList.
- #insert(hash) ⇒ Object
- #remove(id) ⇒ Object
Constructor Details
#initialize ⇒ SimpleLinkedList
Returns a new instance of SimpleLinkedList.
26 27 28 29 30 |
# File 'lib/data_struct_list.rb', line 26 def initialize @head = Head.new @first = nil @last = nil end |
Instance Attribute Details
#first ⇒ Object (readonly)
Returns the value of attribute first.
25 26 27 |
# File 'lib/data_struct_list.rb', line 25 def first @first end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
25 26 27 |
# File 'lib/data_struct_list.rb', line 25 def head @head end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
25 26 27 |
# File 'lib/data_struct_list.rb', line 25 def last @last end |
Instance Method Details
#find(id) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/data_struct_list.rb', line 48 def find(id) elm = @first until elm == nil if elm.id == id then break end elm = elm.next end return elm end |
#insert(hash) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/data_struct_list.rb', line 32 def insert(hash) if @head.quant == 0 then @head.next = Node.new(hash) @first = @head.next @last = @head.next @head.quant += 1 else @last.next = Node.new(hash) @last = @last.next @head.quant += 1 end return @last.id end |
#remove(id) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/data_struct_list.rb', line 59 def remove(id) elm = @first aux = @first until elm == nil if elm.id == id then break end aux = elm elm = elm.next end if elm!=nil then aux.next = elm.next @head.quant -= 1 if elm == @head.next @first = elm.next @head.next = @first end if @last == elm then @last = aux end elm = nil end end |