How to Implement Linked List in Ruby: Simple Guide
To implement a linked list in Ruby, create a
Node class to hold data and a reference to the next node, then build a LinkedList class to manage nodes. Use methods to add, remove, and traverse nodes, linking each node to the next.Syntax
A linked list in Ruby typically uses two classes: Node and LinkedList.
Nodeholds the value and a reference to the next node.LinkedListmanages the list by keeping track of the head node and providing methods to add or remove nodes.
ruby
class Node attr_accessor :value, :next def initialize(value) @value = value @next = nil end end class LinkedList attr_accessor :head def initialize @head = nil end def append(value) new_node = Node.new(value) if @head.nil? @head = new_node else current = @head current = current.next while current.next current.next = new_node end end def display current = @head while current print "#{current.value} -> " current = current.next end puts "nil" end end
Example
This example shows how to create a linked list, add nodes, and display the list.
ruby
class Node attr_accessor :value, :next def initialize(value) @value = value @next = nil end end class LinkedList attr_accessor :head def initialize @head = nil end def append(value) new_node = Node.new(value) if @head.nil? @head = new_node else current = @head current = current.next while current.next current.next = new_node end end def display current = @head while current print "#{current.value} -> " current = current.next end puts "nil" end end list = LinkedList.new list.append(10) list.append(20) list.append(30) list.display
Output
10 -> 20 -> 30 -> nil
Common Pitfalls
Common mistakes when implementing linked lists in Ruby include:
- Not updating the
nextpointer correctly, causing broken links. - Forgetting to handle the case when the list is empty (
headisnil). - Not traversing the list properly, which can cause infinite loops or missed nodes.
Always check if head is nil before traversing, and update next pointers carefully.
ruby
class LinkedList attr_accessor :head def initialize @head = nil end # Wrong: Does not handle empty list def append_wrong(value) return @head = Node.new(value) if @head.nil? current = @head current = current.next while current.next current.next = Node.new(value) end # Right: Handles empty list def append_right(value) new_node = Node.new(value) if @head.nil? @head = new_node else current = @head current = current.next while current.next current.next = new_node end end end
Quick Reference
- Node: Holds data and pointer to next node.
- LinkedList: Manages nodes, tracks head.
- append(value): Adds new node at end.
- display: Prints all node values.
- Always check for empty list before operations.
Key Takeaways
A linked list uses nodes that hold data and a reference to the next node.
Always handle the empty list case when adding or traversing nodes.
Traverse nodes carefully to avoid infinite loops or missing elements.
Use clear methods like append and display to manage the list.
Test your linked list with simple examples to ensure correctness.