Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Doubly linked List DSA

 class LLNode

 {

 constructor(data)

 {

 this.data=data;

 this.next=null;

 this.per=null;

 }

 }

 

class DoublyLinkedList

 {

 constructor()

 {

 this.head=null;

 this.tail=null;

 

 }

 create(data)

 {

 var newNode=new LLNode(data);

 if(this.head==null)

 {

 this.head=this.tail=newNode;

 }

 else

 {

 newNode.per=this.tail;

 this.tail.next=newNode;

 this.tail=newNode;

 }

 }

 display()

 {

 console.log("Elements are:");

 if(this.head==null)

 {

 console.log("List is empty");

 }

 else

 {

 let current=this.head;

 while (current!=null) 

 {

 console.log(current.data);

 current=current.next;

 

 }

 }

 }

 insertFront(data)

 {

 let newNode=new LLNode(data);

 newNode.next=this.head;

 this.head.per=newNode;

 this.head=newNode;

 }

 insertEnd(data)

 {

 let newNode=new LLNode(data);

 this.tail.next=newNode;

 newNode.per=this.tail;

 this.tail=newNode;

 }

 insertInBetween(data,pos)

 {

 let newNode=new LLNode(data);

 let p=this.head;

 for(let i=0;i<pos-1 && p.next!=null;i++)

 {

 p=p.next;

 var q=p.next;

 newNode.next=q;

 q.per=newNode;

 p.next=newNode;

 newNode.per=p;

 }

 }

 

 delfront()

 {

 console.log("The elements deleted is",this.head.data);

 this.head=this.head.next;

 }

 delEnd()

 {

 let q=this.tail;

 var p=q.per;

 console.log("The elements deleted is",q.data);

 p.next=null;

 }

 delInBetween(pos)

 {

 var q,r;

 let p=this.head;

 for(let i=0;i<pos && p!=null;i++)

 {

 p=p.next;

 q=p.next;

 r=p.per;

 r.next=q;

 q.per=r;

 }

 console.log("Deleted element is:",p.data);

 }

 

 }

const list=new DoublyLinkedList();

var ch;

console.log("1. Enter the Element in List");

console.log("2. Enter the Element at the Front");

console.log("3. Enter the Element at the End");

console.log("4. Enter the Element in Between");

console.log("5. Delete the First Element in List");

console.log("6. Delete the Last Element in List");

console.log("7. Delete the Element in Between in List");

console.log("8. Display");

console.log("9. Exit");

do {

 var ch = parseInt(prompt("Enter the choice"));

switch(ch)

 {

 case 1: var x=parseInt(prompt("enter the elemnets:"));

 list.create(x);

 break;

 case 2: var x=parseInt(prompt("enter the elemnets:"));

 list.insertFront(x);

 break;

 case 3: var x = parseInt(prompt("enter the elements:"));

 list.insertEnd(x);

 break;

 case 4: 

 var x=parseInt(prompt("enter the element:")); 

 var y=parseInt(prompt("enter the position:"));

 list.insertInBetween(x,y);

 break;

 case 5: list.delfront();

 break;

 case 6: list.delEnd();

 break;

 case 7: var p=parseInt(prompt("enter the position:"));

 list.delInBetween(p);

 break;

 case 8: list.display();

 break;

 case 9: console.log("program ends here");

 break;

 default:

 console.log("Invalid Operation");

 }

}while(ch!=9);


Output:- 

1. Enter the Element in List 2. Enter the Element at the Front 3. Enter the Element at the End 4. Enter the Element in Between 5. Delete the First Element in List 6. Delete the Last Element in List 7. Delete the Element in Between in List 8. Display Exit Elements are: 10 20 30 40 Elements are: 50 10 20 30 40 Elements are: 50 10 20 30 40 60 Elements are: 10 20 50 30 40 The elements deleted is 10 The elements deleted is 50 Deleted element is: 30 


Post a Comment

0 Comments

Ad Code

Responsive Advertisement