Skip to main content

a simple example of linked list

working sample code->https://stackblitz.com/edit/typescript-mzgnay

operation done:-
add item is a linked list->at last
add item at starting
remove item by value
remove item by index
insert item before
insert item after
reverse linked list


code:-

//Linked list ---------------------------------------------------
//create a class which holds data and address of next class

//newNode
class newNode
{
//data which can store any type of data but right now i am not handling array and object data type only string or number
data:any;
next:newNode;
}




class LinkedList
{
//Head store first node in linked list
Head:newNode=null;
//last node in the linked list
Tail:newNode=null;
//length of linked list
len:number=0;
/*
push a node at last of linked list
*/
public push(data:any)//push data
{
//create a new node---step1
var node=new newNode();
//pass data to node---step2
node.data = data;
//if there is only one node make that node head and tail---step3
if(this.Head===null)
{
this.Head=node;
this.Tail=node;
}
else
{
//fill the tail with new node
//like---> node[data,node]->node[data,node]->node[data,node] so on...
//where second parameter holding address or link to the next node
this.Tail.next = node;
this.Tail = node;
}
//increment index by one
this.len +=1;
}
/*display the whole linked list...............................................*/
public list()
{
//move head to a new node
let node=this.Head;
//loop the node utill you found a null node
while(node !=null)
{
console.log(node.data+"----->");
node=node.next;
}
}
/*reverse the whole linked list................................................*/
public reverse()
{
//if there is only one node in the linked list==>
if(this.Head.next==null)
{
console.warn("i don't know how to reverse one item in linked list");
return;
}
//set curr_head=head.next node
let curr_head=this.Head.next;
//and rev_head to the head
let rev_head=this.Head;
//make rev_head to null
rev_head.next=null;
while(curr_head)
{
let temp=curr_head;
curr_head=curr_head.next;
temp.next=rev_head;
rev_head=temp;
}
this.Head=rev_head;
let currentNode=rev_head;
while(currentNode!=null)
{
console.log(currentNode.data);
currentNode=currentNode.next;
}
}

//delete by value not applied for array and object
public deleteByValue(value)
{
let node=this.Head;
let oldnode=null;
while(node !=null)
{
if(node.data===value)
{
let temp=node;
node=null;
oldnode.next=temp.next;
node=oldnode.next.next;
this.len -=1;
}
oldnode=node;
node=node.next;
}
}
//delete by index
public deleteByIndex(index)
{
let node=this.Head;
let oldnode=null;
let fetchedIndex=0;
while(node !=null)
{
if(index===fetchedIndex)
{
let temp=node;
node=null;
oldnode.next=temp.next;
node=oldnode.next.next;
this.len -=1;
}
oldnode=node;
node=node.next;
fetchedIndex+=1;
}
}
//append at start
public append(data)
{
var node=new newNode();
node.data = data;
if(this.Head===null)
{
this.Head=node;
this.Tail=node;
}else
{
let temp=this.Head;
this.Head=node;
this.Head.next = temp;
}
this.len +=1;
}
//insert at a given position Before
public insertByPositionBefore(data:any,index:number)
{
let startIndex=0;
let node=this.Head;
let previous=null;
while(node !=null)
{
if(startIndex===index)
{
let temp=node;
let newDataWithNode=new newNode();
newDataWithNode.data=data
newDataWithNode.next=temp;
if(previous!=null)
{
previous.next=newDataWithNode;
}
this.len +=1;
}
previous=node;
node=node.next;
startIndex+=1;
}
}
//insert by value Before no object and array
public insertByValueBefore(data:any,searchData:any)
{
let node=this.Head;
let previous=null;
while(node !=null)
{
if(node.data===searchData)
{
let temp=node;
let newDataWithNode=new newNode();
newDataWithNode.data=data
newDataWithNode.next=temp;
if(previous!=null)
{
previous.next=newDataWithNode;
}
this.len +=1;
}
previous=node;
node=node.next;
}
}
//insert at a given position after
public insertByPositionAfter(data:any,index:number)
{
let startIndex=0;
let node=this.Head;
while(node !=null)
{
if(startIndex===index)
{
let temp=node;
let newDataWithNode=new newNode();
newDataWithNode.data=data
newDataWithNode.next=node.next
node.next=newDataWithNode;
this.len +=1;
}
startIndex+=1;
node=node.next;
}
}
//insert by value after no object and array
public insertByValueAfter(data:any,searchData:any)
{
let node=this.Head;
while(node !=null)
{
if(node.data===searchData)
{
let temp=node;
let newDataWithNode=new newNode();
newDataWithNode.data=data
newDataWithNode.next=node.next
node.next=newDataWithNode;
this.len +=1;
}
node=node.next;
}
}
//push a item at last------------------>>>>>
public length()
{
if(length<0)
{
console.warn("linked list is empty.....");
}
return this.len;
}

}

var mylist=new LinkedList();
mylist.push("iphone")
mylist.push("ipad")

mylist.insertByValueAfter("computer","iphone");
mylist.insertByValueAfter("computer next","computer");
mylist.insertByValueBefore("computer next next next","computer next");
mylist.insertByPositionAfter("this is a new computer1",2);
mylist.insertByPositionBefore("this is a new computer2",2);
mylist.append("add me at first");
mylist.push("add me at last");
console.log("1**************---------->display whole list")
mylist.list();
console.log("1**************---------->length of list")
console.log(mylist.length());

console.log("1**************---------->reverse the list")
mylist.reverse();

console.log("1**************---------->length of list")
console.log(mylist.length());
mylist.deleteByValue("computer");
mylist.deleteByValue("computer next next next");
mylist.deleteByIndex(1);
console.log("2**************---------->display whole list scond time")
mylist.list();
console.log("**************---------->length of list")
console.log("2changed length",mylist.length());

Comments

Popular posts from this blog

Creating a word Scramble game where you can drag and drop the word and create a complete word. (drag and drop word Scramble game).

Creating a word game using  phaser game engine which is a javascript based gaming engine  where u can drag and drop world like Word Scramble Game and complete a given word. your index.html file look like below < html > < head > < script src = "src/phaser.min.js" ></ script > < script src = "src/wordGame.js" ></ script > < script src = "src/main.js" ></ script > </ head > < body > </ body > </ html > 1.    <script src="src/phaser.min.js"></script> required to run the  phaser game engine. 2. rest of the two files deals with game logics      <script src="src/wordGame.js"></script>       <script src="src/main.js"></script> you can download whole project from https://github.com/manishchauhan/wordgame/tree/master just copy and paste the project in your local server and run ...

A simple binary search tree with generic approach tree can store any type of data. (DFS and BFS)

 1. a binary search tree in c++ using a generic approach with both BFS and DFS approach    working tree    https://www.onlinegdb.com/edit/HkuQLdA-w code=> some properties of binary search tree-> 1. binary search tree is not always a balanced tree.  2. Inorder, traversing gives you sorted data.  3.  best is o(log n) and worst is O(n).  4. left node data is always less than root data and right node data always be greater than root data.  5. red-black tree STL map is a balanced binary search tree. *******************************************************************************/ #include <stdio.h> #include<iostream> #include<queue> using namespace std; template < typename T > class Node {   //can store any type of data private:   //genric data or store any type of data   T data;   //reference of left pointer   Node *left = nullptr;   //reference of right pointer   Node *rig...

starting with three.js and react with a very simple example

 1. three.js is undoubtedly the best library to create interactive content for the web in 3d. link https://threejs.org/ a working sample of three with react can be found below https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js why choose threejs 1. lightweight  2.fast 3.big community  https://discourse.threejs.org/ 4. even you can use unity or unreal to publish html5 content but i don't think that would be acceptable in many cases. 5. open-source project. i created a running sample with React  https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js