Saturday, August 13, 2011

Stack using Array in Java+Complete Source Code

Stack is a Linear Data Structure.Stack can be Implemented easily by using an array in Java or C.Every Stack has two Main Operations/Methods

a)Push()-Used to Insert an Element to the Stack

b)Pop()- Used to Remove an Element from the Stack

Stack follows a LIFO (Last in First Out)approach.In this method the element inserted last is removed first.The Topmost Element of the Stack is referred to as the top of the stack.When an element is inserted it is inserted at a position above the top.Pop method is used to remove an element from the Stack and always the element at the top is removed from the Stack.
Stack may Throw two exceptions 1.Stack Overflow 2.Stack Empty these can be handled by checking if(top=array length) and if(top=0) respectively.The Complete Source Program to Implement a Stack using array is Provided Below.


 package stack;  
 import java.io.*;  
 import java.lang.*;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 /**  
  *  
  * @author www.c-madeeasy.blogspot.com  
  */  
  class mystack {  
  DataInputStream get=new DataInputStream(System.in);  
  int a[];  
  int i,top=0,n,item,out;  
  void getdata()  
  {  
  try  
   {  
   System.out.println("Enter the limit");  
   n=Integer.parseInt(get.readLine());  
   a=new int[n];  
   }   
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void push(int item)  
  {  
    if(top==n)  
    {  
      System.out.println("STACK IS FULL");  
    }  
    else  
    {  
    a[top]=item;  
    top++;  
    }  
  }  
  void pop()  
  {  
    if(top==0)   
    {  
     System.out.println("STACK EMPTY");   
    }  
    else  
    {  
      top--;  
      out=a[top];  
    }  
    System.out.println(out);  
  }  
  void display()  
  {  
    if(top==0){  
      System.out.println("STACK EMPTY");    
    }  
    else  
   {  
   for(i=top-1;i>=0;i--)  
    System.out.println(+a[i]);  
   }  
  }  
   }  
 class Stack  
 {  
   public static void main(String[]args)  
   {  
      DataInputStream get=new DataInputStream(System.in);  
  int ch = 0,t = 0;  
  mystack obj=new mystack();  
  obj.getdata();  
  System.out.println("1.PUSH 2.POP 3.DISPLAY");  
     try {  
       ch=Integer.parseInt(get.readLine());  
     } catch (IOException ex) {  
       Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     while(ch!=4)  
     {  
       System.out.println("1.PUSH 2.POP 3.DISPLAY");  
  switch(ch)  
  {  
    case 1:  
      try{  
      t=Integer.parseInt(get.readLine());  
      }  
      catch(IOException e)  
      {  
      }  
      System.out.println("value");  
       try {  
       t=Integer.parseInt(get.readLine());  
       obj.push(t);  
     } catch (IOException ex) {  
       Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);  
     }  
      break;  
    case 2:  
      obj.pop();  
      break;  
          case 3:obj.display();  
            break;  
  }  
 }  
   }  
 }  

No comments:

Post a Comment

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...