Skip to content

Instantly share code, notes, and snippets.

@thiensubs
Last active April 11, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiensubs/47b47e487adcbdced58679f65cdb5eda to your computer and use it in GitHub Desktop.
Save thiensubs/47b47e487adcbdced58679f65cdb5eda to your computer and use it in GitHub Desktop.
This is ruby code implement for single linked list (List in DSA - data structures and algorithms)
class Node
attr_accessor :next
attr_reader :value
def initialize(value)
@value = value
@next = nil
end
def to_s
"Node with value: #{@value}"
end
end
In irb or pry.
require_relative './linked_list.rb'
list = LinkedList.new
list.find('333')
list.add_after('222', Node.new('333'))
list.insert_head('1111')
list.print
list.insert_tail('9999')
list.add_after('333', Node.new('444'))
list
list.remove_head
list.pick_after('444')
list.print
list.insert_tail('101010')
list.pick_node('9999')
list.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment