Class: BasicLinkedList

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicLinkedList

Returns a new instance of BasicLinkedList.



5
6
7
# File 'lib/basic_linked_list.rb', line 5

def initialize
  @head = nil
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/basic_linked_list.rb', line 4

def head
  @head
end

Instance Method Details

#append(element) ⇒ Object



9
10
11
12
13
# File 'lib/basic_linked_list.rb', line 9

def append(element)
  new_node = Node.new(element)
  @head.nil? ? (@head = new_node) : (last_node.next_node = new_node)
  new_node.data
end

#count(node = @head, counter = 0) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/basic_linked_list.rb', line 22

def count(node = @head, counter = 0)
  return counter if node.nil?
  counter += 1
  return counter if node.next_node.nil?
  node = node.next_node
  count(node, counter)
end

#find(start_position, num_of_elements) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/basic_linked_list.rb', line 57

def find(start_position, num_of_elements)
  start_node = @head
  start_position.times do
    return "Error: start position outside linkedlist" if start_node.next_node.nil?
    start_node = start_node.next_node
  end
  return "" if num_of_elements == 0
  
  str = start_node.data.to_s
  return str if start_node.next_node.nil?

  node = start_node.next_node
  (num_of_elements - 1).times do
    str << " " + node.data
    return str if node.next_node.nil?
    node = node.next_node
  end
  str
end

#includes?(element) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/basic_linked_list.rb', line 88

def includes?(element)
  node = @head
  until node.next_node.nil?
    return true if node.data == element
    node = node.next_node
  end
  false
end

#insert(position, element) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/basic_linked_list.rb', line 45

def insert(position, element)
  return prepend(element) if position == 0
  new_node = Node.new(element)
  node_before_insertion = @head
  (position - 1).times do
    node_before_insertion = node_before_insertion.next_node
  end
  new_node.next_node = node_before_insertion.next_node
  node_before_insertion.next_node = new_node
  new_node.data
end

#last_node(node = @head) ⇒ Object



15
16
17
18
19
20
# File 'lib/basic_linked_list.rb', line 15

def last_node(node = @head)
  return node if node.nil? 
  return node if node.next_node.nil?
  node = node.next_node
  last_node(node)
end

#popObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/basic_linked_list.rb', line 77

def pop
  node = @head
  previous_node = nil
  until node.next_node.nil? do
    previous_node = node
    node = node.next_node
  end
  previous_node.next_node = nil
  node.data
end

#prepend(element) ⇒ Object



38
39
40
41
42
43
# File 'lib/basic_linked_list.rb', line 38

def prepend(element)
  new_head = Node.new(element)
  new_head.next_node = @head
  @head = new_head
  @head.data
end

#to_string(node = @head, str = "") ⇒ Object



30
31
32
33
34
35
36
# File 'lib/basic_linked_list.rb', line 30

def to_string(node = @head, str = "")
  return str if node.nil?
  node == @head ? (str << node.data.to_s) : (str << " " + node.data.to_s)
  return str if node.next_node.nil?
  node = node.next_node
  to_string(node,str)
end