Class: LinkedList::List

Inherits:
Object
  • Object
show all
Includes:
Conversions
Defined in:
lib/linked-list/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Conversions

List, Node

Constructor Details

#initializeList

Returns a new instance of List.



8
9
10
11
12
# File 'lib/linked-list/list.rb', line 8

def initialize
  @head   = nil
  @tail   = nil
  @length = 0
end

Instance Attribute Details

#lengthObject (readonly) Also known as: size

Returns the value of attribute length.



5
6
7
# File 'lib/linked-list/list.rb', line 5

def length
  @length
end

Instance Method Details

#eachObject

Iterates over nodes from top to bottom passing node data to the block if given. If no block given, returns Enumerator.

Returns:

Enumerator or yields data to the block stored in every node on the list.



132
133
134
135
# File 'lib/linked-list/list.rb', line 132

def each
  return to_enum(__callee__) unless block_given?
  __each { |node| yield(node.data) }
end

#firstObject

Returns the first element of the list or nil.



16
17
18
# File 'lib/linked-list/list.rb', line 16

def first
  @head && @head.data
end

#inspectObject



144
145
146
# File 'lib/linked-list/list.rb', line 144

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

#lastObject

Returns the last element of the list or nil.



22
23
24
# File 'lib/linked-list/list.rb', line 22

def last
  @tail && @tail.data
end

#popObject

Removes data from the end of the list.

Returns:

Data stored in the node or nil.



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

def pop
  return nil unless @head

  tail = __pop
  @head = nil unless @tail

  @length -= 1
  tail.data
end

#push(node) ⇒ Object Also known as: <<

Pushes new nodes to the end of the list.

Parameters:

node

Any object, including Node objects.

Returns:

self of List object.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linked-list/list.rb', line 34

def push(node)
  node = Node(node)
  @head ||= node

  if @tail
    @tail.next = node
    node.prev = @tail
  end

  @tail = node

  @length += 1
  self
end

#reverseObject

Reverse list of nodes and returns new instance of the list.

Returns:

New List in reverse order.



105
106
107
# File 'lib/linked-list/list.rb', line 105

def reverse
  List(to_a).reverse!
end

#reverse!Object

Reverses list of nodes in place.

Returns:

self in reverse order.



114
115
116
117
118
119
120
121
122
123
# File 'lib/linked-list/list.rb', line 114

def reverse!
  return self unless @head

  __each do |curr_node|
    curr_node.prev, curr_node.next = curr_node.next, curr_node.prev
  end
  @head, @tail = @tail, @head

  self
end

#shiftObject

Removes data from the beginning of the list.

Returns:

Data stored in the node or nil.



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

def shift
  return nil unless @head

  head = __shift
  @tail = nil unless @head

  @length -= 1
  head.data
end

#to_aObject Also known as: to_ary

Converts list to array.



139
140
141
# File 'lib/linked-list/list.rb', line 139

def to_a
  each.to_a
end

#to_listObject

Conversion function, see Conversions.List.

Returns:

self



153
154
155
# File 'lib/linked-list/list.rb', line 153

def to_list
  self
end

#unshift(node) ⇒ Object

Pushes new nodes on top of the list.

Parameters:

node

Any object, including Node objects.

Returns:

self of List object.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/linked-list/list.rb', line 58

def unshift(node)
  node = Node(node)
  @tail ||= node

  node.next = @head
  @head.prev = node if @head
  @head = node

  @length += 1
  self
end