Skip to content

Instantly share code, notes, and snippets.

@weilandia
Created April 22, 2016 21:47
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 weilandia/465d6cc4ef892ff2bddf66912184c5e8 to your computer and use it in GitHub Desktop.
Save weilandia/465d6cc4ef892ff2bddf66912184c5e8 to your computer and use it in GitHub Desktop.
#version 1
class Deque
def initialize
@head = nil
@tail = nil
end
def push(data)
if @head.nil?
@head = Node.new(data)
@tail = @head
else
current_node = @head
until current_node.next_node == nil
current_node = current_node.next_node
end
current_node.next_node = Node.new(data, current_node)
@tail = current_node.next_node
end
end
def pop
data = @tail.data
if @tail == @head
@head = nil
@tail = nil
else
@tail = @tail.previous_node
@tail.next_node = nil
end
data
end
def shift
data = @head.data
if @tail == @head
@head = nil
@tail = nil
else
@head = @head.next_node
@head.previous_node = nil
end
data
end
def unshift(data)
if @head.nil?
@head = Node.new(data)
@tail = @head
@head.data
else
@head.previous_node = Node.new(data, nil, @head)
@head = @head.previous_node
end
end
end
class Node
attr_accessor :data, :next_node, :previous_node
def initialize(data, previous_node = nil, next_node = nil)
@data = data
@next_node = next_node
@previous_node = previous_node
end
end
#version 2
class Deque
def initialize
@head = nil
end
def node_step_push(head, data)
current_node = head
until current_node.next_node == nil
current_node = current_node.next_node
end
current_node.next_node = Node.new(data, current_node)
end
def node_step_pop(head)
current_node = head
until current_node.next_node.next_node == nil
current_node = current_node.next_node
end
data = current_node.next_node.data
current_node.next_node = nil
data
end
def push(data)
if @head.nil?
@head = Node.new(data)
else
node_step_push(@head, data)
end
end
def pop
if @head.next_node.nil?
data = @head.data
@head = nil
else
data = node_step_pop(@head)
end
data
end
def shift
data = @head.data
if @head.next_node.nil?
@head = nil
else
@head = @head.next_node
@head.previous_node = nil
end
data
end
def unshift(data)
if @head.nil?
@head = Node.new(data)
@head.data
else
@head.previous_node = Node.new(data, nil, @head)
@head = @head.previous_node
end
end
end
class Node
attr_accessor :data, :next_node, :previous_node
def initialize(data, previous_node = nil, next_node = nil)
@data = data
@next_node = next_node
@previous_node = previous_node
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment