Skip to content

Instantly share code, notes, and snippets.

@jigewxy
Created October 18, 2017 12:07
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 jigewxy/39ff1b2b6f5eb749792149d0f6840ce7 to your computer and use it in GitHub Desktop.
Save jigewxy/39ff1b2b6f5eb749792149d0f6840ce7 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/zegafat
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function Node(data, node){
this.data = data;
this.next = node;
}
function LinkedList(head, tail){
this.head = head;
if(tail.next===null)
this.tail = tail;
else
{
tail.next = null;
this.tail = tail;
}
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode.data;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
var p3 = new Node(3, null);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p3);
var f = ll.getNode(4);
console.log(f);
</script>
<script id="jsbin-source-javascript" type="text/javascript">function Node(data, node){
this.data = data;
this.next = node;
}
function LinkedList(head, tail){
this.head = head;
if(tail.next===null)
this.tail = tail;
else
{
tail.next = null;
this.tail = tail;
}
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode.data;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
var p3 = new Node(3, null);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p3);
var f = ll.getNode(4);
console.log(f);</script></body>
</html>
function Node(data, node){
this.data = data;
this.next = node;
}
function LinkedList(head, tail){
this.head = head;
if(tail.next===null)
this.tail = tail;
else
{
tail.next = null;
this.tail = tail;
}
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode.data;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
var p3 = new Node(3, null);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p3);
var f = ll.getNode(4);
console.log(f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment