Implementation of stack using Array in java

 stack array implementation in java

public class stacks {
int size;
int arr[];
int top;
stacks(int size){
this.size=size;
this.arr=new int[size];
this.top=-1;
}
public void push(int data){
if (top==(size-1)){
System.out.println("overflow");
}
else{
top++;
arr[top]=data;
System.out.println("push elem " + data);
}

}
public void display(){
int temp=top;
for(int i=temp;temp>=0;temp--){
System.out.println(arr[temp]);
}
}
public void peek(){
if (top==-1){
System.out.println("empty stack");
}
else{
System.out.println(" peek is " + arr[top]);
}
}
public void pop(){
if (top==-1){
System.out.println("empty");
}
else{
int temp=top;
top--;
System.out.println("pop element is " + arr[temp]);
}
}
public static void main(String[] args) {
stacks ss=new stacks(3);
ss.push(10);
ss.push(20);
ss.push(30);
ss.display();
ss.peek();
ss.pop();
ss.display();
ss.peek();

}
}

Comments

Popular posts from this blog

Back / Flip Cover Universal List

Super-D, 11D, Super X tempered glass Universal list