Write a Java program that Implements stack ADT.

//Program to implement Stacks
import java.util.*;
import java.io.*;
class Stack1
{
            int t;
            int a[]=new int[5];
            Stack1()
            {
                        t=-1;
            }
            boolean isempty()
            {
                        return (t==-1);
            }
            boolean isfull()
            {
                        return (t==5-1);
            }
            void push(int x)
            {
                        a[++t]=x;
                        System.out.println(“nOne Element inserted in the stack”);
            }
            int pop()
            {
                        System.out.println(“nOne element deleted from stack”);
                        return (t–);
            }
            void display()
            {
                        int i;
                        System.out.print(“nElements in the stack are:”);
                        for(i=t;i>=0;i–)
                        System.out.print(a[i]+”,”);
            }
}
class Stack
{
            public static void main(String args[]) throws IOException
            {
                        int m,n,x;
                        Scanner scr=new Scanner(System.in);                       
                        Stack1 s=new Stack1();
                        do
                        {
                                    System.out.println(“n     MENU”);
                                    System.out.println(”    ——“);
                                    System.out.println();
                                    System.out.println(“1.PUSHn2.POPn3.DISPLAYn4.EXIT”);
                                    System.out.print(“nEnter your choice:”);
                                    n=scr.nextInt();
                                    switch(n)
                                    {
                                        case 1: if(s.isfull())
                                                    System.out.println(“nStack is Full”);
                                                    else
                                                    {
                                                            System.out.print(“nEnter any number into stack:”);
                                                            x=scr.nextInt();
                                                            s.push(x);
                                                     }
                                                      break;
                                       case 2: if(s.isempty())
                                                   System.out.println(“Stack is empty”);
                                                   else
                                                   {
                                                            s.pop();
                                                            s.display();
                                                    }
                                                  break;
                                       case 3: if(s.isempty())
                                                   System.out.println(“Stack is empty”);
                                                   else
                                                   s.display();
                                                   break;
                                       case 4: break;
                                       default:System.out.println(“Invalid choice”);
                                      }
                        }while(n!=4);
            }
}
Output:
     MENU
     ———
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1
Enter any number into stack:10

One Element inserted in the stack

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks