Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- int max = 5;
- int top = -1;
- int[] stack = new int[max];
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- Main main = new Main();
- while (true) {
- System.out.println("1. Push\n2. Pop\n3. Show");
- int choice = input.nextInt();
- if (choice == 1) {
- if (main.top == main.max - 1) {
- System.out.println("Overflow!");
- }
- else {
- System.out.print("Input the value: ");
- int value = input.nextInt();
- main.push(value);
- System.out.println("Done!");
- }
- }
- else if (choice == 2) {
- if (main.top == -1) {
- System.out.println("Underflow!");
- }
- else {
- System.out.printf("Item deleted: %d", main.pop());
- }
- }
- else if (choice == 3) {
- System.out.print("The stack:\t");
- main.printStack();
- }
- else {
- break;
- }
- }
- input.close();
- }
- void printStack() {
- for (int i = 0; i <= top; i++) {
- System.out.printf("%d\t", stack[i]);
- }
- System.out.println();
- }
- int pop() {
- int value = stack[top];
- stack[top] = 0;
- top--;
- return value;
- }
- void push(int value) {
- top++;
- stack[top] = value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement