Stack Program
Stack Program
Scanner;
public class StackPrg {
void push(int e) {
tos++;
S[tos] = e;
}
boolean isFull() {
if(tos == S.length)
return true;
else
return false;
}
int pop() {
int temp;
temp = S[tos];
tos-- ;
return temp;
}
boolean isEmpty() {
if(tos == -1)
return true;
else
return false;
}
void printStack() {
int i;
for(i = tos; i >=0; i--)
System.out.println(S[i]);
}
do {
System.out.println("\n 1.Push\n 2.Pop\n 3.Print\n 0.Exit\n ");
ch = in.nextInt();
switch(ch) {
case 1:
try {if(obj.isFull()!= true)
System.out.println("Enter:");
int e = in.nextInt();
obj.push(e);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Stack full");
}
/* else {
System.out.println("Stack full");
}*/
break;
case 2:
if(obj.isEmpty()!= true) {
System.out.println("Poped: " + obj.pop());
}
else {
System.out.println("Stack Empty");
}
break;
case 3:
if(obj.isEmpty()!= true) {
obj.printStack();
}
else {
System.out.println("Stack empty");
}
break;
case 0:
System.out.println("Exiting......");
break;
default:
System.out.println("Wrong choice");
break;
}
} while(ch!=0);
}
}