Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. public class MyStack {
  6.  
  7.     public static void main(String[] args) {
  8.         test(")", false);
  9.         test("(", false);
  10.         test("()", true);
  11.         test("(()", false);
  12.         test("(())", true);
  13.         test("((())", false);
  14.         test("(()()(()(()())()))", true);
  15.         test("(()()(()(()())())))", false);
  16.     }
  17.    
  18.     private static void test(String str, boolean expected) {
  19.         if(expected == isParenthesisMatch(str)) {
  20.             System.out.println(String.format(
  21.                         "OK:\t'%s'", str
  22.                     ));
  23.         } else {
  24.             System.err.println(String.format(
  25.                         "FAIL:\t'%s'", str
  26.                     ));
  27.         }
  28.     }
  29.     private int maxSize;
  30.     private List<String> container;
  31.  
  32.    
  33.  
  34.     public MyStack(int maxSize) {
  35.         this.maxSize = maxSize;
  36.         container = new ArrayList<>();
  37.     }
  38.  
  39.     public MyStack() {
  40.         this(10);
  41.     }
  42.  
  43.     public void push(String n) {
  44.         if (isFull()) {
  45.             System.err.println("stack is full " + n);
  46.         } else {
  47.             container.add(n);
  48.         }
  49.     }
  50.  
  51.     public String pop() {
  52.         if (container.isEmpty()) {
  53.             System.err.println("stack is empty");
  54.             return null;
  55.         } else {
  56.             int index = container.size() - 1;
  57.             String result = container.remove(index);
  58.             return result;
  59.         }
  60.     }
  61.  
  62.     public String peek() {
  63.         if (container.isEmpty()) {
  64.             System.err.println("stack is empty");
  65.             return null;
  66.         } else {
  67.             int index = container.size() - 1;
  68.             return container.get(index);
  69.         }
  70.     }
  71.  
  72.     public boolean isFull() {
  73.         return container.size() == maxSize;
  74.     }
  75.  
  76.     public boolean isEmpty() {
  77.         return container.isEmpty();
  78.     }
  79.  
  80.     public void empty() {
  81.         container.clear();
  82.     }
  83.  
  84.     @Override
  85.     public String toString() {
  86.         StringBuilder sb = new StringBuilder(maxSize);
  87.         sb.append("[ ");
  88.         for (int i = 0; i < container.size(); i++) {
  89.             sb.append(container.get(i)).append(" ");
  90.         }
  91.         sb.append("]");
  92.         return sb.toString();
  93.     }
  94.  
  95.     public boolean isParenthesisMatch(String str) {
  96.         if ("(".equals(str)) {
  97.             this.push(str);
  98.         }
  99.         if (this.isEmpty()) {
  100.             return false;
  101.         }
  102.         if ("(".equals(this.peek())) {
  103.             this.pop();
  104.         }
  105.         return this.isEmpty();;
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement