Class: DataStructList::CycleSimpleLinkedList

Inherits:
SimpleLinkedList show all
Defined in:
lib/data_struct_list.rb

Instance Attribute Summary

Attributes inherited from Stack

#first, #head, #last

Instance Method Summary collapse

Methods inherited from Stack

#initialize

Constructor Details

This class inherits a constructor from DataStructList::Stack

Instance Method Details

#find(id) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/data_struct_list.rb', line 111

def find(id)
  if @first == nil then return @first end

  elm = @first

  until elm == @last.next
    if elm.id == id then break end
    elm = elm.next
  end

  return elm
end

#insert(hash) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/data_struct_list.rb', line 124

def insert(hash)
  if @head.quant == 0 then
    @head.next = Node.new(hash)
    @first = @head.next
    @last = @head.next
    @head.quant += 1
    @first.next = @last

  else
    @last.next =  Node.new(hash)
    @last = @last.next
    @head.quant += 1
  end

  @last.next = @first

  return @last.id
end

#remove(id) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/data_struct_list.rb', line 143

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