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...

Static Member Functions

  What is static member functions? A static member function can access static data members and static member functions inside or outside of the class. Static member functions have a scope inside the class and cannot access the current object pointer. You can also use a static member function to determine how many objects of the class have been created. Static Member Function in a class is the function that is declared as static because of which function attains certain properties as defined below: A static member function is independent of any object of the class.  A static member function can be called even if no objects of the class exist. A static member function can also be accessed using the class name through the scope resolution operator. A static member function can access static data members and static member functions inside or outside of the class. Static member functions have a scope inside the class and cannot access the current object pointer. You can also u...

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...