Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Singly linked List DSA

 class LLNode

{

    constructor(data)

    {

        this.data = data;

        this.next = null;

    }

}

 

class LinkedList

{

    constructor()

    {

        this.head = null;

    }

 

    create(data)

    {

        const newNode = new LLNode(data);

 

        if(this.head==null)

        {

            this.head = newNode;

        }

        else

        {

            let current = this.head;

 

            while(current.next!=null)

                {

                    current = current.next;

                }

            current.next = newNode;

        }

    }

 

    display()

    {

        if(this.head == null)

            console.log("List is empty");

        else

        {

            let current = this.head;

            console.log("Elements of list are:");

            while(current!=null)

                {

                   

                    console.log(current.data);

                    current = current.next;

                }

           

        }

    }

 

    insertfront(data)

    {

        let newNode = new LLNode(data);

        newNode.next = this.head;

        this.head = newNode;

    }

 

    insertend(data)

    {

        let newNode  = new LLNode(data);

        let l = this.head;

        while(l.next!=null)

            {

                l = l.next;

            }

        l.next = newNode;

        newNode.next = null;

    }

 

    insertinbetween(data,pos)

    {

        let newNode = new LLNode(data);

        let l = this.head;

        for(let i=1;i<pos && l!=null;i++)

        l = l.next;

        newNode.next = l.next;

        l.next = newNode;

    }

 

    delfront()

    {

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

        this.head = this.head.next;

    }

 

    delend()

    {

        let p = this.head;

        while(p.next!=null)

            {

                var q = p;

                p = p.next;

            }

        q.next = null;

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

    }

 

    delinbetween(pos)

    {

        let p = this.head;

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

            {

                var q = p;

                p = p.next;

            }

        q.next = p.next;

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

    }

}

 

const list = new LinkedList();

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 to inserted at position");

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 of the element:"));

                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 Ended");

            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 to inserted at position

5. Delete the First Element in List

6. Delete the Last Element in List

7. Delete the Element in Between in List

8. Display

9. Exit

Elements of list are:

10

20

30

40

Elements of list are:

50

10

20

30

40

Elements of list are:

50

10

20

30

40

60

Elements of list are:

50

10

20

30

70

40

60

Elements of list are:

60

10

20

30

40

50

The deleted element is: 20

Post a Comment

0 Comments

Ad Code

Responsive Advertisement