Skip to content

Instantly share code, notes, and snippets.

@gkop
Created February 4, 2015 05:02
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 gkop/02b4560d898c6ff13e60 to your computer and use it in GitHub Desktop.
Save gkop/02b4560d898c6ff13e60 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :next_node, :previous_node
def initialize(value, next_node, previous_node)
@value = value
@next_node = next_node
@previous_node = previous_node
end
end
class DoublyLinkedList
def initialize
# maybe later?
end
def empty?
@head == nil
end
def shift
node_to_return = @head
if node_to_return
@head = @head.next_node
@head.previous_node = nil
if @tail.previous_node == node_to_return
@tail.previous_node = nil
end
node_to_return.value
else
raise "You can't shift an empty list"
# (returning nil would also be
# acceptable here)
end
end
def append(value)
if empty?
# our list is empty
# that means @head and @tail are nil
@head = @tail = Node.new(value, nil, nil)
else
new_node = Node.new(value, nil, @tail)
@tail.next_node = new_node
@tail = new_node
end
self
end
def prepend(value)
if empty?
# our list is empty
# that means @head is nil
@head = @tail = Node.new(value, nil, nil)
else
new_node = Node.new(value, @head, nil)
@head.previous_node = new_node
@head = new_node
end
self
end
def to_s
cursor = @head
while cursor !=nil
print "#{cursor.value}<->"
cursor = cursor.next_node
end
print "\n"
end
end
binding.pry
my_array = ["apple", "pear", "orange"]
puts my_array.join(", ")
# What does an "arbitrary index" look like?
# for example, I want the second element...
puts "2nd element is #{my_array[1]}"
# (this is really fast with an array)
# What does an prepend look like?
my_array = my_array.unshift("pineapple")
puts my_array.join(", ")
# (this may be faster with a linked list than an array)
# What does an append look like?
my_array = my_array << "strawberry"
puts my_array.join(", ")
# (this may be faster with a linked list than an array)
class Node
attr_reader :value
attr_accessor :next_node
def initialize(value, next_node)
@value = value
@next_node = next_node
end
end
class LinkedList
def initialize
# maybe later?
end
def empty?
@head == nil
end
def shift
node_to_return = @head
if node_to_return
@head = @head.next_node
node_to_return.value
else
raise "You can't shift an empty list"
# (returning nil would also be
# acceptable here)
end
end
def append(value)
if empty?
# our list is empty
# that means @head and @tail are nil
@head = @tail = Node.new(value, nil)
else
new_node = Node.new(value, nil)
@tail.next_node = new_node
@tail = new_node
end
self
end
def prepend(value)
if empty?
# our list is empty
# that means @head is nil
@head = @tail = Node.new(value, nil)
else
new_node = Node.new(value, @head)
@head = new_node
end
self
end
def to_s
cursor = @head
while cursor !=nil
print "#{cursor.value}->"
cursor = cursor.next_node
end
print "\n"
end
end
class MyStack
def initialize
@list = LinkedList.new
end
def push(value)
@list.prepend(value)
self
end
def pop
@list.shift
end
end
class MyQueue
def initialize
@list = LinkedList.new
end
def enqueue(value)
@list.append(value)
self
end
def dequeue
@list.shift
end
end
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment