0% found this document useful (0 votes)
13 views43 pages

Dsa Lab6

Uploaded by

lujainmahesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views43 pages

Dsa Lab6

Uploaded by

lujainmahesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

SZABIST University Larkana

Data Structures and Algorithms in Java 2024


DATA STRUCTURE AND
ALGORITHM IN JAVA
LAB – 06
Kishor Kumar
• Introduction to Stack
• Monotonic Stack
• Types of Monotonic Stack:
• Operation on Stack:
search(), empty(), pop(), peek(), push(), capacity(), Lab Objectives

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

• A Stack is a linear data structure that


follows a particular order in which the
operations are performed.

• The order may be LIFO(Last In First


Out) or FILO(First In Last Out).

• LIFO implies that the element that is


inserted last, comes out first and FILO
implies that the element that is
Stack
inserted first, comes out last. Introduction
Introduction to Stack

• It behaves like a stack of plates, where


the last plate added is the first one to
be removed.

• Think of it this way:


• Pushing an element onto the stack is
like adding a new plate on top.
• Popping an element removes the top
plate from the stack.
Stack
Introduction
Introduction
Let’s do it with

Source Code
Monotonic
Stack
Implementation & Operation
Introduction to Monotonic Stack

It is a common data structure in


computer science that maintains its
elements in a specific order.

Unlike traditional stacks, Monotonic


Monotonic
Stacks ensure that elements inside the
stack are arranged in an increasing or
decreasing order based on their arrival
time.
Stack
In order to achieve the monotonic stacks, Introduction
we have to enforce the push and pop
operation depending on whether we
want a monotonic increasing stack or
monotonic decreasing stack.
Introduction to Monotonic Stack

• Let’s understand the term Monotonic


Stacks by breaking it down.

• Monotonic: It is a word for mathematics


functions.
Monotonic
• A function y = f(x) is monotonically
increasing or decreasing when it follows the
below conditions:
Stack
Introduction
Introduction to Monotonic Stack

• As x increases, y also increases always, then it’s


a monotonically increasing function.
• As x increases, y decreases always, then it’s a
monotonically decreasing function.
• See the below examples:
Monotonic
• y = 2x +5, it’s a monotonically increasing
function.
• y = -(2x), it’s a monotonically decreasing
function.
Stack
• Similarly, A stack is called a monotonic stack if Introduction
all the elements starting from the bottom of
the stack is either in increasing or in decreasing
order.
Introduction
Monotonic
Stack Types
Implementation & Operation
Introduction to Monotonic Increasing Stack

• A Monotonically Increasing Stack is a stack


where elements are placed in increasing
order from the bottom to the top.

• Each new element added to the stack is


Monotonic

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.

• Example: 1, 3, 10, 15, 17


How to achieve Monotonic Increasing Stack?

• To achieve a monotonic increasing stack,


you would typically push elements onto the
stack while ensuring that the stack
maintains a increasing order from bottom
to top. Monotonic

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

Monotonic Decreasing Stack:


• A Monotonically Decreasing Stack is a
stack where elements are placed in
decreasing order from the bottom to the


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.

• Example: 17, 14, 10, 5, 1


Introduction to Monotonic Stack

How to achieve Monotonic Decreasing


Stack?
• To achieve a monotonic decreasing stack,
you would typically push elements onto
the stack while ensuring that the stack
Monotonic

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:

• The java.util.Stack.search(Object element)


method in Java is used to search for an
element in the stack and get its distance
from the top.

• This method starts the count of the position

Search
from 1 and not from 0.

• The element that is on the top of the stack is


considered to be at position 1.

• If more than one element is present, the


index of the element closest to the top is
returned.

• The method returns its position if the


element is successfully found and returns -1
if the element is absent.
Stack empty() Method in Java

• The java.util.Stack.empty() method in


Java is used to check whether a stack is
empty or not.

• The method is of boolean type and


returns true if the stack is empty else
false. Empty
Stack POP() Method in Java

• The Java.util.Stack.pop() method in Java


is used to pop an element from the stack.
• The element is popped from the top of
the stack and is removed from the same.

• Parameters: The method does not take


any parameters.

• Return Value: This method returns the


element present at the top of the stack
and then removes it.
POP
Stack PEEK() Method in Java

• The java.util.Stack.peek() method in Java


is used to retrieve or fetch the first
element of the Stack or the element
present at the top of the Stack.

• The element retrieved does not get


deleted or removed from the Stack.

• Parameters: The method does not take


any parameters.
PEEK
• Return Value: The method returns the
element at the top of the Stack else
returns NULL if the Stack is empty.
Stack PUSH() Method in Java

• Java.util.Stack.push(E element)
method is used to push an
element into the Stack.

• The element gets pushed onto

PUSH
the top of the Stack.
• element to be pushed into the
stack.

• Return Value: The method


returns the argument passed.
• It also accepts the null value
unlike ArrayDeque.push() which
throws
java.lang.NullPointerException
on doing the same.
Stack CAPACITY() Method in Java

• The capacity() method of Stack Class is


used to get the capacity of this Stack.

• That is the number of elements present


in this stack container.

• Parameters: This function accepts a


parameter E obj which is the object to be
added at the end of the Stack.
CAPACITY
• Return Value: The method returns
integer value which is the capacity of the
Stack
Stack CONTAINS() Method in Java

• The java.util.Stack.contains() method is


used to check whether a specific element
is present in the Stack or not.

• So basically it is used to check if a Stack


contains any particular element or not.

• Parameters: This method takes a


mandatory parameter element which is
of the type of Stack.
• This is the element that needs to be
CONTAINS
tested if it is present in the Stack or not.

• Return Value: This method returns True if


the element is present in the Stack
otherwise it returns False.
Stack GET() Method in Java

• The Java.util.Stack.get() method is used


to fetch or retrieve an element at a
specific index from a Stack.

• Parameters: This method accepts a


mandatory parameter index which is of
integer data type. It specifies the position
or index of the element to be fetched
from the Stack.

• Return Value: The method returns the


element present at the position specified
by the parameter index.
Stack INDEXOF() Method in Java

• The Java.util.Stack.indexOf(Object element) method


is used to check and find the occurrence of a
particular element in the Stack.

• If the element is present then the index of the first


occurrence of the element is returned otherwise -1
is returned

• if the Stack does not contain the element.

• Parameters: This method accepts a mandatory


parameter element of the type of Stack.

• It specifies the element whose occurrence is needed


to be checked in the Stack.
INDEXOF
• Return Value: This method returns the index or
position of the first occurrence of the element in the
Stack.

• Else it returns -1 if the element is not present in the


Stack. The returned value is of integer type.
Stack CONTAINSALL() Method in Java

• The containsAll() method of Java Stack is


used to check whether two stacks contain
the same elements or not.

• It takes one stack as a parameter and


returns True if all of the elements of this
stack is present in the other stack.

• Parameters: The parameter C is a


Collection. This parameter refers to the
CONTAINSALL
stack whose elements occurrence is needed
to be checked in this stack.

• Return Value: The method returns True if


this stack contains all the elements of other
stack otherwise it returns False.
Stack CLEAR() Method in Java

• The Java.util.Stack.clear() method is used to


remove all the elements from a Stack.

• Using the clear() method only clears all the


element from the Stack and does not delete
the Stack.

• In other words, we can say that the clear()


method is used to only empty an existing
Stack.

• Parameters: The method does not take any


CLEAR
parameter

• Return Value: The function does not returns


any value.
Stack COPYINTO() Method in Java

• The java.util.Stack.copyInto() method is used to


copy all of the components from this Stack to
another Stack, having enough space to hold all
of the components of the Stack.

• It is to be noted that the index of the elements


remains unchanged.

• The elements present in the Stack are replaced


by the elements of the Stack. COPYINTO
• Parameters: The parameter Stack[] is of the
type of Stack. This is the Stack into which the
elements of the Stack are to be copied.

• Return Value: The method is of void type and


does not return any values.
Stack INSERTELEMENTAT() Method in Java

• The
Java.util.Stack.insertElementAt(element,
index) method is used to insert a particular
element at the specified index of the Stack.

• Both the element and the position is


passed as the parameters.
INSERTELEMENTAT
• If an element is inserted at a specified
index, then all the elements are pushed
upward by one and hence the capacity is
increased, creating a space for the new
element.
Stack ISEMPTY() Method in Java

• The Java.util.Stack.isEmpty() method in Java


is used to check and verify if a Stack is
empty or not.

• It returns True if the Stack is empty else it


returns False.

ISEMPTY
Stack ELEMENTAT() Method in Java

• The Java.util.Stack.elementAt(int pos)


method is used to fetch or retrieve an
element at a specific index from a Stack.

ELEMENTAT
Stack ENSURECAPACITY() Method in Java

• The ensureCapacity() method of


Java.util.Stack class increases the capacity
of this Stack instance,

• if necessary, to ensure that it can hold at


least the number of elements specified by
the minimum capacity argument.
ENSURECAPACITY
Stack FIRSTELEMENT() Method in Java

• The Java.util.Stack.firstElement() method in


Java is used to retrieve or fetch the first
element of the Stack.

• It returns the element present at the 0th


index of the Stack
FIRSTELEMENT
Stack HASHCODE() Method in Java

• The Java.util.Stack.hashCode() method in


Java is used to get the hashcode value of
this Stack.

HASHCODE
Stack INDEXOF() Method in Java

• The Java.util.Stack.indexOf(Object element,


int index) method is used to the index of
the first occurrence of the specified
element in this Stack, searching forwards
from index, or returns -1 if the element is
not found.

• More formally, returns the lowest index i


such that
INDEXOF
• (i >= index && Objects.equals(o, get(i))),
• or -1 if there is no such index.
Let’s do it with

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

• Task 01: Implement two Stacks in an Array


• Task 02: Sort a stack using a temporary stack
Input : [34, 3, 31, 98, 92, 23] Output : [3, 23, 31, 34, 92, 98]
• Task 03: Valid Parentheses in an Expression
[ Input: s = “[()]{}{[()()]()}” Output: true
Explanation: All the brackets are well-formed ]
• Task 04: Convert Infix expression to Postfix expression
[ Input: A + B * C + D Output: ABC*+D+ ]
• Task 05: Convert Postfix expression to Infix expression
[ Input : abc++ Output : (a + (b + c)) ]

You might also like