Linked List Representation Of Stack Data Structure In Javascript | letsbug

     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. 

Comments

Categories

Big Data Analytics Binary Search Binary Search Tree Binary To Decimal binary tree Breadth First Search Bubble sort C Programming c++ Chemical Reaction and equation class 10 class 10th Class 9 Climate Complex Numbers computer network counting sort CSS Cyber Offenses Cyber Security Cyberstalking Data Science Data Structures Decimal To Binary Development diamond pattern Digital Marketing dust of snow Economics Economics Lesson 4 Email Validation English fire and ice Food Security in India Footprints Without feet Forest And Wildlife Resources game Geography Geography lesson 6 glassmorphism Glossary Graph HackerRank Solution hindi HTML image previewer India-Size And Location Insertion Sort Internet Network Status Interview Questions Introduction to cyber crime and cyber security IT javascript tricks json to CSV converter lesson 2 lesson 1 lesson 2 Lesson 3 Lesson 6 lesson 7 Life lines of National Economy life processes Linear Search Linked List lowest common ancestor Machine Learning MCQs median in array Merge sort min and max of two numbers Moment Money and Credit My Childhood Natural Vegetation and Wildlife NCERT Network connectivity devices Network Models Network Security No Men Are foreign Node.js operator overloading P5.js PHP Physical features of India Population Prime Numbers python Quick sort R language Rain on the roof Regular Expression Resources and development reversing array saakhi science Searching Algorithm Selection sort Social Media Marketing social science Software Engineering Software Testing Sorting Algorithm Stacks staircase pattern System Concepts Text Recognition The last Leaf time converter Time Passed From A Date Todo List App Tree Trending Technologies Understanding Economic Development username and password video player Visualization water resources Wired And Wireless LAN साखी
Show more

Popular Posts

Big Data MCQs(multiple choice questions) with answers - letsbug

Digital Marketing MCQ(Multiple Choice Questions) with Answers | part 1 | letsbug

Software Engineering MCQs questions with answers - letsbug