Skip to main content

Write a program for stack operation in java ?

package com.chinta.datastructure;

import java.util.EmptyStackException;
import java.util.Stack;

public class StackOperation {

public static void showpush(Stack st,int a){
st.push(new Integer(a));
System.out.println("push :"+a);
System.out.println("stack "+st);
}
public static void showpop(Stack st){
System.out.println("pop->");
Integer a=(Integer) st.pop();
System.out.println(a);
System.out.println(st);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
   Stack st=new Stack();
            showpush(st, 23);
            showpush(st, 24);
            showpush(st, 25);
            showpush(st, 26);
            showpop(st);
            showpop(st);
            showpop(st);
        System.out.println("top is"+st.peek());
           // showpop(st);
            try{
            showpop(st);
            }
            catch(EmptyStackException ex){
            ex.printStackTrace();
            }
           
           
}

}

Comments

Popular posts from this blog

Find 2nd Largest Number in java ?

import java.util.Scanner; public class SocondLargest {     public static void main(String[] args) {         int secondlargest = Integer.MIN_VALUE;         int largest = Integer.MIN_VALUE;         System.out.println(largest);         Scanner input = new Scanner(System.in);         System.out.println("Enter array values: ");         int arr[] = new int[5];         for (int i = 0; i < arr.length; i++) {             arr[i] = input.nextInt();             if (largest < arr[i]) {                 secondlargest = largest;                 largest = arr[i];  ...

Check the number is Armstrong or not ??

import java.util.Scanner; public class Armstrong { public static void main(String[] args) { System.out.println("Enter the number ..."); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int temp=n; int sum=0 ; while(n>0){  int d=n%10;  sum=sum+d*d*d;  n=n/10; } if(temp==sum){ System.out.println("armstrong"); } else{ System.out.println("not armstrong"); }       sc.close(); } }

Custom exceptional Handling in java

class InvalidAgeException extends Exception { private static final long serialVersionUID = 1L; InvalidAgeException(String s) { super(s); } } public class Custom { public static void age(int age) throws InvalidAgeException { if (age < 18) throw new InvalidAgeException("not valid"); else System.out.println("welcom to vote"); } public static void main(String[] args) { try { age(19); } catch (Exception ex) { System.out.println("exception occured " + ex); } } }