Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions linked_list/00-implement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

class LinkedListNode
{
constructor(data){
this.data = data;
}

getData(){
return this.data;
}

setData(val){
this.data = val;
}

getNext(){
return this.next;
}

setNext(addr){
this.next = addr;
}
}

class LinkedList
{
constructor(){
this.sentinel = new LinkedListNode(null);
}

getHead(){
return this.sentinel.next;
}

insert(val){
var node = new LinkedListNode(val);
node.setNext(this.sentinel.next);
this.sentinel.setNext(node);
}

delete(){
if(this.sentinel.next != null){
this.sentinel.next = this.sentinel.next.next;
}
}
}


module.exports.LinkedList = LinkedList;

// var cur_node = ll.getHead();
// for(var i = 1 ; i <= 10; i++){
// console.log(cur_node.getData());
// cur_node = cur_node.getNext();
// }
52 changes: 52 additions & 0 deletions linked_list/01-find-nth-node-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Write a function that takes 2 arguments; a linked list (ll) and on integer(n).
// The function should return the value of the nth node in the linked list.
// Assume that the linked is in non-empty and is of length at least 'n'

// require_relative '00-implement'
var file = require('./00-implement.js');

function value_of_nth_node(ll, n){
var cur_node = ll.getHead();
for(var i = 1 ; i < n; i++){
cur_node = cur_node.getNext();
}
return cur_node.getData();
}

var ll = new file.LinkedList();
for(var i = 1; i <= 20; i++){
ll.insert(i);
// console.log(ll);
}

console.log(value_of_nth_node(ll,5));


// require "minitest/autorun"
//
// class TestMeme < Minitest::Test
// def setup
// @long_linked_list = LinkedList.new
// (1..100).each do |i|
// @long_linked_list.insert(i)
// end
// @short_linked_list = LinkedList.new
// @short_linked_list.insert(1)
// end
//
// def test_first_node_of_long_linked_list
// assert_equal 100, value_of_nth_node(@long_linked_list, 1)
// end
//
// def test_last_node_of_long_linked_list
// assert_equal 1, value_of_nth_node(@long_linked_list, 100)
// end
//
// def test_middle_node_of_long_linked_list
// assert_equal 46, value_of_nth_node(@long_linked_list, 55)
// end
//
// def test_first_node_of_short_linked_list
// assert_equal 1, value_of_nth_node(@short_linked_list, 1)
// end
// end
55 changes: 50 additions & 5 deletions linked_list/01A-find-nth-value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,58 @@
#
# Notice that this problem has no assumptions that we had in the earlier problem.

require_relative 'linked_list'

def value_of_nth_node(ll, n)
current_node = ll.head
(n-1).times do
if current_node.next != nil
current_node = current_node.next
else
return nil
end
end
return current_node.value
end

@long_linked_list = LinkedList.new
(1..100).each do |i|
@long_linked_list.insert(i)
end
@short_linked_list = LinkedList.new
@short_linked_list.insert(1)

require "minitest/autorun"
puts value_of_nth_node(@long_linked_list, 101)

class TestValueOfNthNode < Minitest::Test
def setup
end
end
# require "minitest/autorun"
#
# class TestValueOfNthNode < Minitest::Test
# def setup
# @long_linked_list = LinkedList.new
# (1..100).each do |i|
# @long_linked_list.insert(i)
# end
# @short_linked_list = LinkedList.new
# @short_linked_list.insert(1)
# end
#
# def test_first_node_of_long_linked_list
# assert_equal 100, value_of_nth_node(@long_linked_list, 1)
# end
#
# def test_last_node_of_long_linked_list
# assert_equal 1, value_of_nth_node(@long_linked_list, 100)
# end
#
# def test_middle_node_of_long_linked_list
# assert_equal 46, value_of_nth_node(@long_linked_list, 55)
# end
#
# def test_first_node_of_short_linked_list
# assert_equal 1, value_of_nth_node(@short_linked_list, 1)
# end
#
# def test_nth_node_does_not_exist
# assert_equal nil, value_of_nth_node(@short_linked_list, 2)
# end
# end
27 changes: 26 additions & 1 deletion linked_list/02-find-nth-from-last.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
#
# Notice that this problem has no assumptions that we had in the earlier problem.

def find_nth_from_last(ll, n)
require_relative 'linked_list'

def find_nth_from_last(ll, n)
len = ll.length
if n > len
return nil
else
n = len - n + 1
end
current_node = ll.head
(n-1).times do
if current_node.next != nil
current_node = current_node.next
else
return nil
end
end
return current_node.value
end

@long_linked_list = LinkedList.new
(1..100).each do |i|
@long_linked_list.insert(i)
end
@short_linked_list = LinkedList.new
@short_linked_list.insert(1)

puts find_nth_from_last(@long_linked_list, 97)
26 changes: 26 additions & 0 deletions linked_list/04-find-start-of-circular-linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
# Assume that the linked list is circular.
# Circular linked list is a linked list of the shape 9

require_relative 'linked_list'

def find_index_of_start_of_loop(ll)
ele1 = ll.head.next
ele2 = ll.head.next.next

while ele1 != ele2
ele1 = ele1.next
ele2 = ele2.next.next
end

ele1 = ll.head

while ele1 != ele2
ele1 = ele1.next
ele2 = ele2.next
end
return ele2.value
end

# ll = LinkedList.new
# ll.insert(1)
# tail = ll.head
# 6.times do |i|
# ll.insert(i+1+1)
# end
#
# tail.next = ll.head.next.next.next
# puts find_index_of_start_of_loop(ll)
15 changes: 13 additions & 2 deletions linked_list/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ def insert(val)
def delete
@sentinel.next = @sentinel.next.try(:next)
end
end

class TestLinkedList < Minitest::Test
def length
len = 0
current_node = @sentinel
while !current_node.next.nil?
len += 1
current_node = current_node.next
end

return len
end
end

# class TestLinkedList < Minitest::Test
# end
51 changes: 50 additions & 1 deletion queue/kueue_as_array.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
# Implement a Queue using Array
class Kueue
class Kueue < Array
attr_accessor :tail_index, :head_index


def push(val)
puts self.tail_index
puts self.head_index
raise "StackOverflow" if (self.tail_index == self.head_index) && (self.head_index != nil)

self.tail_index ||= -1
self.head_index ||= -1
self[(self.tail_index + 1)%self.length] = val
self.tail_index = (self.tail_index + 1)%self.length
end

def pop
raise "StackUnderflow" if self.head_index.nil?

self.head_index = (self.head_index + 1)%self.length
val = self[self.head_index]
self[self.head_index] = nil
self.head_index = nil if self.head_index == -1

return val
end

def peep
return nil if ((self.head_index + 1)%self.length).nil?

return self[(self.head_index + 1)%self.length]
end
end

# s = Kueue.new(8)
# 8.times do |i|
# s.push(i+1)
# end
# puts s.inspect
# s.pop
# s.pop
# s.pop
# s.pop
# puts s.inspect
# s.push(11)
# s.push(12)
# s.push(13)
# s.push(14)
# puts s.inspect
# s.pop
# s.push(15)
# puts s.inspect
28 changes: 27 additions & 1 deletion queue/kueue_as_linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# Implement a Queue using LinkedList
class Kueue
require_relative '../linked_list/linked_list'

class Kueue < LinkedList
def peep
return @sentinel.next.value
end

def push(val)
@tail ||= @sentinel
node = LinkedListNode.new(val)
@tail.next = node
@tail = node
end

def pop
@sentinel.next = @sentinel.next.try(:next)
end
end

# @kueue = Kueue.new
# 4.times do |i|
# @kueue.push(i+1)
# end
# puts @kueue.peep
# puts @kueue.inspect
# @kueue.pop
# puts @kueue.peep
# puts @kueue.inspect
2 changes: 1 addition & 1 deletion stack/stack_as_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Stack < Array
def push(val)
raise "StackOverflow" if self.tail_index == self.length - 1

self.tail_index || = -1
self.tail_index ||= -1
self.tail_index += 1
self[self.tail_index] = val
end
Expand Down
24 changes: 24 additions & 0 deletions stack/stack_as_linked_list.rb
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# Implement a Stack using LinkedList
require_relative '../linked_list/linked_list'
class Stack < LinkedList
def peep
return @sentinel.next.value
end

def push(val)
node = LinkedListNode.new(val)
node.next = head
@sentinel.next = node
end

def pop
@sentinel.next = @sentinel.next.try(:next)
end
end

# @stack = Stack.new
# 4.times do |i|
# @stack.push(i+1)
# puts @stack.peep
# end
# @stack.pop
# puts @stack.peep