ONLINE SUMMER TRAINING Online Courses Free Tutorials 
 Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
0 like 0 dislike
124 views
in VTU B.Tech (CSE-IV Sem) Design and Analysis of Algorithm Lab by Goeduhub's Expert (5.8k points)

Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and Display() methods to demonstrate its working.

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-

S.No.

Course Name

Apply Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon

3.

Complete Machine Learning & Data Science with Python Apply Coupon

4.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

5.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon

6.

Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
 
Best answer

For Theory of Array Implementation of Stack Click here

Java Program

import  java.io.*;

class StackArray

{

    static int max=10,i,top,ch,item;

    static int a[]=new int[10];

    StackArray()

    {

        top=-1;

    }

    public static void main(String args[])throws IOException

    {

        while((boolean)true)

        {

            System.out.print("\n1.Push\n2.Pop\n3.Display\n4.Exit\nEnter your choice:");

            try

            {

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                ch=Integer.parseInt(br.readLine());

            }

            catch(Exception e) { }

            if(ch==4)

                break;

            else

            {

                switch(ch)

                {

                case 1:

                    push();

                    break;

                case 2:

                    pop();

                    break;

                case 3:

                    display();

                    break;

                }

            }

        }

    }

    static void push()

    {

        if(top==max)

            System.out.println("Stack is full!");

        else

            try

            {

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                System.out.print("Enter the element: ");

                item=Integer.parseInt(br.readLine());

                a[++top]=item;

            }

            catch(Exception e) { }

    }

    static void pop()

    {

        if(top==-1)

            System.out.println("Stack is empty!");

        else

            top--;

        System.out.print("Popped item: "+a[top]);

    }

    static void display()

    {

        System.out.print("Elements in stack are: ");

        for(i=top; i>0; i--)

            System.out.print(a[i]+" ");

    }

}

Output

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element: 10

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element: 20

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:1

Enter the element: 30

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:3

Elements in stack are: 30 20 10

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:2

Popped item: 20

1.Push

2.Pop

3.Display

4.Exit

Enter your choice:4


For more VTU IV Sem DAA Lab Experiments Click here


Our Mentors(For AI-ML)


Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune

NISHA (IIT BHU)

Ms. Nisha

An alumna of IIT-BHU

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...