import java.util.Scanner;
class Stack {
Scanner sc = new Scanner(System.in);
int top = -1;
int stackSize=3;
int[] stack = new int[stackSize];
void push() {
if(top<stackSize-1) {
top++;
System.out.println("n:-"+top);
System.out.print("Enter the element you want to push :- ");
int element = sc.nextInt();
stack[top] = element;
System.out.println("Element inserted");
display();
} else {
System.out.println("Stack Overflow");
}
}
void pop() {
if(top>=0) {
System.out.println("n :- "+top);
System.out.println("Deleting last element from stack");
System.out.println("Deleted element :- " + stack[top]);
top--;
display();
}
else {
System.out.println("Stack Underflow");
}
}
void display() {
System.out.println(":::::STACK:::::");
if(top==-1){
System.out.println("Stack is Empty");
}
else {
for (int i = top; i >= 0; i--)
System.out.println(":: "+stack[i]+" ::");
}
System.out.println(":::::::::::::::");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack s1 = new Stack();
while (true) {
System.out.println(":::::::::::::::::::::::::::::::::::::::::::::::");
System.out.println("1. Push Element\n2. Pop Element\n3. Display\n4. Exit");
int input = sc.nextInt();
switch (input) {
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.display();
break;
default:
System.exit(0);
break;
}
}
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: