📖 Scenario: You are building a simple stack to manage books in a small library. The stack will hold the book IDs as integers. You will use an array to store the stack elements.
🎯 Goal: Create a stack using an array with basic operations: push, pop, and display the stack content.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top initialized to -1
Write a method push to add an element to the stack
Write a method pop to remove the top element from the stack
Write a method displayStack to print the stack elements from top to bottom
💡 Why This Matters
🌍 Real World
Stacks are used in undo features in text editors, browser history, and expression evaluation.
💼 Career
Understanding stack implementation helps in software development roles that require managing data in last-in-first-out order.
Progress0 / 4 steps
1
Create the stack array and top variable
Create an integer array called stack with size 5 and an integer variable called top initialized to -1.
DSA Java
Hint
Use int[] stack = new int[5]; to create the array and int top = -1; to track the top index.
2
Write the push method
Add a static method called push that takes an integer parameter value. Inside the method, check if top is less than 4. If yes, increment top by 1 and assign value to stack[top]. If the stack is full, print "Stack Overflow".
DSA Java
Hint
Check if top is less than 4 before adding. Increase top and assign the value to stack[top].
3
Write the pop method
Add a static method called pop that returns an integer. Inside the method, check if top is greater than or equal to 0. If yes, store stack[top] in a variable, decrement top by 1, and return the stored value. If the stack is empty, print "Stack Underflow" and return -1.
DSA Java
Hint
Check if top is at least 0 before popping. Save the top value, decrease top, and return the saved value.
4
Write the displayStack method and test the stack
Add a static method called displayStack that prints the stack elements from top to 0 separated by spaces. Then, in the main method, push the values 10, 20, and 30 onto the stack, call displayStack, pop one element, and call displayStack again.
DSA Java
Hint
Use a for loop from top down to 0 to print elements. In main, push 10, 20, 30, display, pop once, then display again.
Practice
(1/5)
1. What is the main purpose of using an array in stack implementation?
easy
A. To allow random access to elements anywhere in the stack
B. To store elements in a fixed-size linear structure
C. To automatically resize the stack when full
D. To store elements in a linked manner with pointers
Solution
Step 1: Understand stack storage using array
An array provides a fixed-size linear block of memory to store stack elements sequentially.
Step 2: Compare with other storage methods
Unlike linked lists, arrays do not use pointers and have fixed size, so they store elements linearly.
Final Answer:
To store elements in a fixed-size linear structure -> Option B
Quick Check:
Array = fixed-size linear storage [OK]
Hint: Stack array stores elements linearly with fixed size [OK]
Common Mistakes:
Thinking array allows automatic resizing
Confusing array with linked list storage
Assuming random access is main stack feature
2. Which of the following is the correct way to declare a stack array of size 10 in Java?
easy
A. int[] stack = new int[10];
B. int stack = new int[10];
C. int stack[] = int[10];
D. int stack = int[10];
Solution
Step 1: Recall Java array declaration syntax
In Java, arrays are declared with type[], variable = new type[size];
Step 2: Match correct syntax
int[] stack = new int[10]; matches this syntax: int[] stack = new int[10]; others have syntax errors.
Final Answer:
int[] stack = new int[10]; -> Option A
Quick Check:
Java array declaration = int[] name = new int[size]; [OK]
Hint: Use 'type[] name = new type[size];' for arrays in Java [OK]
Common Mistakes:
Missing 'new' keyword
Wrong placement of brackets
Assigning array without 'new'
3. What will be the output after the following operations on a stack of size 3?
Stack s = new Stack(3);
s.push(10);
s.push(20);
s.pop();
s.push(30);
s.push(40);
Assuming push prints "Pushed: value" and pop prints "Popped: value".
medium
A. "Pushed: 10\nPushed: 20\nPopped: 20\nStack Overflow\nPushed: 40"
B. "Pushed: 10\nPushed: 20\nPopped: 20\nPushed: 30"
C. "Pushed: 10\nPushed: 20\nPopped: 20\nPushed: 30\nStack Overflow"
D. "Pushed: 10\nPushed: 20\nPopped: 20\nPushed: 30\nPushed: 40"
Solution
Step 1: Trace push and pop operations on stack size 3
A. Change condition to if (top >= size) to correctly detect overflow
B. Change condition to if (top == size - 1) to avoid overflow
C. Initialize top to 1 instead of 0 to fix indexing
D. Replace top++ with ++top to fix increment order
Solution
Step 1: Analyze overflow condition
Since array indices go from 0 to size-1, top == size means stack is full; condition should catch top >= size to prevent overflow.
Step 2: Check other options
Changing to size - 1 is incorrect because top points to next free slot; initializing top to 1 or changing increment order is unrelated.
Final Answer:
Change condition to if (top >= size) to correctly detect overflow -> Option A
Quick Check:
Overflow check = top >= size [OK]
Hint: Overflow when top index reaches or exceeds size [OK]
Common Mistakes:
Using top == size - 1 instead of top >= size
Incorrect top initialization
Misunderstanding increment order
5. Given a stack implemented using an array with size 5, which sequence of operations will result in the stack containing elements [5, 10, 15] from bottom to top?