You are on page 1of 30

FYMCA DS Practical No: 02 Date: 11/02/2022

Div: B Abdullah Shethwala

1. Program to create and display a singly linked list.

<!DOCTYPE html>
<html>

<head>
    <script>

        class Node {
            constructor(element) {
                this.element = element;
                this.next = null
            }
        }
        class LinkedList {
            constructor() {
                this.head = null;
                this.size = 0;
            }

            add(element) {
                var node = new Node(element);
                var current;
                if (this.head == null)
                    this.head = node;
                else {
                    current = this.head;

                    while (current.next) {
                        current = current.next;
                    }
                    current.next = node;
                }
                this.size++;
            }

            insertAt(element, index) {
                if (index < 0 || index > this.size)
                    return console.log("Please enter a valid index. ");
                else {
                    var node = new Node(element);
                    var curr, prev;
                    curr = this.head;

                    if (index == 0) {
                        node.next = this.head;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                        this.head = node;
                    } else
                    {
                        curr = this.head;
                        var it = 0;

                        while (it < index) {


                            it++;
                            prev = curr;
                            curr = curr.next;
                        }

                        node.next = curr;
                        prev.next = node;
                    }
                    this.size++;
                }
            }

            removeFrom(index) {
                if (index < 0 || index >= this.size)
                    return console.log("Please Enter a valid index. ");
                else {
                    var curr, prev, it = 0;
                    curr = this.head;
                    prev = curr;

                    if (index === 0) {
                        this.head = curr.next;
                    } else {

                        while (it < index) {


                            it++;
                            prev = curr;
                            curr = curr.next;
                        }

                        prev.next = curr.next;
                    }
                    this.size--;

                    return curr.element;
                }
            }

            removeElement(element) {
                var current = this.head;
                var prev = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                while (current != null) {

                    if (current.element === element) {


                        if (prev == null) {
                            this.head = current.next;
                        } else {
                            prev.next = current.next;
                        }
                        this.size--;
                        return current.element;
                    }
                    prev = current;
                    current = current.next;
                }
                return -1;
            }

            indexOf(element) {
                var count = 0;
                var current = this.head;

                while (current != null) {

                    if (current.element === element)


                        return count;
                    count++;
                    current = current.next;
                }

                return -1;
            }
            isEmpty() {
                return this.size == 0;
            }

            size_of_list() {
                console.log(this.size);
            }

            printList() {
                var curr = this.head;
                var str = "";
                while (curr) {
                    str += curr.element + " ";
                    curr = curr.next;
                }
                console.log(str);
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            }

        }

        var ll = new LinkedList();


        console.log(ll.isEmpty());

        ll.add(10);
        ll.printList();

        console.log(ll.size_of_list());

        ll.add(2);
        ll.add(4);
        ll.add(6);
        ll.add(8);
        ll.add(20)
        ll.printList();

        console.log("Remove element: " + ll.removeElement(6));


        ll.printList();

        console.log("Index of 4 is: " + ll.indexOf(4));


        console.log("Inserting an element using index position: ")

        ll.insertAt(15, 2);
        ll.printList();

        console.log("Is the List Empty? " + ll.isEmpty());


        console.log("Removing an element using index position: ")
        console.log(ll.removeFrom(3));
        ll.printList();
    </script>
</head>

</html>

Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

2. Program to create a singly linked list of n nodes and display it in


reverse order.
<!DOCTYPE html>
<html>
<body>
    <script>
        class LinkNode
        {
            constructor(data,first)
            {
                this.data = data;
                this.next = first;
            }
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

        }
        class circularlinklist
        {
            constructor()
            {
                this.head = null;
            }
            insert (value)
            {
                var node = new LinkNode(value, this.head);
                if(this.head == null)
                {
                    this.head = node;
                    node.next = this.head;
                }
                else
                {
                    var temp = this.head;
                    while(temp.next != this.head)
                    {
                        temp = temp.next;
                    }
                    temp.next = node;
                }
            }
            display()
            {
                if (this.head == null)
                {
                    console.log("Empty linked list ");
                }
                else{
                    console.log("Linked List elements: ");
                    var temp  = this.head;
                    while(temp != null)
                    {
                        console.log(" "+temp.data);
                        temp= temp.next;
                        if(temp == this.head)
                        {
                            return;
                        }
                    }
                }
            }
            reverse()
            {
                if (this.head == null)
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                {
                    console.log("Empty linked list ");
                    return;
                }
                var temp= this.head;
                var back = null;
                var curr = null;
                while (temp != null && temp.next != this.head)
                {
                    curr = temp;
                    temp = temp.next;
                    curr.next = back;
                    back = curr;
                }
                this.head.next = temp;
                if(back != null)
                {
                    temp.next = back;
                }
                this.head = temp;
            }
        }
        var cll = new circularlinklist();
        cll.insert(15);
        cll.insert(25);
        cll.insert(35);
        cll.insert(45);
        cll.insert(55);
        console.log("Original Linked List: ");
        cll.display();

        console.log("\n After Reversing Linked List: ");


        cll.reverse();
        cll.display();
    </script>
</body>
</html>

Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

3. Program to delete a new node from the index of the singly linked list.
<!DOCTYPE html>
<html>

<head>
    <script>

        class Node {
            constructor (element) {
                this.element = element;
                this.next = null
            }
        }
        class DelSinglyLL {
            constructor () {
                this.head = null;
                this.size = 0;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            }

            add(element) {
                var node = new Node(element);
                var current;
                if (this.head == null)
                    this.head = node;
                else {
                    current = this.head;

                    while (current.next) {
                        current = current.next;
                    }
                    current.next = node;
                }
                this.size++;
            }

            removeFrom(index) {
                if (index < 0 || index >= this.size)
                    return console.log("Please Enter a valid index. ");
                else {
                    var curr, prev, it = 0;
                    curr = this.head;
                    prev = curr;

                    if (index === 0) {
                        this.head = curr.next;
                    } else {

                        while (it < index) {


                            it++;
                            prev = curr;
                            curr = curr.next;
                        }

                        prev.next = curr.next;
                    }
                    this.size--;

                    return curr.element;
                }
            }

            removeElement(element) {
                var current = this.head;
                var prev = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                while (current != null) {

                    if (current.element === element) {


                        if (prev == null) {
                            this.head = current.next;
                        } else {
                            prev.next = current.next;
                        }
                        this.size--;
                        return current.element;
                    }
                    prev = current;
                    current = current.next;
                }
                return -1;
            }

            printList() {
                var curr = this.head;
                var str = "";
                while (curr) {
                    str += curr.element + " ";
                    curr = curr.next;
                }
                console.log(str);
            }

        }

        var ll = new DelSinglyLL();


        ll.add(2);
        ll.add(4);
        ll.add(6);
        ll.add(8);
        ll.add(10);
        ll.add(20);
        ll.printList();

        console.log("Remove element: " + ll.removeElement(10));


        ll.printList();

        console.log("Removing an element at index position 3: ")


        ll.removeFrom(3);
        ll.printList();

    </script>
</head>
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

</html>

Output:

4. Program to Create and Display a Circular Linked List.


<!DOCTYPE html>
<html>

<body>
    <script>

        function circularLinkedList() {
            let Node = function (element) {
                this.element = element;
                this.next = null;
            }

            let length = 0;
            let head = null;

            this.getElementAt = function (index) {


                if (index >= 0 && index <= length) {
                    let node = head;
                    for (let i = 0; i < index && node != null; i++) {
                        node = node.next;
                    }
                    return node;
                }
                return undefined;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            }

            this.append = function (element) {


                const node = new Node(element);
                let current;

                if (head === null) {


                    head = node;
                } else {
                    current = this.getElementAt(length - 1);
                    current.next = node;
                }

                node.next = head;
                length++;
            }

            this.insert = function (element, index) {


                if (index >= 0 && index <= length) {
                    const node = new Node(element);
                    let current = head;

                    if (index === 0) {
                        if (head === null) {
                            head = node;
                            node.next = head;
                        } else {
                            node.next = current;
                            current = this.getElementAt(length);
                            head = node;
                            current.next = head;
                        }
                    } else {
                        const previous = this.getElementAt(index - 1);
                        node.next = previous.next;
                        previous.next = node;
                    }

                    length++;
                    return true;
                }
                return false;
            }

            this.removeAt = function (index) {


                if (index >= 0 && index < length) {
                    let current = head;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                    if (index === 0) {
                        if (length === 1) {
                            head = undefined;
                        } else {
                            const removed = head;
                            current = this.getElementAt(length - 1);
                            head = head.next;
                            current.next = head;
                            current = removed;
                        }
                    } else {
                        const previous = this.getElementAt(index - 1);
                        current = previous.next;
                        previous.next = current.next;
                    }

                    length--;
                    return current.element;
                }
                return undefined;
            }

            this.indexOf = function (elm) {


                let current = head,
                    index = -1;

                while (current) {
                    if (elm === current.element) {
                        return ++index;
                    }

                    index++;
                    current = current.next;
                }

                return -1;
            };

            this.isPresent = (elm) => {


                return this.indexOf(elm) !== -1;
            };

            this.getHead = function () {
                return head;
            }

            this.delete = (elm) => {


FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                return this.removeAt(this.indexOf(elm));
            };

            this.deleteHead = function () {
                this.removeAt(0);
            }

            this.toString = function () {
                let current = head,
                    string = '';
                const temp = head.element;

                while (current) {
                    if (temp === current.next.element) {
                        string += current.element + (current.next ? '\n' :
'');
                        break;
                    }

                    string += current.element + (current.next ? '\n' : '');


                    current = current.next;
                }

                return string;
            };

            this.toArray = function () {
                let arr = [],
                    current = head;
                const temp = head.element

                while (current) {
                    if (temp === current.next.element) {
                        arr.push(current.element);
                        break;
                    }

                    arr.push(current.element);
                    current = current.next;
                }

                return arr;
            };

            this.isEmpty = function () {
                return length === 0;
            };
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            this.size = function () {
                return length;
            }

        }

        let cll = new circularLinkedList();


        console.log("Is the list empty?: " + cll.isEmpty());
        cll.append(20);
        cll.append(30);
        cll.append(40);
        cll.append(50);
        cll.append(60);
        console.log(cll.toArray());

        console.log("Removing an element at index 3: "+ cll.removeAt(3));


        console.log(cll.toArray());
       
        console.log("Inserting an element at index 3: " );
        cll.insert(70,3);
        console.log(cll.toArray());

        console.log("Deleting the head element of the list: ");


        cll.deleteHead();
        console.log(cll.toArray());

        console.log("Deleting an element: " + cll.delete(40));


        console.log(cll.toArray());

    </script>
</body>

</html>

Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

5. Program to Create and Display a Doubly Linked List.


<html>
    <body>
        <script>

        class Node {
            constructor(elm, next = null, prev = null) {
                this.element = elm;
                this.next = next;
                this.prev = prev;
            }
        }

        class doublyLinkedList {
            constructor() {
                this.length = 0;
                this.tail = null;
                this.head = null;

            }

            append = function (element) {

                let node = new Node(element),


                    current = this.head,
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                    previous;

                if (!this.head) {
                    this.head = node;
                    this.tail = node;
                } else {
                    node.prev = this.tail;
                    this.tail.next = node;
                    this.tail = node;
                }
                this.length++;
            }

            insert = function (position, element) {

                if (position >= 0 && position <= this.length) {


                    let node = new Node(element),
                        current = this.head,
                        previous,
                        index = 0;
                    if (position === 0) {
                        if (!this.head) {
                            this.head = node;
                            this.tail = node;
                        } else {
                            node.next = current;
                            current.prev = node;
                            this.head = node;
                        }
                    } else if (position === this.length) {
                        current = this.tail;
                        current.next = node;
                        node.prev = current;
                        this.tail = node;
                    } else {
                        while (index++ < position) {
                            previous = current;
                            current = current.next;
                        }
                        node.next = current;
                        previous.next = node;

                        current.prev = node;
                        node.prev = previous;
                    }
                    this.length++;
                    return true;
                } else {
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                    return false;
                }
            }

            removeAt = function (position) {

                if (position > -1 && position < this.length) {


                    let current = this.head, previous, index = 0;

                    if (position === 0) {
                        this.head = current.next;

                        if (length === 1) {
                            this.tail = null;
                        } else {
                            this.head.prev = null;
                        }
                    } else if (position === this.length - 1) {
                        current = this.tail;
                        this.tail = current.prev;
                        this.tail.next = null;
                    } else {
                        while (index++ < position) {
                            previous = current;
                            current = current.next;
                        }

                        previous.next = current.next;
                        current.next.prev = previous;
                    }
                    this.length--;
                    return current.element;
                } else {
                    return null;
                }
            }

            indexOf = function (elm) {


                let current = this.head,
                    index = -1;

                while (current) {
                    if (elm === current.element) {
                        return ++index;
                    }
                    index++;
                    current = current.next;
                }
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                return -1;
            };

            isPresent = (elm) => {


                return this.indexOF(elm) !== -1;
            };

            delete = (elm) => {


                return this.removeAt(this.indexOf(elm));
            };

            deleteHead = function () {
                this.removeAt(0);
            }

            deleteTail = function () {
                this.removeAt(this.length - 1);
            }

            toString = function () {
                let current = this.head,
                    string = '';

                while (current) {
                    string += current.element + (current.next ? '\n' : '');
                    current = current.next;
                }
                return string;
            };

            toArray = function () {
                let arr = [],
                    current = this.head;

                while (current) {
                    arr.push(current.element);
                    current = current.next;
                }
                return arr;
            };

            isEmpty = function () {
                return this.length === 0;
            };

            size = function () {
                return this.length;
            }
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            getHead = function () {
                return this.head;
            }
            getTail = function () {
                return this.tail;
            }
        }
   
        let dll = new doublyLinkedList();
        dll.append('Harry');
        dll.append('Ron');
        dll.append('Hermoine');
        dll.insert(1, 'Snape');

        document.writeln(dll.toArray());

        </script>
    </body>
</html>

Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

6. Program to Delete a New Node from Doubly Linked List.


<html>
    <head>
        <script>
           
            var head;
           
                class Node {

                    constructor(val) {
                        this.data = val;
                        this.prev = null;
                        this.next = null;
                    }
                }
           
                function push(new_data) {

                    new_Node = new Node(new_data);


                    new_Node.next = head;
                    new_Node.prev = null;
           
                    if (head != null)
                        head.prev = new_Node;
           
                    head = new_Node;
                }
           
                function printlist( node) {
                    last = null;
           
                    while (node != null) {
                        document.write(node.data + " ");
                        last = node;
                        node = node.next;
                    }
           
                    document.write("<br/>");
                }

                function deleteNode( del) {


           
                    if (head == null || del == null) {
                        return;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                    }
           
                    if (head == del) {
                        head = del.next;
                    }
           

                    if (del.next != null) {
                        del.next.prev = del.prev;
                    }
           
                    if (del.prev != null) {
                        del.prev.next = del.next;
                    }
           
                    return;
                }
           
                    push(5);
                    push(9);            
                    push(13);
                    push(21);
                    push(29)
           
                    document.write("Created Doubly Linked List is: ");
                    printlist(head);
           
                    document.write("Deleting head element: ");
                    deleteNode(head);
                    printlist(head);

                    document.write("Deleting element next to head element: ");


                    deleteNode(head.next);
                    printlist(head);
           
                    document.write("Modified Linked list after deletion: ");
                    printlist(head);
           
           
           
            </script>
           
    </head>
</html>
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

Output:

7. Program to Create and Display a Circular doubly Linked List.


<html>

<head>
    <script>

        function circularDoublyLL() {
            let Node = function (element) {
                this.element = element;
                this.next = null;
                this.prev = null;
            }

            let length = 0;
            let head = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            let tail = null;

            this.getElementAt = function (index) {


                if (index >= 0 && index <= length) {
                    let node = head;
                    for (let i = 0; i < index && node != null; i++) {
                        node = node.next;
                    }
                    return node;
                }
                return undefined;
            }

            this.append = function (element) {


                let node = new Node(element),
                    current = head,
                    previous;

                if (!head) {
                    head = node;
                    tail = node;
                } else {
                    node.prev = tail;
                    tail.next = node;
                    tail = node;
                }

                head.prev = tail;

                tail.next = head;
                length++;
            }

            this.insert = function (position, element) {

                if (position >= 0 && position <= length) {


                    let node = new Node(element),
                        current = head,
                        previous,
                        index = 0;

                    if (position === 0) {
                        if (!head) {
                            head = node;
                            tail = node;
                        } else {
                            node.next = current;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                            current.prev = node;
                            head = node;
                        }
                    } else if (position === length) {
                        current = tail;
                        current.next = node;
                        node.prev = current;
                        tail = node;
                    } else {
                        while (index++ < position) {
                            previous = current;
                            current = current.next;
                        }

                        node.next = current;
                        previous.next = node;

                        current.prev = node;
                        node.prev = previous;
                    }

                    head.prev = tail;

                    tail.next = head;

                    length++;
                    return true;
                } else {
                    return false;
                }
            }

            this.removeAt = function (index) {


                if (index >= 0 && index < length) {
                    let current = head;

                    if (index === 0) {
                        if (length === 1) {
                            head = undefined;
                        } else {
                            const removed = head;
                            current = this.getElementAt(length - 1);
                            head = head.next;
                            current.next = head;
                            current = removed;
                        }
                    } else {
                        const previous = this.getElementAt(index - 1);
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

                        current = previous.next;
                        previous.next = current.next;
                    }

                    if (head) {
                        head.prev = tail;

                        tail.next = head;
                    }

                    length--;
                    return current.element;
                }
                return undefined;
            }

            this.indexOf = function (elm) {


                let current = head,
                    index = -1;

                while (current) {
                    if (elm === current.element) {
                        return ++index;
                    }

                    index++;
                    current = current.next;
                }

                return -1;
            };

            this.isPresent = (elm) => {


                return this.indexOf(elm) !== -1;
            };

            this.getHead = function () {
                return head;
            }

            this.getTail = function () {
                return tail;
            }

            this.delete = (elm) => {


                return this.removeAt(this.indexOf(elm));
            };
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            this.deleteHead = function () {
                this.removeAt(0);
            }

            this.toString = function () {
                let current = head,
                    string = '';
                const temp = head.element;

                while (current) {
                    if (temp === current.next.element) {
                        string += current.element + (current.next ? '\n' :
'');
                        break;
                    }

                    string += current.element + (current.next ? '\n' : '');


                    current = current.next;
                }

                return string;
            };

            this.toArray = function () {
                let arr = [],
                    current = head;
                const temp = head.element

                while (current) {
                    if (temp === current.next.element) {
                        arr.push(current.element);
                        break;
                    }

                    arr.push(current.element);
                    current = current.next;
                }

                return arr;
            };

            this.isEmpty = function () {
                return length === 0;
            };

            this.size = function () {
                return length;
            }
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

        }

        let cLL = new circularDoublyLL();


        cLL.append(10);
        cLL.append(20);
        cLL.append(30);
        cLL.append(40);
        cLL.append(50);
        console.log(cLL.toArray());

        cLL.insert(3, 25);
        console.log(cLL.toArray());
       
        cLL.removeAt(2);
        console.log(cLL.toArray());

    </script>
</head>

</html>
Output:

8. Program to Create a Doubly Linked List and Display it in Reverse Order.

<html>
    <head>
        <script>

            var head;
           
            class Node
            {
                constructor(val)
                {
                    this.data = val;
                    this.prev = null;
                    this.next = null;
                }
            }
               
            function reverse()
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            {
                var temp = null;
                var current = head;
               
                while (current != null)
                {
                    temp = current.prev;
                    current.prev = current.next;
                    current.next = temp;
                    current = current.prev;
                }
           
                if (temp != null)
                {
                    head = temp.prev;
                }
            }
           

            function push(new_data)
            {
                var new_node = new Node(new_data);                
                new_node.prev = null;
                new_node.next = head;
           
                if (head != null)
                {
                    head.prev = new_node;
                }
           
                head = new_node;
            }
           
           
            function printList(node)
            {
                while (node != null)
                {
                    document.write(node.data + " ");
                    node = node.next;
                }
            }
           
            push(21);
            push(10);
            push(33);
            push(7);
           
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala

            document.write("Original linked list: <br/>");


            printList(head);
           
            reverse();
            document.write("<br/>");
            document.write("The reversed Linked List is: <br/>");
            printList(head);
            </script>
           
    </head>
</html>

Output:

You might also like