Data Structures — Stack and Queue-Practice Problems (Any Four)
Practice Problem 1. Undo Feature in a Text Editor (Stack)
Concept: Stack (LIFO) — Undo operations
Scenario:
You’re developing a simple text editor. Every time a user types a word, it’s
stored. When the user presses Undo, the last word typed should be removed.
Boilerplate Code:
import [Link].*;
public class TextEditorUndo {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
Scanner sc = new Scanner([Link]); while
(true) {
[Link]("Enter command (TYPE
<word>/UNDO/PRINT/EXIT): ");
String cmd = [Link]();
// TODO: Handle TYPE - push word
// TODO: Handle UNDO - pop last word // TODO: Handle
PRINT - display full sentence // TODO: Handle EXIT -
break
}
}
Example Input/Output:
Commands:
TYPE Hello
TYPE World
UNDO
TYPE Java
PRINT
Output:
Hello Java
Hint:
Use a stack to store typed words. Pop
words when “UNDO” is triggered.
Program
import [Link].*;
public class TextEditorUndo {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Enter command (TYPE
<word>/UNDO/PRINT/EXIT): ");
String cmd = [Link]();
if ([Link]("TYPE")) {
String word = [Link]();
[Link](word);
} else if ([Link]("UNDO")) {
if (![Link]()) [Link]();
else [Link]("Nothing to undo!");
} else if ([Link]("PRINT")) {
for (String word : stack) [Link](word + "
");
[Link]();
} else if ([Link]("EXIT")) {
break;
} else {
[Link]("Invalid command!");
}
}
}
}
Output
TYPE Hello
TYPE World
UNDO
TYPE Java
PRINT
Output: Hello Java
Practice Problem 2. Browser Navigation Simulation (Stack)
Concept: Stack (LIFO) — Back/Forward navigation
Scenario:
Simulate how a web browser works — when you open new pages, they’re added to a stack.
When you click Back, you go to the previous page.
Boilerplate Code:
import [Link].*;
public class BrowserNavigation {
public static void main(String[] args) {
Stack<String> backStack = new Stack<>();
Stack<String> forwardStack = new Stack<>();
Scanner sc = new Scanner([Link]);
String current = "Home";
while (true) {
[Link]("Command
(VISIT/BACK/FORWARD/PRINT/EXIT): ");
String cmd = [Link]();
// TODO: Implement VISIT - push to backStack
// TODO: Implement BACK - move from backStack to
forwardStack
// TODO: Implement FORWARD - move from forwardStack to
backStack
// TODO: PRINT current page }
Example:
Input:
VISIT [Link]
VISIT [Link]
VISIT [Link]
BACK
BACK
PRINT
Output:
Current Page: [Link]
Hint:
Use one stack for “visited pages”, another for “forward pages”.
Program
import [Link].*;
public class BrowserNavigation {
public static void main(String[] args) {
Stack<String> backStack = new Stack<>();
Stack<String> forwardStack = new Stack<>();
Scanner sc = new Scanner([Link]);
String current = "Home";
while (true) {
[Link]("Command
(VISIT/BACK/FORWARD/PRINT/EXIT): ");
String cmd = [Link]();
if ([Link]("VISIT")) {
String url = [Link]();
[Link](current);
current = url;
[Link]();
} else if ([Link]("BACK")) {
if (![Link]()) {
[Link](current);
current = [Link]();
} else {
[Link]("No pages in back history!");
}
} else if ([Link]("FORWARD")) {
if (![Link]()) {
[Link](current);
current = [Link]();
} else {
[Link]("No forward pages!");
}
} else if ([Link]("PRINT")) {
[Link]("Current Page: " + current);
} else if ([Link]("EXIT")) {
break;
} else {
[Link]("Invalid command!");
}
}
}
}
Output
VISIT [Link]
VISIT [Link]
VISIT [Link]
BACK
BACK
PRINT
Output: Current Page: [Link]
Practice Problem 3. Print Queue System for Office Printer (Queue)
Concept: Queue (FIFO) — Job Scheduling
Scenario:
In an office, multiple users send print jobs.
The printer executes them in the order they were received.
Boilerplate Code:
import [Link].*;
public class PrintQueueSystem {
public static void main(String[] args) {
Queue<String> printQueue = new LinkedList<>();
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Command (ADD <doc>/PRINT/EXIT): ");
String cmd = [Link]();
// TODO: Handle ADD - enqueue document // TODO: Handle
PRINT - dequeue and show printed document
// TODO: Handle EXIT - break loop
}
}
}
Example Input:
ADD Document1
ADD Document2
PRINT
PRINT
PRINT
Output:
Printing Document1
Printing Document2
No jobs left!
Hint: Use a queue to enqueue and dequeue job names.
Program
import [Link].*;
public class PrintQueueSystem {
public static void main(String[] args) {
Queue<String> printQueue = new LinkedList<>();
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Command (ADD <doc>/PRINT/EXIT): ");
String cmd = [Link]();
if ([Link]("ADD")) {
String doc = [Link]();
[Link](doc);
[Link]("Added " + doc + " to queue.");
} else if ([Link]("PRINT")) {
if (![Link]()) {
[Link]("Printing " +
[Link]());
} else {
[Link]("No jobs left!");
}
} else if ([Link]("EXIT")) {
break;
} else {
[Link]("Invalid command!");
}
}
}
}
Output
ADD Document1
ADD Document2
PRINT
PRINT
PRINT
Output:
Printing Document1
Printing Document2
No jobs left!
Practice Problem 4. Customer Service Counter Simulation (Queue)
Concept: Queue (FIFO) — Real-life waiting line
Scenario:
At a bank counter, customers are served one after another in the order they arrive.
When a customer is served, they leave the queue.
Boilerplate Code:
import [Link].*;
public class CustomerServiceCounter {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Command (ARRIVE
<name>/SERVE/STATUS/EXIT): ");
String cmd = [Link]();
// TODO: Handle ARRIVE - add to queue // TODO: Handle
SERVE - remove from queue // TODO: Handle STATUS -
display waiting list // TODO: Handle EXIT - break
}
}
}
Example Input:
ARRIVE Alice
ARRIVE Bob
ARRIVE Charlie
SERVE
SERVE
STATUS
Output:
Serving Alice
Serving Bob
Waiting: [Charlie]
Hint:
Model each customer as a string and maintain the waiting list using a queue.
Program
import [Link].*;
public class CustomerServiceCounter {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Command (ARRIVE
<name>/SERVE/STATUS/EXIT): ");
String cmd = [Link]();
if ([Link]("ARRIVE")) {
String name = [Link]();
[Link](name);
[Link](name + " arrived.");
} else if ([Link]("SERVE")) {
if (![Link]()) {
[Link]("Serving " + [Link]());
} else {
[Link]("No customers waiting!");
}
} else if ([Link]("STATUS")) {
if ([Link]()) {
[Link]("No one is waiting.");
} else {
[Link]("Waiting: " + queue);
}
} else if ([Link]("EXIT")) {
break;
} else {
[Link]("Invalid command!");
}
}
}
}
Output
ARRIVE Alice
ARRIVE Bob
ARRIVE Charlie
SERVE
SERVE
STATUS
Output:
Serving Alice
Serving Bob
Waiting: [Charlie]
Practice Problem 5. Expression Validator for Calculator App (Stack)
Concept: Stack — Parenthesis Validation
Scenario:
You’re building a simple calculator app. Before evaluation, you must check if the
mathematical expression entered by the user has balanced parentheses.
Boilerplate Code:
import [Link].*;
public class ExpressionValidator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter expression: ");
String exp = [Link]();
Stack<Character> stack = new Stack<>();
// TODO: Traverse characters
// TODO: Push opening brackets
// TODO: Pop for closing brackets and validate //
TODO: At end, print if balanced or not }
}
Example Input:
(a+b)*{c/[d-e]}
Output:
Expression is Balanced
Hint:
Push opening brackets; when a closing one appears, pop and check the match. Practice
Problem 6. Hospital Emergency Room (Priority Queue Concept)
Concept: Priority Queue — High priority patients treated first
Scenario:
In an emergency room, each patient has a name and a severity level (1 = low, 3 =
high). Doctors treat higher-severity patients first, regardless of arrival order.
Boilerplate Code:
import [Link].*;
class Patient {
String name;
int priority;
Patient(String n, int p) { name = n; priority = p; } }
public class EmergencyRoom {
public static void main(String[] args) {
PriorityQueue<Patient> pq = new
PriorityQueue<>([Link](p -> -[Link]));
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("Command (ARRIVE <name>
<priority>/TREAT/STATUS/EXIT): ");
String cmd = [Link]();
// TODO: Handle ARRIVE - add patient // TODO: Handle TREAT
- poll highest priority // TODO: Handle STATUS - print
remaining patients // TODO: Handle EXIT - break
}
}
}
Example Input:
ARRIVE John 2
ARRIVE Alice 3
ARRIVE Bob 1
TREAT
TREAT
STATUS
Output:
Treating Alice (Priority 3)
Treating John (Priority 2)
Waiting: [Bob]
Hint:
Use a PriorityQueue where patients are ordered by severity.