Stack implementation in Java

import org.testng.annotations.Test;

/**

* Test Stack

* @author ashishsharda

*

*/

public class StackerTest {

int[] st=new int[10];

int tos=-1;

/**

* Push an element to top of stack.

* @param value

*/

publicvoid push(int value)

{

if (tos==9)

{

System.out.println(“Stack is full”);

}

else

{

st[++tos]=value;

}

}

/**

* Pop element from stack.

* @return

*/

publicint pop()

{

int value = 0;

if(tos==-1)

{

System.out.println(“Stack is empty”);

System.exit(0);

}

else

{

value=st[tos];

tos–;

}

returnvalue;

}

@Test

public void testStack() {

StackerTest st1=new  StackerTest();

st1.push(10);

st1.push(20);

System.out.println(“Value popped is “+st1.pop());

}

}

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