Another question Solution Create an interface stack with methods - TopicsExpress



          

Another question Solution Create an interface stack with methods push() and pop(). write a class to implement this interface so that the methods perform push and pop operation on stack interface Stack{ public void push(int e); public void pop(); } class MyStack implements Stack{ int maxSize; int[] items; int top; public MyStack(int maxSize) { this.maxSize=maxSize; items=new int[maxSize]; top=-1; } public void push(int e){ items[++top]=e; } public void pop(){ --top; } //u can also add these methods public boolean isEmpty(){ return (top==-1); } public int getValue(){ return items[top--]; } } public class StackExample { public static void main(String args[]){ MyStack st=new MyStack(10); st.push(5); st.push(10); st.push(12); st.push(14); st.push(25); st.push(27); st.pop(); while(!st.isEmpty()){ System.out.println(st.getValue()); } } } output 25 14 12 10 5
Posted on: Tue, 18 Jun 2013 05:13:47 +0000

Trending Topics



Recently Viewed Topics




© 2015