Dsa Lab6
Dsa Lab6
Stack
contains(), equals(), get(), indexOf(),containsAll(),
clear(), copyInto(), insertElementAt(), isEmpty(),
elementAt(), ensureCapacity(), firstElement(),
hashCode(), indexOf(Object, int)
• Implement a stack using singly linked list
Implementations & Operations
Stack
Implementation & Operation
Introduction to Stack
Source Code
Monotonic
Stack
Implementation & Operation
Introduction to Monotonic Stack
Stack
greater than or equal to the one below it. If
a new element is smaller, we remove
elements from the top of the stack until we
find one that is smaller or equal to the new
element, or until the stack is empty.
Increment
• This ensures that the stack always stays in
increasing order.
Stack
• When pushing a new element, you would
pop elements from the stack that are
smaller than the new element until the
stack maintains the desired monotonic
increasing property.
Increment
MONOTONIC INCREASING STACK
Introduction to Monotonic Stack
•
top.
Each new element added to the stack
Monotonic
Stack
must be smaller than or equal to the one
below it.
• If a new element is greater than top of
stack then we remove elements from the
top of the stack until we find one that is
greater or equal to the new element, or
until the stack is empty. Decrement
• This ensures that the stack always stays in
decreasing order.
Stack
maintains a decreasing order from bottom
to top. When pushing a new element, you
would pop elements from the stack that
are greater than the new element until
the stack maintains the desired
monotonic decreasing property.
Decrement
Push
STACK
Operations
Implementation
Stack search() Method in Java:
Search
from 1 and not from 0.
• Java.util.Stack.push(E element)
method is used to push an
element into the Stack.
PUSH
the top of the Stack.
• element to be pushed into the
stack.
• The
Java.util.Stack.insertElementAt(element,
index) method is used to insert a particular
element at the specified index of the Stack.
ISEMPTY
Stack ELEMENTAT() Method in Java
ELEMENTAT
Stack ENSURECAPACITY() Method in Java
HASHCODE
Stack INDEXOF() Method in Java
Source Code
Input : [34, 3, 31, 98, 92, 23] Output : [3, 23, 31, 34, 92, 98]
Lab Activity
Write a program in Intellij Idea using Java Programming Language