Uipoka’s Weblog

Stack Implementation Using Java

with 2 comments

Stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used in the basic design of an operating system for interrupt handling and operating system function calls. Stacks are used to run a Java Virtual Machine. The stack is ubiquitous.
First i write an interface for implementing stack:

public interface Stacks {
	void push(Object x);
	void pop();
	Object top();
	Object topAndPop();
	boolean isEmpty();
	void makeEmpty();

Now look at the RishanStack class which implements the interface stacks

public class RishanStack implements Stacks{

	private static final int DEFAULT_CAPACITY = 10;
	private Object [] theArray; 
	private int topOfStack;
    public RishanStack() {
    	theArray = new Object[DEFAULT_CAPACITY];
    	topOfStack = -1;	
    public boolean isEmpty(){
    	return topOfStack == -1;
    public void makeEmpty(){
    	topOfStack = -1;	
    public void push(Object x){
    public Object top(){
    	if(isEmpty())throw new RishanStackException("Stack top");
    	return theArray[topOfStack];
    public void pop(){
    	if(isEmpty())throw new RishanStackException("Stack pop");
    public Object topAndPop(){
    	if(isEmpty())throw new RishanStackException("Stack top and pop");
    	return theArray[topOfStack--];
    public void status(){
    	for(int i=0;i<theArray.length;i++){
    public void increaseSize(){
    	Object &#91;&#93; dummy = new Object&#91;theArray.length*2&#93;;
    	for(int i=0;i<theArray.length;i++){
    	theArray = dummy;

And lastly the RishanStackException class which extends <em>java.io.RuntimeException</em>
and overridden constructor:

public class RishanStackException extends RuntimeException{
    public RishanStackException(String msg) {

Written by uipoka

August 13, 2008 at 7:18 pm

2 Responses

Subscribe to comments with RSS.

  1. thank u rishan vai.
    this very helpfullllllllllllllllll


    August 19, 2008 at 4:22 pm

  2. good job….love you…

    ha ha

    August 26, 2008 at 10:18 pm

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: