Tuesday 21 August 2012

To implement the stack using Array.



import java.io.*;
import java.lang.*;
import java.util.*;


class Stack
{
int top = -1;
int stk[];
int max;

 Stack(int size)
{
max=size;
 stk=new int[max];

}


void push(int item)
{
  if(top==max-1)
{
System.out.println("Stack overflow");
}
else
{
top=top+1;
 stk[top]=item;
display();
}
}

public boolean stackoverflow()
{
if(top==max)
return true;
else
return false;
}

boolean isempty()
{
 if(top== -1)
   return true;  
else
return false;
}


public void pop()
 {

    if(isempty())
  {
    System.out.println("Stack Underflow...");
 
  }
 else
{
top=top-1;
  System.out.println("Element is Deleted..");
display();
 }

}


void stacktop()
{
if (isempty())
System.out.println("Stack underflow..");

else

System.out.println("Stack top is" +(stk[top]));
}


void display()
 {
   if (isempty())
System.out.println("Stack underflow..");

else
 {
   System.out.println("STACK--->");
   
   for(int i=top;  i>=0 ;i--)
    System.out.println(stk[i]);
    }
}



int peek()
{
 if(isempty())
  {
    System.out.println("Stack Empty...");
    return 0;
  }
else
{
System.out.println("The top most element is: " +stk[top]);
return stk[top];
}
}


public void search(int item)
{
 if(isempty())
  {
    System.out.println("Stack Empty...");

  }
else
{
 for(int i=top;  i>=0 ;i--)
{
if(stk[i]==item)
System.out.println("Element found..");
else
System.out.println("Element not found..");
}

}


}
}


class Mystack1
{
  public static void main(String[] args)
{
int s,el,ch;
   Scanner src=new Scanner(System.in);

System.out.println("\n\n\t\tWELCOME TO STACK IMPLEMENTATION USING ARRAY");
System.out.println("\n\t======================================================");

System.out.println("\n\n\t\tMAIN MENU...");

System.out.println("\n\n\t1.Push Element...");
System.out.println("\n\t2.Pop Element...");
System.out.println("\n\t3.Display Stack...");
System.out.println("\n\t4.Peek...");
System.out.println("\n\t5.Search Element...");
System.out.println("\n\t6.Exit...");

System.out.println("\n\n\tEnter the size of Array: ");
s=src.nextInt();

Stack p=new Stack(s);

while(true)
{



System.out.println("\n\tEnter the input : ");
ch=src.nextInt();

switch(ch)
{

 case 1:
                System.out.println("\n\tEnter the element : ");
                el=src.nextInt();                        
                 p.push(el);            
                  break;            

case 2:        p.pop();
                   break;

case 3:p.display();
          break;

case 4:
p.peek();
break;

case 5:
 System.out.println("\n\tEnter the element : ");
el=src.nextInt();
p.search(el);
break;

}
if(ch==6)
break;
}
}
}



 OUTPUT
  ========


C:\Program Files\Java\jdk1.7.0_03\bin>javac Mystack1.java

C:\Program Files\Java\jdk1.7.0_03\bin>java Mystack1


                WELCOME TO STACK IMPLEMENTATION USING ARRAY

        ======================================================


                MAIN MENU...


        1.Push Element...
        2.Pop Element...
        3.Display Stack...
        4.Peek...
        5.Search Element...
        6.Exit...


        Enter the size of Array: 3

     
        Enter the input : 1
        Enter the element : 10

        STACK--->
        10

        Enter the input : 1
        Enter the element : 20

        STACK--->
        20
        10

        Enter the input : 1
        Enter the element : 30

        STACK--->
        30
        20
        10

        Enter the input : 2

        Element is Deleted..

        STACK--->
        20
        10

        Enter the input : 4

        The top most element is: 20

        Enter the input : 5
        Enter the element : 20

        Element found..
        Element not found..

        Enter the input : 3

        STACK--->
        20
        10

No comments:

Post a Comment