Class: DoublyLinkedlist::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/doubly_linkedlist/list.rb

Overview

Creates list with nodes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = []) ⇒ List

Initializes the object with nodes having the values passed in the optional argument array.

Parameters:

  • values (Array) (defaults to: [])

    the values of nodes



17
18
19
20
# File 'lib/doubly_linkedlist/list.rb', line 17

def initialize(values = [])
  @count = 0
  build_list_from_array(values)
end

Instance Attribute Details

#countInteger (readonly)

Returns the number of nodes in the list.

Returns:

  • (Integer)

    the number of nodes in the list.



11
12
13
# File 'lib/doubly_linkedlist/list.rb', line 11

def count
  @count
end

Instance Method Details

#delete_at(index) ⇒ Object

Deletes the node at a given index and returns the value present in the deleted node. Returns nil if the given index is out of range.

Parameters:

  • index (Integer)

    the index at which node has to be deleted.

Returns:

  • (Object)

    the deleted node value.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/doubly_linkedlist/list.rb', line 106

def delete_at(index)
  return if (index + 1) > count || index < 0

  if index.zero?
    deleted = @head
    @head = @head.next
  else
    prev_item = @head

    (index - 1).times { prev_item = prev_item.next }

    deleted = prev_item.next
    prev_item.next = deleted.next
    @tail = prev_item if prev_item.next.nil?
  end

  @count -= 1
  deleted.value
end

#eachEnumerator, DoublyLinkedlist

Support for Enumerable methods.

Returns:

  • (Enumerator)

    the enumerator when no block is given

  • (DoublyLinkedlist)

    the object after yielding block on every item



156
157
158
159
160
161
162
163
164
165
# File 'lib/doubly_linkedlist/list.rb', line 156

def each
  return enum_for(:each) unless block_given?

  item = @head

  while item do
    yield item.value
    item = item.next
  end
end

#enqueue(value) ⇒ Integer

Inserts a node with the given value into the tail of the list, increments and returns the count of nodes.

Parameters:

  • value (Object)

    the node value to be enqueued into the list.

Returns:

  • (Integer)

    the number of nodes after insertion.



91
92
93
94
95
96
97
98
# File 'lib/doubly_linkedlist/list.rb', line 91

def enqueue(value)
  new_node = Node.new(value)
  @head = new_node unless @head
  @tail.next = new_node if @tail
  new_node.prev = @tail
  @tail = new_node
  @count += 1
end

#find_at(index) ⇒ Object, Nil

Returns the node object at the given index if present, nil otherwise.

Parameters:

  • index (Integer)

    the index where to look up.

Returns:

  • (Object, Nil)

    the value at the given index, or nil if index is out of range.



44
45
46
47
48
49
50
# File 'lib/doubly_linkedlist/list.rb', line 44

def find_at(index)
  return if (index + 1) > count || index < 0

  item = @head
  index.times { item = item.next }
  item.value
end

#headObject, Nil Also known as: first

Returns the value at the head of the list if present, nil otherwise.

Returns:

  • (Object, Nil)

    the node value object or nil.



25
26
27
# File 'lib/doubly_linkedlist/list.rb', line 25

def head
  @head.value if @head
end

#index(value) ⇒ Integer, Nil

Returns the leftmost index of the value present in the list. Returns nil if the value is not present.

Parameters:

  • value (Object)

    the node value for which to be looked up.

Returns:

  • (Integer, Nil)

    the index of the value passed in, or nil if value is not present.



57
58
59
# File 'lib/doubly_linkedlist/list.rb', line 57

def index(value)
  find_index(value, 0, @head, :next, 1)
end

#insert(value) ⇒ Integer Also known as: push

Inserts a node with the given value into the head of the list, increments and returns the count of nodes.

Parameters:

  • value (Object)

    the node value to be inserted into the list.

Returns:

  • (Integer)

    the number of nodes after insertion.



75
76
77
78
79
80
81
82
# File 'lib/doubly_linkedlist/list.rb', line 75

def insert(value)
  new_node = Node.new(value)
  @head.prev = new_node if @head
  new_node.next = @head
  @tail = new_node unless @tail
  @head = new_node
  @count += 1
end

#popObject Also known as: deque

Pops out and returns the node_value at the list head.

Returns:

  • (Object)

    the popped out node value.



129
130
131
# File 'lib/doubly_linkedlist/list.rb', line 129

def pop
  delete_at(0)
end

#rindex(value) ⇒ Integer, Nil

Returns the rightmost index of value present in the list. Returns nil if the value is not present.

Parameters:

  • value (Object)

    the node value for which to be looked up.

Returns:

  • (Integer, Nil)

    the index of the value passed in, or nil if value is not present.



66
67
68
# File 'lib/doubly_linkedlist/list.rb', line 66

def rindex(value)
  find_index(value, count - 1, @tail, :prev, -1)
end

#tailObject, Nil Also known as: last

Returns the value at the tail of the list if present, nil otherwise.

Returns:

  • (Object, Nil)

    the node value object or nil.



34
35
36
# File 'lib/doubly_linkedlist/list.rb', line 34

def tail
  @tail.value if @tail
end

#to_sString Also known as: inspect

Converts the list representation into a string.

Returns:

  • (String)

    the string object after converting the representation of list into string.



138
139
140
141
142
143
144
145
146
147
# File 'lib/doubly_linkedlist/list.rb', line 138

def to_s
  str = "<#{self.class}: ["

  each_with_index do |v, i|
    str += v.to_s
    str += ', ' unless i == (count - 1)
  end

  str + ']>'
end