Class: DSAVisualizer::DataStructures::LinkedList

Inherits:
Object
  • Object
show all
Defined in:
lib/dsa_visualizer/data_structures/linked_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinkedList

Returns a new instance of LinkedList.



15
16
17
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 15

def initialize
  @head = nil
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



13
14
15
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 13

def head
  @head
end

Class Method Details

.demoObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 50

def self.demo
  Visualizer.print_header("LINKED LIST - Core Level Visualization")
  
  # Memory Model
  Visualizer.print_section("1. Memory Model Comparison")
  
  ruby_code = <<~RUBY
    class Node
      attr_accessor :data, :next
      def initialize(data)
        @data = data
        @next = nil
      end
    end
    # Each node is a Ruby object
    # @next stores reference to next node
  RUBY

  cpp_code = <<~CPP
    struct Node {
      int data;
      Node* next;
      Node(int d) : data(d), next(nullptr) {}
    };
    // Each node allocated on heap
    // next is explicit pointer
  CPP

  explanation = "Ruby uses object references (implicit pointers), C++ uses explicit pointers. Both achieve same structure but Ruby adds GC overhead while C++ requires manual memory management."
  
  Visualizer.print_comparison(ruby_code, cpp_code, explanation)
  
  # Creation
  Visualizer.print_section("2. Creating Linked List")
  tracker = MemoryTracker.new
  list = LinkedList.new
  
  Visualizer.print_step(1, "Adding nodes: 10, 20, 30")
  
  [10, 20, 30].each do |value|
    list.append(value)
    tracker.track_operation("Append", "Added node with value #{value}")
  end
  
  puts "\n" + list.visualize
  
  puts "\nšŸ“¦ Memory Layout:"
  current = list.head
  index = 0
  while current
    puts "  Node #{index}:"
    puts "    Address: 0x#{current.object_id.to_s(16)}"
    puts "    Data: #{current.data}"
    puts "    Next: #{current.next ? "0x#{current.next.object_id.to_s(16)}" : "nil"}"
    current = current.next
    index += 1
  end
  
  puts "\nšŸ” Ruby Internals:"
  puts "  - Each Node is a Ruby object (heap allocated)"
  puts "  - @next is object reference (managed by GC)"
  puts "  - No manual memory management needed"
  puts "  - Overhead: object header + reference tracking"
  
  puts "\nšŸ” C++ Internals:"
  puts "  - Each Node allocated with 'new'"
  puts "  - next is raw pointer (8 bytes on 64-bit)"
  puts "  - Must manually delete nodes"
  puts "  - No overhead beyond struct size"
  
  # Traversal
  Visualizer.print_section("3. Traversal")
  Visualizer.print_step(2, "Traversing to find value 20")
  
  puts "\nProcess:"
  current = list.head
  position = 0
  while current
    if current.data == 20
      puts "  Position #{position}: [#{current.data}] ← FOUND!".colorize(:green)
    else
      puts "  Position #{position}: [#{current.data}]"
    end
    current = current.next
    position += 1
  end
  
  Comparator.compare_complexity(
    "Linked List Traversal",
    "O(n) - must follow references",
    "O(n) - must follow pointers",
    "Both require sequential access. No random access like arrays. Cache performance worse due to non-contiguous memory."
  )
  
  # Insertion
  Visualizer.print_section("4. Insertion at Beginning")
  Visualizer.print_step(3, "Inserting 5 at head")
  
  puts "\nBefore: " + list.visualize
  
  new_node = Node.new(5)
  new_node.next = list.head
  list.instance_variable_set(:@head, new_node)
  tracker.track_operation("Insert", "Inserted 5 at head")
  
  puts "After:  " + list.visualize
  
  puts "\nšŸ”„ Ruby Process:"
  puts "  1. Create new Node object (GC manages)"
  puts "  2. Set new_node.next = current head"
  puts "  3. Update head reference"
  puts "  4. O(1) operation"
  
  puts "\nšŸ”„ C++ Process:"
  puts "  1. Allocate: Node* new_node = new Node(5)"
  puts "  2. Set: new_node->next = head"
  puts "  3. Update: head = new_node"
  puts "  4. O(1) operation"
  
  # Memory comparison
  Visualizer.print_section("5. Memory Comparison")
  
  puts "\nšŸ“Š Array vs Linked List:"
  puts "\nArray:"
  puts "  āœ“ Contiguous memory (better cache)"
  puts "  āœ“ O(1) random access"
  puts "  āœ— O(n) insertion/deletion (middle)"
  puts "  āœ— Fixed size (or expensive resize)"
  
  puts "\nLinked List:"
  puts "  āœ“ O(1) insertion/deletion (at known position)"
  puts "  āœ“ Dynamic size (no resize needed)"
  puts "  āœ— O(n) random access"
  puts "  āœ— Poor cache locality"
  puts "  āœ— Extra memory for pointers/references"
  
  tracker.print_summary
  
  puts "\n\nšŸŽÆ Key Takeaways:".colorize(:green).bold
  puts "  1. Linked lists trade random access for efficient insertion/deletion"
  puts "  2. Ruby's GC simplifies memory management vs C++ manual management"
  puts "  3. Non-contiguous memory means poor cache performance"
  puts "  4. Each node has pointer/reference overhead"
  puts "  5. Best for frequent insertions/deletions, not random access"
end

.learnObject



46
47
48
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 46

def self.learn
  demo
end

Instance Method Details

#append(data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 19

def append(data)
  new_node = Node.new(data)
  
  if @head.nil?
    @head = new_node
    return
  end

  current = @head
  current = current.next while current.next
  current.next = new_node
end

#visualizeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dsa_visualizer/data_structures/linked_list.rb', line 32

def visualize
  return "Empty list" if @head.nil?
  
  result = ""
  current = @head
  while current
    result += "[#{current.data}|•]"
    result += " -> " if current.next
    current = current.next
  end
  result += " -> nil"
  result
end