Class: Algorithmable::DataStructs::LinkedList::Doubly

Inherits:
Base
  • Object
show all
Defined in:
lib/algorithmable/data_structs/linked_list/doubly.rb

Instance Method Summary collapse

Instance Method Details

#delete(item) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/algorithmable/data_structs/linked_list/doubly.rb', line 55

def delete(item)
  node = search(item)
  return unless node

  prev_node = node.prev
  next_node = node.next

  if prev_node.nil?
    @front = next_node
  else
    prev_node.next = next_node
  end

  if next_node.nil?
    @back = prev_node
  else
    next_node.prev = prev_node
  end

  @size -= 1
  item
end

#pop_backObject


51
52
53
# File 'lib/algorithmable/data_structs/linked_list/doubly.rb', line 51

def pop_back
  delete peek_back
end

#pop_frontObject


47
48
49
# File 'lib/algorithmable/data_structs/linked_list/doubly.rb', line 47

def pop_front
  delete peek_front
end

#push_back(item) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/algorithmable/data_structs/linked_list/doubly.rb', line 32

def push_back(item)
  prev_back = @back
  node = Node.new item, prev_back, nil
  @back = node

  if prev_back
    prev_back.next = node
  else
    @front = node
  end

  @size += 1
  item
end

#push_front(item) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/algorithmable/data_structs/linked_list/doubly.rb', line 17

def push_front(item)
  prev_front = @front
  node = new_node item, nil, prev_front
  @front = node

  if prev_front
    prev_front.prev = node
  else
    @back = node
  end

  @size += 1
  item
end