πŸ“’
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript β€Šβ€”β€Šthis keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists β€” 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (β€˜ ’) and double quotes (β€œ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • Lets’ begin!!
  • Core
  • ⚜️ The list
  • πŸ”… Push
  • πŸ”… Pop
  • πŸ”… Peek
  • πŸ”… Reverse
  • πŸ”… Length, Search & IsEmpty
  • πŸ”… Traverse
  • Core
  • ⚜️ The list
  • πŸ”… Enqueue
  • πŸ”… Dequeue
  • πŸ”… Length, Peek & IsEmpty
  • πŸ”… Traverse
  • Practice

Was this helpful?

  1. Data Structures&Algorithms
  2. Data Structures in JavaScript

Stack & Queue

PreviousHash TablesNextSingle quotes (β€˜ ’) and double quotes (β€œ ”) in JavaScript

Last updated 5 years ago

Was this helpful?

❗️All the code snippets you will see below are just Pseudo code.

Why to use Linked Lists while implementing Stacks and Queues ?

I found some lines over this β€”

…because linked lists store data elements in linear sequences, they can be used to give alternative implementations of stacks and queues. One advantage to using linked lists is that we don’t have to worry about filling up something like an array β€” we can just keep allocating cells as long as we need to (unless we run out of memory). Implementing a stack using a linked list is particularly easy because all accesses to a stack are at the top. One end of a linked list, the beginning, is always directly accessible. We should therefore arrange the elements so that the top element of the stack is at the beginning of the linked list, and the bottom element of the stack is at the end of the linked list. We can represent an empty stack with null…

another reason is that you will able to practice linked-lists while implementing stacks and queues which will give you the understanding of how Stacks and Queues works while making you strong with linked-lists.

read full article over ..

Lets’ begin!!

This is how stack works…

Core

⚜️ The list

  1. Push

  2. Pop

  3. Peek

  4. Reverse

  5. Length

  6. Search

  7. IsEmpty

  8. Traverse

Lets do some code now…

Initial Stack

πŸ”… Push

push( item ){
   let node = new Node( item )
   if( this.top ) {
     node.next = this.top
     this.top = node
   } else {
     this.top = node
   }
}

Push Operation

πŸ”… Pop

pop() {
  if( this.top ) {
    let itemToPop = this.top
    this.top = this.top.next
    return itemToPop.data
  } else {
    log('Stack is empty!')
    return false;
  }
}

Pop Operation

πŸ”… Peek

peek() {
  if( this.top ) {
   return this.top.data
  } else {
   return null
  }
}

Peek Operation

πŸ”… Reverse

reverse() {
  let current = this.top
  let prev = null;
  while( current ) {
    let next = current.next
    current.next = prev
    prev = current
    current = next
  }
  this.top = prev
}

⚠️ It is same as reversing a Singly-Linked List

Reverse Operation

πŸ”… Length, Search & IsEmpty

length() {
  let current = this.top
  let counter = 0
  while( current ) {
   counter++
   current = current.next
  }
  return counter
}
search( item ) {
  let current = this.top
  while( current ) {
   if( current.data === item ) return true
   current = current.next
  }
  return false
}
isEmpty() {
  return this.length > 1
}

Length, Search & IsEmpty Operation

πŸ”… Traverse

traverse( fn ) {
  let current = this.top
  while( current ) {
   fn( current )
   current = current.next
  }
}

Traverse Operation

Queue Definition

This is how Queue works…

Core

⚜️ The list

  1. Enqueue

  2. Dequeue

  3. Length

  4. Peek

  5. IsEmpty

  6. Traverse

Initial Queue

πŸ”… Enqueue

enqueue( item ) {
   let node = new Node( item )   if( !this.head  ) {
     this.head = node
     this.tail = node
   } else {
     this.tail.next = node
     this.tail = node
   }
}

Enqueue Operation

πŸ”… Dequeue

dequeue() {
  if( !this.head ) {
   return 'No item'
  } else {
   let itemToPop = this.head
   this.head = this.head.next
   return itemToPop
  }
}

Dequeue Operation

πŸ”… Length, Peek & IsEmpty

length() {
  let current, counter
  [ current, counter ] = [ this.head, 0 ]
  while( current ) {
   counter++
   current = current.next
  }
  return counter
}peek() {
  if( this.head ) {
   return this.head.data
  } else {
   return 'Empty'
  }
}isEmpty() {
  return this.length() < 1
}

Length, Peek & IsEmpty Operation

πŸ”… Traverse

traverse( fn ) {
  let current = this.head
  while( current ) {
   fn( current )
   current = current.next
  }
}

Traverse Operation

You can implement any operation over these stacks and queues which you can implement using Linked Lists.

Practice

Hackerrank β€”

Data Structures
here