Skip to main content

Implement Stack in java

Implement Stack in java

Program

import java.util.Stack;

import java.util.Scanner;//Scanner class reads data from input stream

class Stackdemo

{

 public static void main(String[] args)

 {

  int size=5;

  Stack<Integer> obj=new Stack<Integer>();//Stack class is accepting wrapper class Integer type of data

  Scanner s=new Scanner(System.in);

  while(true)

  {

   System.out.println("1.Push");

   System.out.println("2.Pop");

   System.out.println("3.Display");

   System.out.println("4.Quit");

   System.out.println("Enter choice");

   int choice=s.nextInt();

   switch(choice)

   {

     case 1:System.out.println("Enter the element to be pushed into stack");

            int e=s.nextInt();

            if(obj.size()==size)

            System.out.println("Stack overflow");

            else

            obj.push(e);//pushes element into stack

            break;

     case 2:if(obj.isEmpty())//checks whether stack is empty or not

            System.out.println("Stack underflow");

            else

            System.out.println("Element popped from stack"+obj.pop());

            break;

     case 3:if(obj.isEmpty())

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

            else

            {

            System.out.println("Stack elements");

            for(int i=obj.size()-1;i>=0;--i)//obj.size() gives no of elements in stack

            System.out.println(obj.get(i));//get()-returns the element from stack based on position

            }

            break;

     case 4:System.exit(0);

     default:System.out.println("Wrong choice");

    }

   }

  }

}


Expected Output

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

10

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

20

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

30

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

40

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

50

1.Push

2.Pop

3.Display

4.Quit

Enter choice

1

Enter the element to be pushed into stack

60

Stack overflow

1.Push

2.Pop

3.Display

4.Quit

Enter choice

3

Stack elements

50

40

30

20

10

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Element popped from stack50

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Element popped from stack40

1.Push

2.Pop

3.Display

4.Quit

Enter choice

3

Stack elements

30

20

10

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Element popped from stack30

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Element popped from stack20

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Element popped from stack10

1.Push

2.Pop

3.Display

4.Quit

Enter choice

3

Stack is empty

1.Push

2.Pop

3.Display

4.Quit

Enter choice

2

Stack underflow

1.Push

2.Pop

3.Display

4.Quit

Enter choice

4


Comments

Popular posts from this blog

Machine Learning Lab Internal Questions

 Internal Lab Programs 1. Read a CSV File, apply preprocessing, apply a few data visualizations, and train SVM model for a dataset and calculate its accuracy. Download DATASET 2. Read the CSV File, apply preprocessing, and train the data decision tree-based ID3 algorithm. Calculate each attribute value and display  3. Create a data and save it CSV, apply preprocessing by using that file demonstrate the FIND-S algorithm for finding the most specific hypothesis based on a given set of training data samples. 4. Read the super Market CSV file apply relevant visualizations (Box plot, Scatter plot, Bargraphs), linear regression and display performance metrics 5. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering using k-Means algorithm. Compare the results of these two algorithms and comment on the quality of clustering. 6. Write a program for implementing Density based clustering algorithm.  7. Write a pr...

How to Install Parrot Operating System in Virtual Box using OVA

Step by Step Process of Parrot OS Installation What is Parrot OS Parrot is a free and open-source Linux system based on Debian that is popular among security researchers, security experts, developers, and privacy-conscious users. It comes with cyber security and digital forensics arsenal that is totally portable. It also includes everything you'll need to make your own apps and protect your online privacy. Parrot is offered in Home and Security Editions, as well as a virtual machine and a Docker image, featuring the KDE and Mate desktop environments. Features of Parrot OS The following are some of the features of Parrot OS that set it apart from other Debian distributions: Tor, Tor chat, I2P, Anonsurf, and Zulu Crypt, which are popular among developers, security researchers, and privacy-conscious individuals, are included as pre-installed development, forensics, and anonymity applications. It has a separate "Forensics Mode" that does not mount any of the system's hard...

Big Data Analytics Programs

  List of Programs for Big Data Analytics   CLICK ON ME 1.  Implement the following Data structures in Java       a)  Linked Lists            b)   Stacks       c)  Queues     d)   Set            e)   Map 2.  Perform setting up and Installing Hadoop in its three operating modes:      Standalone,     Pseudo distributed,     Fully distributed. 3.  Implement the following file management tasks in Hadoop:    a) Adding files and directories    b) Retrieving files    c) Deleting files 4. Run a basic Word Count Map Reduce program to understand Map Reduce Paradigm. 5. Write a Map Reduce program that mines weather data.     Weather sensors collecting data every hour at many locations across the globe gather a large volume of log data, which is a ...