Skip to content

Instantly share code, notes, and snippets.

@jigewxy
Created October 18, 2017 23:26
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/c2f4d716158f80a781c34137bf496c4f to your computer and use it in GitHub Desktop.
Save jigewxy/c2f4d716158f80a781c34137bf496c4f 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.getLength = function(){
var len = 1;
var childNode = this.head;
if(this.head.next===null)
return 1;
while(childNode.next!==null)
{
childNode=childNode.next;
len++;
}
return len;
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
Linkedlist.prototype.insertNode = function(node, index){
var nextNode = this.getNode(index);
var prevNode = this.getNode(index-1);
prevNode.next = node;
node.next = nextNode;
}
var p4 = new Node(4, null);
var p3 = new Node(3, p4);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p4);
var f = ll.getNode(4);
var len = ll.getLength();
console.log(f);
console.log(len);
</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.getLength = function(){
var len = 1;
var childNode = this.head;
if(this.head.next===null)
return 1;
while(childNode.next!==null)
{
childNode=childNode.next;
len++;
}
return len;
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
Linkedlist.prototype.insertNode = function(node, index){
var nextNode = this.getNode(index);
var prevNode = this.getNode(index-1);
prevNode.next = node;
node.next = nextNode;
}
var p4 = new Node(4, null);
var p3 = new Node(3, p4);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p4);
var f = ll.getNode(4);
var len = ll.getLength();
console.log(f);
console.log(len);</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.getLength = function(){
var len = 1;
var childNode = this.head;
if(this.head.next===null)
return 1;
while(childNode.next!==null)
{
childNode=childNode.next;
len++;
}
return len;
}
LinkedList.prototype.getNode = function(index){
var val=0;
var destNode = this.head;
while(index>=0)
{
if(index===0)
{
val=destNode;
break;
}
destNode = destNode.next;
if(destNode===null)
{
val ="Oops! exceeds the maximum length";
break;
}
index--;
}
return val;
}
Linkedlist.prototype.insertNode = function(node, index){
var nextNode = this.getNode(index);
var prevNode = this.getNode(index-1);
prevNode.next = node;
node.next = nextNode;
}
var p4 = new Node(4, null);
var p3 = new Node(3, p4);
var p2 = new Node(2, p3);
var p1 = new Node(1, p2);
var ll = new LinkedList(p1, p4);
var f = ll.getNode(4);
var len = ll.getLength();
console.log(f);
console.log(len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment