Java Program for Stack Simulation
Program
import java.io.*;
import java.util.*;
public class stack {
private final int size = 10;
private int top;
private int st[];
stack() {
top = -1;
st = new int[size];
}
void push(int item) {
if (top == st.length - 1) {
System.out.println("Stack overflow");
return;
} else {
st[++top] = item;
System.out.println("Element pushed is:\t" + st[top]);
return;
}
}
void pop() {
if (top == -1) {
System.out.println("Stack underflow");
return;
} else {
System.out.println("Element poped is:\t" + st[top--]);
return;
}
}
void display() {
if (top == -1) {
System.out.println("Stack underflow");
return;
} else {
System.out.println("Contents of stack:");
for (int i = 0; i <= top; i++)
System.out.println(st[i]);
return;
}
}
}
public class StackSimulation {
public static void main(String[] args) {
stack s = new stack();
int choice;
Scanner sc = new Scanner(System.in);
System.out.println("STACK");
while (true) {
System.out.println("1. Push\n2. Pop\n3. Display\n4. Exit");
System.out.println("Enter choice");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the item to push");
int item = sc.nextInt();
s.push(item);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
sc.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
This program demonstrates stack simulation in Java using an array-based implementation. The stack operates on the LIFO (Last In, First Out) principle, supporting operations like push, pop, and display.
Key Concepts of the Stack:
- Push: Add an element to the top of the stack.
- Pop: Remove the topmost element from the stack.
- Display: Show all elements currently in the stack.
- Overflow and Underflow:
- Overflow occurs when trying to push an element into a full stack.
- Underflow occurs when trying to pop an element from an empty stack.
1. Class stack
:
- Implements the stack functionality.
- Data Members:
size
: Maximum capacity of the stack (set to 10).top
: Tracks the index of the topmost element (initialized to-1
).st[]
: Array to hold stack elements (size is10
). The stack is implemented using a fixed-size array (st[]
), withtop
tracking the index of the top element.
- Methods:
- Constructor (
stack()
):- Initializes
top
to-1
and creates the stack array with a fixed size (size
).
- Initializes
push(int item)
:- Adds a new element to the stack if it is not full.
- If the stack is full (
top == st.length - 1
), prints "Stack overflow".
pop()
:- Removes and returns the topmost element if the stack is not empty.
- If the stack is empty (
top == -1
), prints "Stack underflow".
display()
:- Displays all elements from the bottom to the top of the stack.
- If the stack is empty, prints "Stack underflow".
- Constructor (
2. Class StackSimulation
:
- Contains the
main()
method to simulate stack operations. - Steps:
- Creates an object of the
stack
class. - Uses a
while
loop to present a menu of stack operations:- Push: Add an element to the stack.
- Pop: Remove the top element from the stack.
- Display: Show the contents of the stack.
- Exit: Exit the program.
- Based on the user's choice, the program calls the appropriate method of the
stack
class.
- Creates an object of the
Output
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter choice
2
Stack underflow
1. Push
2. Pop
3. Display
4. Exit
Enter choice
1
Enter the item to push
10
Element pushed is: 10
1. Push
2. Pop
3. Display
4. Exit
Enter choice
1
Enter the item to push
20
Element pushed is: 20
1. Push
2. Pop
3. Display
4. Exit
Enter choice
1
Enter the item to push
30
Element pushed is: 30
1. Push
2. Pop
3. Display
4. Exit
Enter choice
3
Contents of stack:
10
20
30
1. Push
2. Pop
3. Display
4. Exit
Enter choice
4