STACKS
APPLICATION
⚫⚫⚫
Presented by: Group 9
Bonbon, Jhon Rohclem L.
Jericho Gapuzan
Dave Vasay
Jessa Resaba
STACKS APPLICATION
⚫ ⚫ ⚫
- Stacks are a fundamental data structure in
computer science that follow the Last In, First
Out (LIFO) principle. This means that the last
element added to the stack is the first one to
be removed. Stacks are widely used in
various applications across computer science
and programming.
2
STACKS APPLICATION
⚫ ⚫ ⚫
“common applications of stacks”
• UNDO MECHANISM
- Many applications, such as text editors or graphic design software, use
stacks to implement an "undo" mechanism. Each action performed is pushed
onto a stack, allowing the user to reverse the actions in the reverse order.
PRESENTATION TITLE 3
STACKS APPLICATION
⚫ ⚫ ⚫
“common applications of stacks”
• Algorithmic Problems
- Stacks are used to solve various algorithmic problems, such as checking for
balanced parentheses, finding the next greater element in an array, and
solving the Tower of Hanoi problem.
PRESENTATION TITLE 4
STACKS APPLICATION
⚫ ⚫ ⚫
“common applications of stacks”
• Parsing and Syntax Analysis
- Stacks are used in the parsing of expressions and syntax analysis of
programming languages. They help in tracking the structure of expressions
and ensuring proper syntax
PRESENTATION TITLE 5
STACKS
APPLICATION
⚫ ⚫ ⚫
In Java, as in many other
programming languages, stacks find
numerous applications due to their
versatility and usefulness in solving
various problems. Here's how stacks are
connected to Java:
⚫ ⚫ ⚫
“Here's how stacks are connected to Java”
import java.util.Stack;
Java provides a “Stack” class
public class StackExample { in the “java.util” package, which is a
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>(); part of the Java Collections Framework.
This class extends “Vector” with
// Pushing elements onto the stack
stack.push(1); methods that allow a vector to be
stack.push(2); treated as a stack. Developers can use
stack.push(3);
this class to implement stack-based
// Popping elements from the stack algorithms easily.
System.out.println(stack.pop()); // Outputs: 3
}
}
STACKS APPLICATION 7
import java.util.Stack;
public class StackExample {
⚫ ⚫ ⚫
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
// Pushing elements onto the stack
stack.push(1);
“Here's how stacks are connected to Java”
stack.push(2);
stack.push(3);
// Popping elements from the stack
System.out.println(stack.pop()); // Outputs: 3
}
}
Function Call Stack
•Java uses a call stack to manage
method calls and returns. Each time a
method is called, a new frame is pushed
onto the stack, and when the method
returns, the frame is popped off. This is
crucial for the execution of Java
program
STACKS APPLICATION 8
⚫ ⚫ ⚫
“Here's how stacks are connected to Java”
public class RecursionExample { Recursion
public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5: " + result); •Java supports recursion, where a
}
method can call itself. The call stack is
public static int factorial(int n) { employed to keep track of the different
if (n == 0 || n == 1) {
return 1; instances of the recursive method. Each
} else { recursive call adds a new frame to the
return n * factorial(n - 1);
} stack.
}
}
STACKS APPLICATION 9
⚫ ⚫ ⚫
“Here's how stacks are connected to Java”
public class RecursionExample { Undo Mechanism in GUIs
public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5: " + result); •Graphical User Interfaces (GUIs) in
}
Java often use stacks to implement
public static int factorial(int n) { undo mechanisms. Each user action
if (n == 0 || n == 1) {
return 1; that can be undone is recorded on a
} else { stack, and the stack is used to revert
return n * factorial(n - 1);
} actions in reverse order.
}
}
STACKS APPLICATION 10
⚫ ⚫ ⚫
“Here's how stacks are connected to Java”
public class RecursionExample { Java Virtual Machine (JVM)
public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5: " + result); •The Java Virtual Machine manages
}
memory using a combination of the
public static int factorial(int n) { heap and the call stack. Local variables,
if (n == 0 || n == 1) {
return 1; function parameters, and method calls
} else { are all managed using the stack,
return n * factorial(n - 1);
} providing memory efficiency and
} controlled access.
}
STACKS APPLICATION 11
⚫⚫⚫
“Here's how stacks are connected to Java”
import java.util.LinkedList;
Java Collections Framework:
public class CustomStackExample {
public static void main(String[] args) {
LinkedList<Integer> stack = new LinkedList<>(); While the “Stack” class is available, in
practice, developers often use other
// Pushing elements onto the stack
stack.push(1); classes from the Java Collections
stack.push(2); Framework like “LinkedList” or
stack.push(3);
“ArrayDeque” to implement stacks due
// Popping elements from the stack to some of their advantages.
System.out.println(stack.pop()); // Outputs: 3
}
}
STACKS APPLICATION 12
“Stacks Application Using Java Sample Code”
import java.util.LinkedList; public class RecursionExample { import java.util.Stack;
public static void main(String[] args) {
public class CustomStackExample { int result = factorial(5); public class StackExample {
public static void main(String[] args) { Class Simple;
System.out.println("Factorial of 5: " + result); public static void main(String[] args) {
LinkedList<Integer> stack = new LinkedList<>(); } Stack<Integer> stack = new Stack<>();
public class CustomStackEndOfDiscussion {
public static void main(String[] args) { // Pushing elements onto the stack
// Pushing elements onto the stack
public static int factorial(int n) {
stack.push(1); System.out.println(“Hello
stack.push(1);
stack.push(2);
if (n == 0 || n == 1) {Classmates” (+-
Thanks>For>Listening)); // Outputs: 3 stack.push(2);
stack.push(3); return 1; stack.push(3);
} } else {
// Popping elements from the stack } return n * factorial(n - 1); // Popping elements from the stack
System.out.println(stack.pop()); // Outputs: 3 } System.out.println(stack.pop()); // Outputs: 3
} } }
} } }
⚫ ⚫ ⚫
These Java Sample Codes demonstrate the integration of stacks into various aspects of Java
programming, emphasizing their importance in algorithm design, data structure implementation, and
program execution
STACKS APPLICATION 13
Class Simple;
public class CustomStackEndOfDiscussion {
public static void main(String[] args) {
System.out.println(“Hello Classmates”
(+-Thanks>For>Listening)); //
Outputs: 3
}
}
STACKS APPLICATION 14
THANK
YOU
STACKS APPLICATION 15