示例#1
0
 public static void main(String[] args) {
   MyStack stack = new MyStack();
   stack.push(5);
   stack.push(6);
   stack.push(9);
   stack.push(3);
   stack.push(8);
   stack.push(1);
   while (!stack.isEmpty()) {
     int min = stack.min();
     int value = stack.pop();
     System.out.println("Pop value: " + value + ", current min: " + min);
   }
 }
示例#2
0
  public static void main(String[] args) throws Exception {
    MyStack stack = new MyStack();

    Scanner input = new Scanner(new File(args[0]));

    try {
      while (input.hasNext()) {
        String s = input.nextLine();

        StringTokenizer tokens = new StringTokenizer(s, "[](){}", true);

        while (tokens.hasMoreTokens()) {
          String token = tokens.nextToken().trim();
          if (token.length() == 0) continue;
          else if (token.charAt(0) == '[') {
            stack.push(']');
          } else if (token.charAt(0) == '{') {
            stack.push('}');
          } else if (token.charAt(0) == '(') {
            stack.push(')');
          } else if (token.charAt(0) == ']' || token.charAt(0) == '}' || token.charAt(0) == ')') {
            char ch = ((Character) (stack.pop())).charValue();
            if (ch != token.charAt(0)) {
              System.out.println("Exit 1: Incorrect grouping pairs");
              System.exit(0);
            }
          }
        }
      }

      if (!stack.isEmpty()) {
        System.out.println("Exit 2: Incorrect grouping pairs");
        System.exit(0);
      }
    } catch (Exception ex) {
      System.out.println("Exit 3: Incorrect grouping pairs");
    }

    System.out.println("Correct grouping pairs");
  }