<script>
class Block{
constructor(value, localMax){
this
.value = value
this
.localMax = localMax
}
}
class Stack{
constructor(size){
this
.stack =
new
Array(size).fill(
null
)
this
.size = size
this
.top = -1
}
push(value){
if
(
this
.top ==
this
.size - 1)
document.write(
"Stack is full"
,
"</br>"
)
else
{
this
.top += 1
if
(
this
.top == 0)
this
.stack[
this
.top] =
new
Block(value, value)
else
{
if
(
this
.stack[
this
.top - 1].localMax > value){
this
.stack[
this
.top] =
new
Block(
value,
this
.stack[
this
.top - 1].localMax)
}
else
{
this
.stack[
this
.top] =
new
Block(value, value)
}
}
document.write(value,
"inserted in the stack"
,
"</br>"
)
}
}
pop(){
if
(
this
.top == -1)
document.write(
"Stack is empty"
,
"</br>"
)
else
{
this
.top -= 1
document.write(
"Element popped"
,
"</br>"
)
}
}
max(){
if
(
this
.top == -1)
document.write(
"Stack is empty"
,
"</br>"
)
else
{
document.write(
"Maximum value in the stack:"
,
this
.stack[
this
.top].localMax,
"</br>"
)
}
}
}
let stack =
new
Stack(5)
stack.push(2)
stack.max()
stack.push(6)
stack.max()
stack.pop()
stack.max()
</script>