From ab6887f65994b630fe2179bf37b5720243255a6f Mon Sep 17 00:00:00 2001 From: Dhanunjaya Lakshmi Date: Fri, 13 May 2016 16:11:18 +0530 Subject: [PATCH 1/3] Linked list implementation and finding nth element of a list from the beginning in javascript --- linked_list/00-implement.js | 56 +++++++++++++++++++++++++++ linked_list/01-find-nth-node-value.js | 52 +++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 linked_list/00-implement.js create mode 100644 linked_list/01-find-nth-node-value.js diff --git a/linked_list/00-implement.js b/linked_list/00-implement.js new file mode 100644 index 0000000..e6dd1ef --- /dev/null +++ b/linked_list/00-implement.js @@ -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(); +// } diff --git a/linked_list/01-find-nth-node-value.js b/linked_list/01-find-nth-node-value.js new file mode 100644 index 0000000..4db6b76 --- /dev/null +++ b/linked_list/01-find-nth-node-value.js @@ -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 From 947e409971f2f0c59c91c3ad7dd62cac24934458 Mon Sep 17 00:00:00 2001 From: Dhanunjaya Lakshmi Date: Mon, 16 May 2016 15:38:38 +0530 Subject: [PATCH 2/3] Partially completed --- linked_list/01A-find-nth-value.rb | 55 +++++++++++++++++-- linked_list/02-find-nth-from-last.rb | 27 ++++++++- .../04-find-start-of-circular-linked-list.rb | 4 +- linked_list/linked_list.rb | 15 ++++- 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/linked_list/01A-find-nth-value.rb b/linked_list/01A-find-nth-value.rb index 8eb2709..da97280 100644 --- a/linked_list/01A-find-nth-value.rb +++ b/linked_list/01A-find-nth-value.rb @@ -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 diff --git a/linked_list/02-find-nth-from-last.rb b/linked_list/02-find-nth-from-last.rb index 2f17c82..14f94cf 100644 --- a/linked_list/02-find-nth-from-last.rb +++ b/linked_list/02-find-nth-from-last.rb @@ -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) diff --git a/linked_list/04-find-start-of-circular-linked-list.rb b/linked_list/04-find-start-of-circular-linked-list.rb index cb6706f..19a202b 100644 --- a/linked_list/04-find-start-of-circular-linked-list.rb +++ b/linked_list/04-find-start-of-circular-linked-list.rb @@ -3,6 +3,8 @@ # Assume that the linked list is circular. # Circular linked list is a linked list of the shape 9 -def find_index_of_start_of_loop(ll) +require_relative 'linked_list' +def find_index_of_start_of_loop(ll) + end diff --git a/linked_list/linked_list.rb b/linked_list/linked_list.rb index 4710f66..5a5dae8 100644 --- a/linked_list/linked_list.rb +++ b/linked_list/linked_list.rb @@ -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 From 740ef281a36c1badf7546f6d4f289bd6b6d75e52 Mon Sep 17 00:00:00 2001 From: Dhanunjaya Lakshmi Date: Tue, 17 May 2016 16:20:56 +0530 Subject: [PATCH 3/3] Done with the implementation of stack and queue --- .../04-find-start-of-circular-linked-list.rb | 26 +++++++++- queue/kueue_as_array.rb | 51 ++++++++++++++++++- queue/kueue_as_linked_list.rb | 28 +++++++++- stack/stack_as_array.rb | 2 +- stack/stack_as_linked_list.rb | 24 +++++++++ 5 files changed, 127 insertions(+), 4 deletions(-) diff --git a/linked_list/04-find-start-of-circular-linked-list.rb b/linked_list/04-find-start-of-circular-linked-list.rb index 19a202b..e6923a3 100644 --- a/linked_list/04-find-start-of-circular-linked-list.rb +++ b/linked_list/04-find-start-of-circular-linked-list.rb @@ -6,5 +6,29 @@ 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) diff --git a/queue/kueue_as_array.rb b/queue/kueue_as_array.rb index cc0cd5a..ae6b23a 100644 --- a/queue/kueue_as_array.rb +++ b/queue/kueue_as_array.rb @@ -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 diff --git a/queue/kueue_as_linked_list.rb b/queue/kueue_as_linked_list.rb index 3c6ec18..c5a2d08 100644 --- a/queue/kueue_as_linked_list.rb +++ b/queue/kueue_as_linked_list.rb @@ -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 diff --git a/stack/stack_as_array.rb b/stack/stack_as_array.rb index a97ca00..20f5109 100644 --- a/stack/stack_as_array.rb +++ b/stack/stack_as_array.rb @@ -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 diff --git a/stack/stack_as_linked_list.rb b/stack/stack_as_linked_list.rb index cb663f3..82b717b 100644 --- a/stack/stack_as_linked_list.rb +++ b/stack/stack_as_linked_list.rb @@ -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