Skip to content

Commit 97a79ec

Browse files
committed
feat: Implement custom stack with StackOverFlow and StackUnderFlow exceptions
WHAT the code does: - Defines two custom exception classes: - StackOverFlow → thrown when pushing into a full stack. - StackUnderFlow → thrown when popping from an empty stack. - Implements Stack class: - Uses an integer array S[] with fixed capacity. - Maintains size and top index (initialized to -1). - push(int x): - If stack is full (top == size - 1), throws StackOverFlow. - Otherwise, increments top and stores the element. - pop(): - If stack is empty (top == -1), throws StackUnderFlow. - Otherwise, returns the top element and decrements top. - Demonstrates usage in StackOverAndUnderFlow main(): - Creates a stack of size 5. - Pushes six elements, triggering StackOverFlow. - Exception is caught and printed. WHY this matters: - Shows how to implement a stack data structure with robust error handling. - Demonstrates custom exceptions for domain-specific errors, instead of relying on generic runtime exceptions. - Reinforces defensive programming: preventing invalid operations before they cause silent failures or array out-of-bounds errors. HOW it works: 1. Stack(5) → initializes an empty stack with capacity 5. 2. Push operations increment top and fill stack until it reaches index 4. 3. On the 6th push, condition top == size - 1 evaluates true → throws StackOverFlow. 4. catch(StackOverFlow) prints "Stack is Full". Tips and gotchas: - Always define clear custom exceptions when domain rules are broken (stack overflow/underflow are classic examples). - push() and pop() can be tested separately: - Call pop() immediately on a new stack to trigger StackUnderFlow. - Call push() beyond capacity to trigger StackOverFlow. - In real-world systems, consider using java.util.Stack or Deque, but writing it manually builds deep understanding. - Avoid hardcoding "5"; instead make size dynamic or user-defined. Use-cases / analogies: - Undo/redo operations in text editors (stack of actions). - Balanced parentheses checking in compilers. - Function call stack simulation in interpreters. - Browser back/forward navigation history. Short key: java stack custom-exception overflow-underflow defensive-programming. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent eaf9249 commit 97a79ec

File tree

1 file changed

+12
-74
lines changed

1 file changed

+12
-74
lines changed
Lines changed: 12 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
1-
//Pending solve it
2-
class StackOverFlow extends Exception
3-
{
4-
public String toString()
5-
{
1+
class StackOverFlow extends Exception {
2+
public String toString() {
63
return "Stack is Full";
74
}
85
}
96

10-
class StackUnderFlow extends Exception
11-
{
12-
public String toString()
13-
{
7+
class StackUnderFlow extends Exception {
8+
public String toString() {
149
return "Stack is Empty";
1510
}
1611
}
1712

18-
class Stack
19-
{
13+
class Stack {
2014
private int size;
2115
private int top=-1;
2216
private int S[];
2317

24-
public Stack(int sz)
25-
{
18+
public Stack(int sz) {
2619
size=sz;
2720
S=new int[sz];
2821
}
2922

30-
public void push(int x) throws StackOverFlow
31-
{
23+
public void push(int x) throws StackOverFlow {
3224
if(top==size-1)
3325
throw new StackOverFlow();
3426
top++;
3527
S[top]=x;
3628
}
3729

38-
public int pop() throws StackUnderFlow
39-
{
30+
public int pop() throws StackUnderFlow {
4031
int x=-1;
4132

4233
if(top==-1)
@@ -47,72 +38,19 @@ public int pop() throws StackUnderFlow
4738
}
4839
}
4940

50-
public class StackOverAndUnderFlow
51-
{
52-
public static void main(String[] args)
53-
{
41+
public class StackOverAndUnderFlow {
42+
public static void main(String[] args) {
5443
Stack st=new Stack(5);
55-
try
56-
{
44+
try {
5745
st.push(10);
5846
st.push(15);
5947
st.push(10);
6048
st.push(15);
6149
st.push(10);
6250
st.push(15);
63-
6451
}
65-
catch(StackOverFlow s)
66-
{
52+
catch(StackOverFlow s) {
6753
System.out.println(s);
6854
}
69-
70-
}
71-
}
72-
73-
/*
74-
class StackOverFlow extends IllegalStateException {
75-
76-
@Override
77-
public String toString() {
78-
return "Stack is Full";
79-
}
80-
}
81-
82-
class StackUnderFlow extends IllegalStateException {
83-
84-
@Override
85-
public String toString() {
86-
return "Stack is Empty";
8755
}
8856
}
89-
90-
class Stack {
91-
92-
private final int[] values;
93-
private int count = 0;
94-
95-
public Stack(int capacity) {
96-
values = new int[capacity];
97-
}
98-
99-
public int getCapacity() {
100-
return values.length;
101-
}
102-
103-
public void push(int x) throws StackOverFlow {
104-
if (count == values.length) {
105-
throw new StackOverFlow();
106-
}
107-
values[count] = x;
108-
++count;
109-
}
110-
111-
public int pop() throws StackUnderFlow {
112-
if (count == 0) {
113-
throw new StackUnderFlow();
114-
}
115-
--count;
116-
return values[count];
117-
}
118-
}*/

0 commit comments

Comments
 (0)