Stack in Java

package temp;

import java.util.Iterator;
import java.util.Stack;

public class StackTest {

/**
* @param args
*/

Stack<Integer>st=new Stack<Integer>();
public void push()
{

st.add(10);
st.add(20);
st.add(30);
}//end of push

public void display()
{
Iterator itr=st.iterator();
while(itr.hasNext())
{
System.out.println(“Contents of stack are”+itr.next());
}

}

public void pop()
{
if(st.isEmpty())
{
System.out.println(“Stack is Empty”);

}

else
{
st.remove(0);
}

}//end of pop
public static void main(String[] args) {
// TODO Auto-generated method stub
StackTest s1=new StackTest();
s1.push();
s1.display();
s1.pop();
System.out.println(“Stack Contents after pop is “);
s1.display();

}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Java and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s