From 9b48f929ec059ce2f0f98b829ff7881c94bba4ac Mon Sep 17 00:00:00 2001 From: Selvaa Jayan <112411803+SelvaaIFETian@users.noreply.github.com> Date: Sat, 4 Oct 2025 15:37:14 +0530 Subject: [PATCH] Create Stack.java --- Stack.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Stack.java diff --git a/Stack.java b/Stack.java new file mode 100644 index 0000000..31b4301 --- /dev/null +++ b/Stack.java @@ -0,0 +1,48 @@ +import java.util.Stack; + +public class StackDemo { + public static void main(String[] args) { + // Create a Stack of integers + Stack stack = new Stack<>(); + + // 1. PUSH - Add elements to the stack + System.out.println("Pushing elements: 10, 20, 30, 40"); + stack.push(10); + stack.push(20); + stack.push(30); + stack.push(40); + System.out.println("Current Stack: " + stack); + + // 2. PEEK - View the top element (without removing) + System.out.println("\nTop element using peek(): " + stack.peek()); + System.out.println("Stack after peek (unchanged): " + stack); + + // 3. POP - Remove the top element + System.out.println("\nPopping top element: " + stack.pop()); + System.out.println("Stack after pop: " + stack); + + // 4. SEARCH - Find position of an element + int elementToSearch = 20; + int position = stack.search(elementToSearch); + if (position == -1) { + System.out.println("\nElement " + elementToSearch + " not found in stack"); + } else { + System.out.println("\nElement " + elementToSearch + " found at position (from top): " + position); + } + + // 5. SIZE - Get the number of elements + System.out.println("\nCurrent size of stack: " + stack.size()); + + // 6. ISEMPTY - Check if the stack is empty + System.out.println("\nIs stack empty? " + stack.isEmpty()); + + // 7. POP all elements (LIFO) + System.out.println("\nPopping all elements:"); + while (!stack.isEmpty()) { + System.out.println("Popped: " + stack.pop() + " | Remaining Stack: " + stack); + } + + // 8. Final check + System.out.println("\nIs stack empty now? " + stack.isEmpty()); + } +}