My Experience Implementing Stacks && Queues

class Node {
constructor(value){
this.value = value
this.next = null
this.length = 1
}
}
class Stack {
constructor(value){
const newNode = new Node(value)
this.top = newNode
this.length = 1
}
}
push(value){
const newNode = new Node(value)
if(this.length === 0){
this.top = newNode
} else {
this.top.next = newNode
this.top = newNode
}
this.length++
return this
}
pop(){
if(this.length === 0) return undefined
let temp = this.top
this.top = this.next
temp.next = null
this.length--
return temp
}
let myStack = new Stack(5)myStack.push(7)
myStack.pop()
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Queue {
constructor(value){
const newNode = new Node(value)
this.first = newNode
this.last = newNode
this.length = 1
}
}
enqueue(value){const newNode = new Node(value)if(this.length === 0){
this.first = newNode
this.last = newNode
} else {
this.last.next = newNode
this.last = newNode
}
this.length++
return this
}
dequeue(){

if(this.length === 0) return undefined
let temp = this.firstif(this.length === 1){
this.first = null
this.last = null
} else {
this.first = this.first.next
temp.next = null
}
this.length--
return temp
}
let myQueue = new Queue(11)myQueue.enqueue(3)myQueue.dequeue()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store