You are on page 1of 2

6. Program for implementing all stack operations.(java) ---------------------------------------------------------------------------------------------------------------------Aim : Write program for implementing all stack operations.

Source code : import java.io.*; class stack { int st[]=new int[5]; int n,top=0,i; void push(int p) { if(top>=5) System.out.println( Sorry ! Stack is full ); else st[top++]=p; } void pop() { if(top==0) System.out.println( Stack is empty ); else { n=st[--top] System.out.println( The deleted element is +n); } } public void display() { if(top==0) System.out.println( Stack is empty ); else for(i=0;i<top;i++) System.out.println( +st[i]); } } class mainstack { public static void main(String arg[]) { int ch,p; stack s=new stack(); DataInputStream in=new DataInputStream(System.in); try { do { System.out.println( \n1. PUSH ); System.out.println( \n2. POP ); System.out.println( \n3. DISPLAY ); System.out.println( \n4. EXIT ); System.out.println( \ENTER YOUR CHOICE );

ch=Integer.parseInt(in.readLine()); switch(ch) { case 1: System.out.println( ENTER THE ELEMENT ); p= Integer.parseInt(in.readLine()); s.push(p); break; case 2: s.pop(); break; case 3: s.display(); break; case 4: break; } }while(ch!=4) } catch(Exception e) { System.out.println(e); } } }

You might also like