August 2020
Intermediate to advanced
508 pages
11h 53m
English
Some programming languages, such as Java, come with linked lists built into the language. However, many languages do not, and it’s fairly simple to implement them on our own.
Let’s create our own linked list using Ruby. We’ll use two classes to implement this: Node and LinkedList. Let’s create the Node class first:
| | class Node |
| | |
| | attr_accessor :data, :next_node |
| | |
| | def initialize(data) |
| | @data = data |
| | end |
| | |
| | end |
The Node class has two attributes: data contains the node’s primary value (for example, the string "a"), while next_node contains the link to the next node in the list. We can use this class as follows:
| | node_1 = Node.new("once") |
| | node_2 = Node.new("upon") |
| | node_3 = Node.new("a" |
Read now
Unlock full access