示例#1
0
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.println("What should be the size of stack? ");
    int size = sc.nextInt();

    Stack s = new Stack(size);

    while (true) {
      System.out.println("1.Push\n2.Pop\n3.Display stack\n4.Exit\nEnter your choice:");
      int choice = sc.nextInt();

      switch (choice) {
        case 1:
          System.out.print("Enter a number: ");
          int item = sc.nextInt();
          s.push(item);
          break;

        case 2:
          try {
            System.out.println("Popped value = " + s.pop());
          } catch (Exception e) {
            System.out.println(e.getMessage());
          }

          break;

        case 3:
          s.displayStack();
          break;

        case 4:
          System.exit(0);
          break;

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