Redefining Stacks: Innovative Approaches to Implementing Without Array

Redefining Stacks: Innovative Approaches to Implementing Without Array

Unleashing the Power of Linked Lists: Implementing a Stack Without Arrays

Introduction

Hi, Guys. In this blog, I'll be explaining an interesting implementation of the stack without using Arrays in javascript with O(1) constant time complexity with the help of a doubly linked list.

I hope it will give a good understanding of the linked list for becoming a better software engineer.

People who know well about linked lists can feel free to skip this section below.

Linked List

A linked list is a data structure commonly used in computer programming for organizing and storing a collection of elements called nodes. Unlike arrays, linked lists do not store their elements in contiguous memory locations.

Instead, each element (node) in a linked list consists of two parts: the actual data (often called the "payload") and a reference (or link) to the next node in the sequence.

Components of Linked List

The basic components of a linked list are as follows:

  1. Node: Each node contains two parts - the data it holds and a reference (or pointer) to the next node in the list.

  2. Head: The first node in the linked list is known as the head. It serves as the starting point for traversing the list.

  3. Tail: The last node in the linked list is known as the tail. It has a reference pointing to null or a sentinel value to indicate the end of the list.

Types of Linked Lists

Linked lists come in various forms:

  1. Singly Linked List

  2. Doubly Linked List

  3. Circular Linked List

Singly Linked List

Each node has a reference to the next node in the list, forming a linear sequence. The last node's reference points to null.

Doubly Linked List

Each node has references to both the next and the previous nodes in the list, allowing for bidirectional traversal.

Circular Linked List

Similar to a singly or doubly linked list, the last node's reference in a singly circular linked list points back to the head, forming a loop. In a doubly circular linked list, both the first and last nodes are connected.

Linked List Implementation

Node

class Node {
    constructor(data) {
        this.data = data
        this.next = null                
    }
}

Insert Node

insertNode(value){
 let node = this.head;
    if(node==null){
        //Means it's just empty list
        this.head = new Node(value);
        return;
    }
    while (node.next) {
        node = node.next;
    }
}

remove Last Node

removeFromLast(){
    let node = this.head;
    let prev = null;
    while(node){
        if(node.next!==null){
            prev = node;
            node = node.next;
        }    
    }
   prev.next = null;
}

Time Complexity of this implementation

As we are looping through the size of the linked list on insert & remove node form a list. It is O(n) time complexity. This approach has both advantages & disadvantages.

Advantages

  • Dynamic size: Linked lists can easily grow or shrink in size as needed.

  • Insertions and deletions: Adding or removing elements in a linked list is often more efficient than in arrays, especially for large lists, as it involves changing references rather than shifting elements.

  • Memory allocation: Nodes can be allocated dynamically, making efficient use of memory.

Dis-Advantages

  • Non-contiguous memory: The lack of contiguous memory access can lead to higher cache misses and slower access times compared to arrays.

  • Additional memory: Each node requires additional memory for the reference, leading to increased memory overhead compared to arrays.


Stack Implementation without using Arrays

We are going to leverage the use of the doubly linked list to build a stack. Which can have an O(1) time complexity on both push & pop operations. The implementation can do better to but, but for simplicity & understanding. I am using one more pointer to keep track of the last node in a linked List.

So we know the last node that gives the constant time insert operation & removing the last node. Check out the code on replit & suggest your comments to improve it.

Conclusion

In conclusion, the linked list data structure is a fundamental concept in computer science that offers a simple and efficient way to manage data. The modern problem needs a modern solution. The linked list has many real-world use cases. Never stop learning.

Share & comment your thoughts on it for more content like this.

Did you find this article valuable?

Support Saravana sai blog by becoming a sponsor. Any amount is appreciated!