Skip to content

Instantly share code, notes, and snippets.

@swathiPaipalle
Last active September 22, 2017 19:04
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 swathiPaipalle/87fea04126d81a1574b9d3a27dcb9e3f to your computer and use it in GitHub Desktop.
Save swathiPaipalle/87fea04126d81a1574b9d3a27dcb9e3f to your computer and use it in GitHub Desktop.
Singly Linked List// source http://jsbin.com/vofenup
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Singly Linked List">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div>
<h1>Singly Linked List</h1>
<p>
Singly linked list is a data structure that holds a sequence of linked nodes.<br>
Each node has value and pointer
</p>
<pre>
----------------- -----------------
| Data | Pointer| ------->| Data | Pointer|
----------------- -----------------
</pre>
<ul>
<li>Is linked list empty</li>
<li>Add to linked list</li>
<li>Traverse linked list</li>
<li>Remove from linked list</li>
<li>Get length of linked list</li>
</ul>
</div>
<script id="jsbin-javascript">
function Node(data){
this.data = data;
this.next = null;
}
function LinkedList(){
this.head = null;
this.length = 0;
}
LinkedList.prototype.isEmpty = function(){
return this.head === null;
}
LinkedList.prototype.append = function(val){
var node = new Node(val);
if(!this.head){
this.head = node;
this.length++;
return node;
}
var current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
this.length++;
return node;
}
LinkedList.prototype.prepend = function(val){
var node = new Node(val);
node.next = this.head;
this.head = node;
this.length++;
}
LinkedList.prototype.contains = function(val){
var cur = this.head;
while(cur !== null){
if(cur.data === val){
return true;
}
cur = cur.next;
}
return false;
}
LinkedList.prototype.remove = function(val){
if(!this.contains(val)){
return;
}
if(this.head.data === val){
this.head = this.head.next;
this.length--;
return;
}
var cur = this.head;
var prev = null;
while(cur.data !== val){
prev = cur;
cur = cur.next
}
prev.next = cur.next;
this.length--;
}
LinkedList.prototype.print = function(){
var output = '[';
var cur = this.head
while(cur !== null){
output += cur.data;
if(cur.next != null){
output+=', ';
}
cur = cur.next;
}
output += ']';
return output;
}
var list = new LinkedList();
list.append(4);
list.append(5);
list.append(6);
list.append(7);
list.prepend(3);
list.prepend(2);
list.prepend(1);
list.remove(1);
console.log(list.isEmpty());
console.log(list.print());
console.log(list.contains(8));
console.log(list.length);
</script>
<script id="jsbin-source-javascript" type="text/javascript">function Node(data){
this.data = data;
this.next = null;
}
function LinkedList(){
this.head = null;
this.length = 0;
}
LinkedList.prototype.isEmpty = function(){
return this.head === null;
}
LinkedList.prototype.append = function(val){
var node = new Node(val);
if(!this.head){
this.head = node;
this.length++;
return node;
}
var current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
this.length++;
return node;
}
LinkedList.prototype.prepend = function(val){
var node = new Node(val);
node.next = this.head;
this.head = node;
this.length++;
}
LinkedList.prototype.contains = function(val){
var cur = this.head;
while(cur !== null){
if(cur.data === val){
return true;
}
cur = cur.next;
}
return false;
}
LinkedList.prototype.remove = function(val){
if(!this.contains(val)){
return;
}
if(this.head.data === val){
this.head = this.head.next;
this.length--;
return;
}
var cur = this.head;
var prev = null;
while(cur.data !== val){
prev = cur;
cur = cur.next
}
prev.next = cur.next;
this.length--;
}
LinkedList.prototype.print = function(){
var output = '[';
var cur = this.head
while(cur !== null){
output += cur.data;
if(cur.next != null){
output+=', ';
}
cur = cur.next;
}
output += ']';
return output;
}
var list = new LinkedList();
list.append(4);
list.append(5);
list.append(6);
list.append(7);
list.prepend(3);
list.prepend(2);
list.prepend(1);
list.remove(1);
console.log(list.isEmpty());
console.log(list.print());
console.log(list.contains(8));
console.log(list.length);
</script></body>
</html>
function Node(data){
this.data = data;
this.next = null;
}
function LinkedList(){
this.head = null;
this.length = 0;
}
LinkedList.prototype.isEmpty = function(){
return this.head === null;
}
LinkedList.prototype.append = function(val){
var node = new Node(val);
if(!this.head){
this.head = node;
this.length++;
return node;
}
var current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
this.length++;
return node;
}
LinkedList.prototype.prepend = function(val){
var node = new Node(val);
node.next = this.head;
this.head = node;
this.length++;
}
LinkedList.prototype.contains = function(val){
var cur = this.head;
while(cur !== null){
if(cur.data === val){
return true;
}
cur = cur.next;
}
return false;
}
LinkedList.prototype.remove = function(val){
if(!this.contains(val)){
return;
}
if(this.head.data === val){
this.head = this.head.next;
this.length--;
return;
}
var cur = this.head;
var prev = null;
while(cur.data !== val){
prev = cur;
cur = cur.next
}
prev.next = cur.next;
this.length--;
}
LinkedList.prototype.print = function(){
var output = '[';
var cur = this.head
while(cur !== null){
output += cur.data;
if(cur.next != null){
output+=', ';
}
cur = cur.next;
}
output += ']';
return output;
}
var list = new LinkedList();
list.append(4);
list.append(5);
list.append(6);
list.append(7);
list.prepend(3);
list.prepend(2);
list.prepend(1);
list.remove(1);
console.log(list.isEmpty());
console.log(list.print());
console.log(list.contains(8));
console.log(list.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment