Monthly Archives: March 2017

Stack

package com.ravis;

public class Stack {
int pointer = -1;
int stackMaxLength = 10;
int container[] = new int[stackMaxLength];

public Stack(){

for (int i = 0; i < container.length; i++) {
try {
this.push(i);
} catch (Exception e) {
e.printStackTrace();
}

}
for (int i = 0; i < container.length; i++) {
try {
this.pop();
} catch (Exception e) {
e.printStackTrace();
}
}

} public static void main(String[] args) {
new Stack();
}
void push(final int newValue) throws Exception{
if(pointer == stackMaxLength){
System.out.println(“Stack is full”); return;
}else{
container[++pointer] = newValue;
}
}
int pop() throws Exception{
if(pointer == -1){
System.out.println(“Stack is empty”);
new Exception();
}else{
System.out.println(“Pop the value –> “+container[pointer]);
–pointer;
}
return pointer;
}
}

Advertisements