Header Ads

How to Implement Stack in Java

How to Implement Stack in Java
Before we proceed further we need to understand what is Stack all about.
As per Wiki-One way of describing the stack is as a last in, first out (LIFO) abstract data type and linear data structure.

So our objective will be to 

1.create a data structure to hold values
2.should be able to insert values in the current position
3.Should be able to remove values from last 

For point#1 I will use array.This is easy to understand with Array. Later I will use some advance concept.So we need to create an array and a pointer which will provide me the current position of the array.let us initialize the the pointer to -1 . We need some control to restrict user for the pop without a push. and based on the size we can restrict user to push beyond array capacity.



int array_[];
int Stack_pointer_;
int Size_=0;
public void array_set(int Size)
{
array_=new int[Size];
Stack_pointer_=-1;

}
Well now we need to implement the point-2 that It should be able to push values to the array..The Steps will be 
1. Increment the the pointer by one during push.
2. Insert the value to the corresponding location.


public void push(int number) {
System.out.println("Stack pointer value"+Stack_pointer_);
array_[++Stack_pointer_]=number;
}
We will implement the point-3 that It should be able to pop values from the array.Here we need to decrease the pointer during pop. So only we need decrease the stack pointer value by one.
public long pop(){
//Stack_pointer_=Stack_pointer_-1;
System.out.println("Current Stack pointer value"+Stack_pointer_);
return array_[Stack_pointer_--];
}
Implementing the different Exception handling the code will look like-for Push


public void push(int number) {
    if (Stack_pointer_<Size_)
    {
    array_[++Stack_pointer_]=number;
    }
    else{
        System.out.println("Stack pointer value has reached to the limit");
    }
}
Exception handling the code will look like-for POP


public long  pop(int j){
    //Stack_pointer_=Stack_pointer_-1;
    if(Stack_pointer_==-1)
    {
        System.out.println("You can not pop here");
        return array_[Stack_pointer_];
    }
    else
    {
       System.out.println("Poping "+j+array_[j]);
      return  array_[Stack_pointer_--];
 
    }
   
}
The final code will looks like---



import java.io.PrintStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JOptionPane;

public class Calculator {
    int array_[];
    int Stack_pointer_;
    int Size_=0;
public void push(int number) {
    if (Stack_pointer_<Size_)
    {
    array_[++Stack_pointer_]=number;
    }
    else{
        System.out.println("Stack pointer value has reached to the limit");
    }
}
public long  pop(int j){
    //Stack_pointer_=Stack_pointer_-1;
    if(Stack_pointer_==-1)
    {
        System.out.println("You can not pop here"); 
        return array_[Stack_pointer_]; 
    }
    else
    {
       System.out.println("Poping "+j+array_[j]);
      return  array_[Stack_pointer_--]; 
   
    }
    
}
public void printarray() {
    for(int g=0;g<array_.length;g++) {
            System.out.println("The array"+g+"element is"+array_[g] );
        }
}
  
public void array_set(int Size) {
    Size_=Size;
    array_=new int[Size_];
    Stack_pointer_=-1;
    
}

public static void main(String Args[]) {
    int Size_,i;
    Size_ =Integer.parseInt( JOptionPane.showInputDialog(null, "What is your size?", "Enter your Array Size", JOptionPane.QUESTION_MESSAGE));
      
        Calculator calc=new Calculator();
        calc.array_set(Size_);
       for(i=0;i<Size_;i++)
       {
           int val =Integer.parseInt( JOptionPane.showInputDialog(null, "What is your new Number ?", "Enter your New Number", JOptionPane.QUESTION_MESSAGE));
           System.out.println("Pushing"+val);
        calc.push(val);    
           
       }
    calc.printarray();
        for(int j=--i;j>=0;j--)
        {
            System.out.println("Poping"+j);
            calc.pop(j);
        }
        

    }
  
}


Powered by Blogger.