Pages

12 June 2012

Implementation of Stack ADT using java

Finding tough to write your own Source codes in DSF ( Data Structures and Files).We've come out with a new series of Source Codes for all those engineers.We are starting of with implementations of STACK ADT.
http://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/pix/stack.bmp
Here's a simple java code for the same.



PROGRAM FOR IMPLEMENTATION OF STACK ADT
import java.util.Scanner;

/**
 *
 * @author THE CYBER SOUL
 */
public class Stack {
    private int MAX;
    private int pos;
    private Object stack[];
    public Stack()
    {
        stack=new Object[10];
        pos=-1;
        MAX=10;
    }
    public boolean push(Object data)
    {
        if (pos==MAX-1)
                {
                    return false;
                }
        else{
            pos++;
            stack[pos]=data;
            return true;
        }
    }
    public Object pop()
    {
        Object data;
        if (pos==-1)
        {
            return null;
        }
        else
        {
            data=stack[pos];
            pos--;
            return data;
        }
    }
    public static void main(String a[])throws Exception
    {
        int choice;
        Object data;
        Scanner s=new Scanner(System.in);
        Stack S=new Stack();
        do
        {
          System.out.println("1.Push Data into Stack"); 
                  System.out.println("2.Pop Data from Stack"); 
                          System.out.println("3.Exit");
                                  System.out.println("Enter your choice"); 
        choice=s.nextInt();
        if (choice==1)
        {
            System.out.println("Enter data");
            data=s.next();
            boolean status=S.push(data);
            if(status==false)
            {
                System.out.println("Overflow");
            }
            else
            {
                System.out.println("Sucessful");
            }
        }
        if(choice==2)
            {
             Object dataPopped=S.pop();
             if(dataPopped==null)
             {
                 System.out.println("Underflow");
             }
             else
             {
                 System.out.println("Popped data is "+dataPopped);
             }
     
       
     
  
}}while(choice!=3);
        }
                }



Kindly post your requirements.We will try to fulfill  them as quickly
            
             


Technorati Delicious Reddit BlinkList Furl Mixx Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

0 comments:

Post a Comment

Please Feel free to Share your view with us ....

Related Posts Plugin for WordPress, Blogger...
Search Engine Submission - AddMe Hypersmash.com Blogarama - The Blog Directory Bloggers - Meet Millions of Bloggers