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:-
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
Post a Comment