Wednesday, 14 November 2018

How To implement Stack in Java


public class MyStack {

int data[]=new int[5];

int top=0;
static int size=0;

public void push(int value) {
data [top]=value;
top++;
size++;
}

int pop() {
int d=0;
for(int i=size-1; i>=0; i--) {
System.out.println(data[i]);
d=data[i];
data[i]=0;
}

return d;
}

public static void main(String[] args) {

MyStack st=new MyStack();
st.push(1);
st.push(5);
st.push(2);
st.push(0);
st.push(1);
st.pop();
}

}

No comments:

Post a Comment