Mon Oct 18 2021

Stack

File Name: stack.java

import java.io.*;
import java.util.*;

class stack {
	public static void main(String args[]) {
		Stack st = new Stack();
		Scanner scan = new Scanner(System.in);
		int opt = 0;
		while(opt != 4) {
			System.out.println("1. Push 2. Pop 3. Display 4. Exit");
			System.out.println("Enter your choice:");
			opt = Integer.parseInt(scan.nextLine());
			switch(opt) {
				case 1:
					System.out.println("Please Enter Value in stack:");
					String ab = scan.nextLine();

					/* Push data into the stack */
					st.push(new Integer(ab));
					System.out.println("Value pushed in stack!");
					break;
				case 2:

					/* Pop data from stack */
					st.pop();
					System.out.println("Item popped from the stack!");
					break;
				case 3:
					System.out.println("Stack:");

					/* Display data from stack */
					System.out.println(st);
					break;
				case 4:
					System.out.println("Bye Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Invalid choice!");
			}
		}
	}
}




/* Output */
1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
1

Please Enter Value in stack:
5

Value pushed in stack!

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
1

Please Enter Value in stack:
6

Value pushed in stack!

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
1

Please Enter Value in stack:
7

Value pushed in stack!

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
1

Please Enter Value in stack:
6

Value pushed in stack!

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
3

stack:
[5, 6, 7, 8]

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
2

Item popped from the stack!

1. Push 2. Pop 3. Display 4. Exit
Enter your choice:
4

Bye Bye!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.