DS with JS — Stack & Queue

Data Structures with JavaScript — Chapter Three — Stacks and Queues

Gaurav Mehla
Apr 15, 2018 · 5 min read
Image for post
Image for post

Prerequisites

Lets’ begin!!

Image for post
Image for post
Stack Definition
Image for post
Image for post
This is how stack works…

Core

Image for post
Image for post

⚜️ The list

Image for post
Image for post
Initial Stack
push( item ){
let node = new Node( item )
if( this.top ) {
node.next = this.top
this.top = node
} else {
this.top = node
}
}
Image for post
Image for post
Push Operation
pop() {
if( this.top ) {
let itemToPop = this.top
this.top = this.top.next
return itemToPop.data
} else {
log('Stack is empty!')
return false;
}
}
Image for post
Image for post
Pop Operation
peek() {
if( this.top ) {
return this.top.data
} else {
return null
}
}
Image for post
Image for post
Peek Operation
reverse() {
let current = this.top
let prev = null;
while( current ) {
let next = current.next
current.next = prev
prev = current
current = next
}
this.top = prev
}
Image for post
Image for post
Reverse Operation
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
}
Image for post
Image for post
Length, Search & IsEmpty Operation
traverse( fn ) {
let current = this.top
while( current ) {
fn( current )
current = current.next
}
}
Image for post
Image for post
Traverse Operation
Image for post
Image for post
Queue Definition
Image for post
Image for post
This is how Queue works…

Core

Image for post
Image for post

⚜️ The list

Image for post
Image for post
Initial Queue
enqueue( item ) {
let node = new Node( item )
if( !this.head ) {
this.head = node
this.tail = node
} else {
this.tail.next = node
this.tail = node
}
}
Image for post
Image for post
Enqueue Operation
dequeue() {
if( !this.head ) {
return 'No item'
} else {
let itemToPop = this.head
this.head = this.head.next
return itemToPop
}
}
Image for post
Image for post
Dequeue Operation
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
}
Image for post
Image for post
Length, Peek & IsEmpty Operation
traverse( fn ) {
let current = this.head
while( current ) {
fn( current )
current = current.next
}
}
Image for post
Image for post
Traverse Operation

Practice

Last important thing…

About this post

That’s It

Image for post
Image for post

_devblogs

Stories for Full-Stack Web developers which help them in…

Gaurav Mehla

Written by

Software engineer & Web hacker. Spent 30+% of life on playing with JS

_devblogs

_devblogs

Stories for Full-Stack Web developers which help them in pursuing their goals as a developer, mastering the modern web technologies and *hacking the web.

Gaurav Mehla

Written by

Software engineer & Web hacker. Spent 30+% of life on playing with JS

_devblogs

_devblogs

Stories for Full-Stack Web developers which help them in pursuing their goals as a developer, mastering the modern web technologies and *hacking the web.