Skip to content
Blog

Stack Using a Linked List in JavaScript

By Published A short read

     In the article we are going to implement the data structure called stack. If you don't know about them you can visit my previous blog article for the same by clicking here. In this article we are going the implement the stack but not with array but with linked list.

    Now what is linked list? You can say linked list is data structure when one single linear data structure is made by linking many smaller nodes like a chain. To learn more about it please visit here. 

    We will create a Node class which will be are individual node with next pointer and data property to store the data. Then we will link them in the stack class. And perform stack operations on the linked list.

    So let's get started 

    Linked List Representation Of Stack

    Our Stack will have following methods or operations on it.

  1. Push()
  2. Pop()
  3. Peek()
  4. isEmpty()
  5. print()
  6. reverse()
  7. length()
code:
 
//create node class
class Node {
    constructor(data) {
        this.data = data
        this.next = null
    }
}
//create stack data structure with nodes
class Stack {
    constructor() {
        this.top = null
        this.size = 0
    }
    // push the element to the stack
    push(data) {
        let node = new Node(data)
        node.next = this.top
        this.top = node
        this.size++
    }
    // pop the last element of the stack
    pop() {
        if (this.top === null) {
            return null
        }
        let node = this.top
        this.top = node.next
        this.size--
        return node.data
    }
    // peek the stack
    peek() {
        if (this.top === null) {
            return null
        }
        return this.top.data
    }
    // function to check if stack is empty or not
    isEmpty() {
        return this.top === null
    }
    // function to print the stack
    print() {
        let current = this.top
        while (current) {
            console.log(current.data)
            current = current.next
        }
    }
    // function to reverse the stack
    reverse() {
        let stack = new Stack()
        while (!this.isEmpty()) {
            stack.push(this.pop())
        }
        this.size = stack.size
        return stack
    }
    // function to check the length of the stack
    length() {
        return this.size
    }
}

let s = new Stack()
for(let i = 0; i < 10; i++){
    s.push(i) // 0 1 2 3 4 5 6 7 8 9
}
console.log("printing the stack")
s.print()
console.log(`peek: ${s.peek()}`)
console.log(`Pop: ${s.pop()}`)
console.log(`isEmpty: ${s.isEmpty()}`)
console.log(`length: ${s.length()}`)
console.log(`reverse:`)
s.reverse().print()

output:

Linked List Representation of Stack letsbug

    This was how you can implement the stack with linked list.