Class: DoublyLinkedList

Inherits:
Object
  • Object
show all
Defined in:
lib/doubly_linked_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DoublyLinkedList

( list, current_item ) returns instance of item in list

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/doubly_linked_list.rb', line 37

def initialize(*args)
  @list      = []
  @list_info = nil

  if !args.empty? && args[0].is_a?(Hash)
    @list      = args[0][:items]              if args[0].key?(:items)
    @list_info = args[0][:list_info]          if args[0].key?(:list_info)

    # set callbacks
    @clear_first  = args[0][:clear_first_callback]  if args[0].key?(:clear_first_callback)
    @clear_last   = args[0][:clear_last_callback]   if args[0].key?(:clear_last_callback)
    @update_first = args[0][:update_first_callback] if args[0].key?(:update_first_callback)
    @update_last  = args[0][:update_last_callback]  if args[0].key?(:update_last_callback)

    @clear_next   = args[0][:clear_next_callback]   if args[0].key?(:clear_next_callback)
    @clear_prev   = args[0][:clear_prev_callback]   if args[0].key?(:clear_prev_callback)
    @update_next  = args[0][:update_next_callback]  if args[0].key?(:update_next_callback)
    @update_prev  = args[0][:update_prev_callback]  if args[0].key?(:update_prev_callback)

    @find_first   = args[0][:find_first_callback]   if args[0].key?(:find_first_callback)
    @find_last    = args[0][:find_last_callback]    if args[0].key?(:find_last_callback)
    @find_next    = args[0][:find_next_callback]    if args[0].key?(:find_next_callback)
    @find_prev    = args[0][:find_prev_callback]    if args[0].key?(:find_prev_callback)

    unless @list.empty?
      @list = organize_list
      update_first_in_listinfo
      update_last_in_listinfo
    end
  end
  raise ArgumentError, "items list must be an Array" unless @list.kind_of?(Array)
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object

Passing all missing methods to the list so all array operations can be performed.



183
184
185
186
187
188
189
190
191
# File 'lib/doubly_linked_list.rb', line 183

def method_missing method_id, *args, &block
  begin
    return @list.send(method_id,*args, &block)     if @list.respond_to?(method_id)
    return @list_info.send(method_id,*args,&block) if @list_info.respond_to?(method_id)
    super
  rescue
    super
  end
end

Instance Attribute Details

#list_infoObject

include Conversions



19
20
21
# File 'lib/doubly_linked_list.rb', line 19

def list_info
  @list_info
end

Instance Method Details

#add_first(data) ⇒ Object

Add data as the first item in the list.

Returns:

  • position where added



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/doubly_linked_list.rb', line 126

def add_first(data)
  raise ArgumentError 'data must have non-nil value' unless data
  @list.insert(0,data)
  if @list.size > 1
    update_prev_in_listitem(head+1)
    update_next_in_listitem(head)
  else
    clear_next_in_listitem(head)
    update_last_in_listinfo
  end
  clear_prev_in_listitem(head)
  update_first_in_listinfo
  @list.size
end

#add_last(data) ⇒ Object Also known as: <<

Add data to the end of the list.

Returns:

  • position where added



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/doubly_linked_list.rb', line 104

def add_last(data)
  raise ArgumentError 'data must have non-nil value' unless data
  @list << data
  if @list.size > 1
    update_next_in_listitem(tail-1)
    update_prev_in_listitem(tail)
  else
    clear_prev_in_listitem(tail)
    update_first_in_listinfo
  end
  clear_next_in_listitem(tail)
  update_last_in_listinfo
  @list.size
end

#firstObject

Get the first element of the list.

Returns:

  • first node in the list or nil



84
85
86
87
# File 'lib/doubly_linked_list.rb', line 84

def first
  return nil if @list.empty?
  @list[head]
end

#inspectObject



193
194
195
# File 'lib/doubly_linked_list.rb', line 193

def inspect
  sprintf('#<%s:%#x %s>', self.class, self.__id__, to_a.inspect)
end

#lastObject

Get the last element of the list.

Returns:

  • last node in the list or nil



93
94
95
96
# File 'lib/doubly_linked_list.rb', line 93

def last
  return nil if @list.empty?
  @list[tail]
end

#remove_firstObject

Removes data from the beginning of the list.

Returns:

  • data stored in the removed node



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/doubly_linked_list.rb', line 162

def remove_first
  return nil if @list.empty?
  item = @list.delete_at(head)
  if @list.size > 0
    clear_prev_in_listitem(head)
    update_first_in_listinfo
  else
    clear_first_in_listinfo
    clear_last_in_listinfo
  end
  item
end

#remove_lastObject

Remove node from the end of the list.

Returns:

  • data stored in the removed node



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/doubly_linked_list.rb', line 145

def remove_last
  return nil if @list.empty?
  item = @list.delete_at(tail)
  if @list.size > 0
    clear_next_in_listitem(tail)
    update_last_in_listinfo
  else
    clear_first_in_listinfo
    clear_last_in_listinfo
  end
  item
end

#sizeObject Also known as: count

Get the number of items in the list.

Returns:

  • the number of items



75
76
77
# File 'lib/doubly_linked_list.rb', line 75

def size
  @list.size
end

#to_aObject Also known as: to_ary

Returns list array without list info.



177
178
179
# File 'lib/doubly_linked_list.rb', line 177

def to_a
  @list
end