Approach to Create a Linked List

  • Create a class for Node which has an attribute for value and the next node.
  • Create a class for LinkedList with methods to add elements, remove elements, and other necessary operations.

Node Class

The Node class represents each item of the linked list. It has two attributes: value which stores the data, and next_node which keeps track of what follows after it in the list. The initialize function sets up the beginning value of this node along with initializes next_node as nil.

class Node
attr_accessor :value, :next_node
def initialize(value)
@value = value
@next_node = nil
end
end

Linked List Class

The LinkedList class represents all nodes available in an entire linked list. Initially pointing to first node in a list, @head is its only instance variable at first since there isn’t any item on that list.

class LinkedList
def initialize
@head = nil
end

Adding Nodes

  • The add method adds an element to the linked list at its end.
  • If the linked list is empty (the head pointer i.e. @head is nil), we create a new node and make it point to @head.
  • Otherwise, it traverses the list until it gets to the last node (node.next_node =nil) and then inserts anode after it.

def add(value)
if @head.nil?
@head = Node.new(value)
else
current = @head
while current.next_node != nil
current = current.next_node
end
current.next_node = Node.new(value)
end
end

Removing Nodes

  • The remove method removes the first occurrence of a node with the given value from the linked list.
  • If that node is found in the beginning of the list, @head should be updated so that it points to next_node.
  • Else, go through all nodes starting from start till one before current and update reference for next_node for this previous one to skip over current one being removed.

def remove(value)
current = @head
if current.value == value
@head = current.next_node
else
while (current.next_node != nil) && (current.next_node.value != value)
current = current.next_node
end
if current.next_node != nil
current.next_node = current.next_node.next_node
end
end
end

Displaying Linked List

  • The display method prints values of all nodes in a linked list.
  • It starts at @head and goes on outputting each node’s value followed by “->” until reaching nil which means end of a cell structure.

def display
current = @head
while current != nil
print “#{current.value} -> “
current = current.next_node
end
puts “nil”
end

How to Create a Linked List in Ruby?

A linked list is a form of linear data structure whereby the elements are not positioned closely together in memory. Each element here instead points to the subsequent one, thus leading to a chain-like arrangement.

Creating a linked list in Ruby entails defining an information structure where each node has value and link (or reference) to the next node in the sequence. It is also dynamic meaning it can grow or shrink during runtime making it efficient for specific operations such as insertion and deletion.

Similar Reads

Approach to Create a Linked List

Create a class for Node which has an attribute for value and the next node.Create a class for LinkedList with methods to add elements, remove elements, and other necessary operations....

Implementation of Linked List using Ruby

Below Ruby program demonstrates creating a linked list, adding nodes to it, displaying its contents, and removing a node....