Created
March 31, 2020 04:35
-
-
Save sanishkr/f3046f11fb841660455cf9f53a9ad4a7 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gayejuz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> | |
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var Node = function Node() { | |
var data = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | |
_classCallCheck(this, Node); | |
this.next = null; | |
this.data = data; | |
}; | |
var LinkedList = (function () { | |
function LinkedList() { | |
_classCallCheck(this, LinkedList); | |
this.head; | |
} | |
_createClass(LinkedList, [{ | |
key: "add", | |
value: function add(data) { | |
var ll = new Node(data); | |
ll.next = this.head; | |
this.head = ll; | |
} | |
}, { | |
key: "find", | |
value: function find(data) { | |
var node = this.head; | |
var i = 0; | |
while (node !== null) { | |
if (node.data === data) { | |
return i; | |
} | |
node = node.next; | |
i++; | |
} | |
return null; | |
} | |
}, { | |
key: "length", | |
value: function length() { | |
var node = this.head; | |
var i = 0; | |
while (node) { | |
i++; | |
node = node.next; | |
} | |
return i; | |
} | |
}]); | |
return LinkedList; | |
})(); | |
var ll1 = new LinkedList(); | |
ll1.add(5); | |
ll1.add(7); | |
ll1.add(12); | |
ll1.add(3242); | |
ll1.add(546); | |
console.log(ll1.find(12)); | |
ll1.add(56); | |
console.log(ll1.length()); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">class Node { | |
constructor(data = null) { | |
this.next = null | |
this.data = data; | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head | |
} | |
add(data) { | |
let ll = new Node(data) | |
ll.next = this.head | |
this.head = ll | |
} | |
find(data) { | |
let node = this.head | |
let i = 0 | |
while(node !== null){ | |
if(node.data === data){ | |
return i | |
} | |
node = node.next | |
i++ | |
} | |
return null | |
} | |
length() { | |
let node = this.head | |
let i = 0 | |
while(node){ | |
i++ | |
node = node.next; | |
} | |
return i | |
} | |
} | |
let ll1 = new LinkedList() | |
ll1.add(5) | |
ll1.add(7) | |
ll1.add(12) | |
ll1.add(3242) | |
ll1.add(546) | |
console.log(ll1.find(12)) | |
ll1.add(56) | |
console.log(ll1.length()) | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var Node = function Node() { | |
var data = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | |
_classCallCheck(this, Node); | |
this.next = null; | |
this.data = data; | |
}; | |
var LinkedList = (function () { | |
function LinkedList() { | |
_classCallCheck(this, LinkedList); | |
this.head; | |
} | |
_createClass(LinkedList, [{ | |
key: "add", | |
value: function add(data) { | |
var ll = new Node(data); | |
ll.next = this.head; | |
this.head = ll; | |
} | |
}, { | |
key: "find", | |
value: function find(data) { | |
var node = this.head; | |
var i = 0; | |
while (node !== null) { | |
if (node.data === data) { | |
return i; | |
} | |
node = node.next; | |
i++; | |
} | |
return null; | |
} | |
}, { | |
key: "length", | |
value: function length() { | |
var node = this.head; | |
var i = 0; | |
while (node) { | |
i++; | |
node = node.next; | |
} | |
return i; | |
} | |
}]); | |
return LinkedList; | |
})(); | |
var ll1 = new LinkedList(); | |
ll1.add(5); | |
ll1.add(7); | |
ll1.add(12); | |
ll1.add(3242); | |
ll1.add(546); | |
console.log(ll1.find(12)); | |
ll1.add(56); | |
console.log(ll1.length()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment