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

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

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

Hadoop file Management Tasks

  Implement the following file management tasks in Hadoop: a) Adding files and directories b) Retrieving files c) Deleting files Hint: A typical Hadoop workflow creates data files (such as log files) elsewhere and copies them into HDFS using one of the above command line utilities. Program:  The most common file management tasks in Hadoop includes: Adding files and directories to HDFS Retrieving files from HDFS to local filesystem Deleting files from HDFS Hadoop file commands take the following form:     hadoop fs - cmd Where cmd is the specific file command and <args> is a variable number of arguments. The command cmd is usually named after the corresponding Unix equivalent. For example, the command for listing files is ls as in Unix. a) Adding Files and Directories to HDFS Creating Directory in HDFS    $ hadoop fs - mkdir foldername (syntax)  $ ha...