public static void main(final String[] args) { final Scanner stdin = new Scanner(System.in); final MyStack<Integer> stack = new MyStack<Integer>(Integer.parseInt(args[0])); while (stdin.hasNext()) { final String command = stdin.next(); try { if (command.equals("push")) { stack.push(Integer.parseInt(stdin.next())); } else if (command.equals("pop")) { stack.pop(); } else { continue; } } catch (final BufferFullException e) { System.out.println("Stack is full"); } catch (final BufferEmptyException e) { System.out.println("Stack is empty"); } } stdin.close(); }
private static void testMyStack() { MyStack<Integer> testStack = new MyStack<>(); testStack.push(1); testStack.push(2); testStack.push(3); System.out.println(testStack); }
@Test public void testMyStack() { // Test expected results of creating a new MyStack object MyStack<Integer> testStack = new MyStack<Integer>(); assertTrue(testStack.size() == 0); assertTrue(testStack.isEmpty()); }
@Test public void canPushAndPeekTag() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("b"); testStack.push(testTag); assertEquals(testStack.peek().getElement(), testTag.getElement()); }
@Test public void testIsEmpty() { MyStack<Integer> test = new MyStack<Integer>(); test.push(2); test.push(230); test.pop(); test.pop(); assertTrue(test.isEmpty()); }
@Test public void pushShouldAddToEmptyStack() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("html"); assertTrue(testStack.isEmpty()); testStack.push(testTag); assertFalse(testStack.isEmpty()); }
@Test public void canPushAndPopTag() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("html"); testStack.push(testTag); assertEquals(testStack.pop().getElement(), testTag.getElement()); assertTrue(testStack.isEmpty()); }
@Test public void testPeek() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(23); assertTrue(testStack.peek() == 23); // Ensure the next pushed item is on top testStack.push(2); assertTrue(testStack.peek() == 2); }
@Test public void testPush() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(23); testStack.push(43); testStack.push(21); // Ensure the correct size after doing a number of pushes assertFalse(testStack.isEmpty()); assertTrue(testStack.size() == 3); }
// creates in order iterator public InIter() { // if the BST has nodes, push the root to iterator first if (!isEmpty()) { iterStack.push(root); BSTNode current = root; // traverse the tree to leftmost element while (current.left != null) { iterStack.push(current.left); current = current.left; } } }
/* * uses iterator to output what the next value is, T output */ public T next() { if (!hasNext()) { throw new NoSuchElementException(); } BSTNode current = iterStack.pop(); if (current.right != null) { iterStack.push(current.right); // i think its missing something here } return current.element; }
public static void moveUsingStack(int n) { MyStack<Integer> p1 = new MyStack<Integer>(); MyStack<Integer> p2 = new MyStack<Integer>(); MyStack<Integer> p3 = new MyStack<Integer>(); for (int i = n; i > 0; i--) { p1.push(i); } MyStack[] stacks = new MyStack[] {p1, p2, p3}; moveUsingStack(3, stacks, 0, 2, 1); int noop = 0; }
// if iterStack is empty, we are at the end public boolean hasNext() { if (iterStack.isEmpty()) { return false; } else { return true; } }
for(int i = 0; i < 100; i++){ String s = "" + i; s.push(s); myS.push(s); q.enqueue(s); myQ.enqueue(s); }
/* * uses iterator to output what the next value is, T output */ public T next() { if (!hasNext()) { throw new NoSuchElementException(); } BSTNode current = iterStack.pop(); if (current.right != null) { iterStack.push(current.right); } if (current.left != null) { iterStack.push(current.left); } return current.element; }
public void excuteDFS(int startVertex) { markAddPush(startVertex); // put start point into stack mark while (!myStack.isEmpty()) { int nextPoint = getadjVertex(myStack.peek()); if (nextPoint == -1) { // if can't find adjVertex, pop up myStack.pop(); } else { // if can find adjVertex, push mark and display markAddPush(nextPoint); } } for (int i = 0; i < currentVertexNum; i++) { vertexList[i].wasVisited = false; } }
public static void main(String[] args) { MyStack myStack = new MyStack(); myStack.push(1); myStack.push(2); myStack.push(3); myStack.push(4); myStack.push(5); myStack.push(6); myStack.print(); myStack.pop(); myStack.push(100); myStack.print(); }
@Test public void testClear() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(23); testStack.push(2039); testStack.push(220); assertTrue(testStack.size() == 3); // Ensure quality of the clear method. testStack.clear(); assertTrue(testStack.size() == 0); assertTrue(testStack.isEmpty()); }
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); } }
@Override public void run() { for (int i = 0; i < 50; ) { try { char c = stack.pop(); // System.out.println("popped " + c); i++; try { Thread.sleep(random.nextInt(500) + 400); } catch (InterruptedException ex) { ex.printStackTrace(); } } catch (IllegalStateException ex) { // System.out.println(ex.getMessage()); } } }
@Test public void testPop() { MyStack<String> testStack = new MyStack<String>(); testStack.push("hello"); testStack.push("dog"); testStack.push("cat"); assertEquals("cat", testStack.pop()); assertTrue(testStack.size() == 2); // Ensure the popped object is gone assertEquals("dog", testStack.peek()); }
/** * This method Solved the equivalent expression (all operators appears in the descending order) of * the provided input Mathematical expression * * @param operator * @param digits * @return */ public static float simple_Expression_Solver(MyStack<String> operator, MyStack<Float> digits) { float variable1, variable2; // once the entire input character string is pushed into the stack we // perform operations from higher precedence order to lower precedence // order. it runs until the operator stack is empty. while (!operator.empty()) { if (digits.size() == 1) { // System.out.println(digits.pop()); } if (digits.size() >= 2) { variable1 = digits.pop(); variable2 = digits.pop(); digits.push(PerformOperation(operator.pop(), variable2, variable1)); } } return digits.pop(); }
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"); }
public static void main(String[] args) { MyStack<String> stack = new MyStack<String>(); // The stack Scanner scan = new Scanner(System.in); // Input scanner System.out.println("Choose one of the following operations"); System.out.println(" - push/add (enter the letter a)"); System.out.println(" - pop/delete (enter the letter d)"); System.out.println(" - peek (enter the letter p)"); System.out.println(" - check if the list is empty (enter the letter e)"); System.out.println(" - quit (enter the letter q)"); String tempString; // Temporary storage string char choice; // User's menu choice choice = 'g'; // Initialization while (choice != 'q') { System.out.print("Enter a choice: "); choice = scan.nextLine().charAt(0); switch (choice) { case 'a': System.out.print("Enter an input value: "); if (scan.hasNext()) { tempString = scan.nextLine(); stack.push(tempString); System.out.println("" + tempString + " pushed in"); } else { System.out.println("Invalid value entered"); scan.nextLine(); } break; case 'd': try { tempString = stack.pop(); System.out.println("" + tempString + " popped out"); } catch (MyStack.MyException e) { System.out.println("Invalid Operation: the stack is empty"); } break; case 'e': if (stack.isEmpty()) { System.out.println("empty"); } else { System.out.println("not empty"); } break; case 'p': try { tempString = stack.peek(); System.out.println("" + tempString + " on the top"); } catch (MyStack.MyException e) { System.out.println("Invalid Operation: the stack is empty"); } break; case 'q': break; default: System.out.println("Invalid choice"); } } System.out.println("quitting "); while (!stack.isEmpty()) { System.out.print("" + stack.pop() + " "); } }
public static boolean isMatching(String s) { MyStack<Character> parens = new MyStack<Character>(); if (s.charAt(0) == ')' || s.charAt(0) == '}' || s.charAt(0) == ']' || s.charAt(0) == '>') { return false; } for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '<') { parens.push(s.charAt(i)); } if (s.charAt(i) == ')' && parens.peek() == '(') { parens.pop(); } if (s.charAt(i) == '}' && parens.peek() == '{') { parens.pop(); } if (s.charAt(i) == ']' && parens.peek() == '[') { parens.pop(); } if (s.charAt(i) == '>' && parens.peek() == '<') { parens.pop(); } } return parens.isEmpty(); }
public void markAddPush(int v) { vertexList[v].wasVisited = true; myStack.push(v); displayVertex(v); }
/** * This method convert Mathematical expression into its equivalent form where all the operators * appear in the descending order of precedence. For solving sub expressions of mathematical * expressions within parentheses, this method calls recursively. * * @param calcString mathematical expression from User * @return Output of the provided Mathematical expression */ public static float calculator_controller(String[] calcString) { float variable1, variable2, sFlag = 0; float temp; int precedenceNumber; boolean lastWasDigit = false, lastWasOpe = true; String optNew, tempOpt; // new String array of length 6; String[] precedence = {"+", "-", "%", "/", "*", "^"}; // for a valid input, last element should be a numeric value. if (!calcString[calcString.length - 1].matches("(\\+)?(\\-)?\\d+(\\.\\d+)?") && !calcString[calcString.length - 1].equals(" ") && !calcString[calcString.length - 1].equals(")")) { error = "Invalid Expression as last element is not digit " + "or closing parenthsis"; return 0; } // creating a stack to store integers in float. MyStack<Float> digits = new MyStack<Float>(calcString.length); // creating a stack to store all the operators. MyStack<String> operator = new MyStack<String>(calcString.length); // using stack data structure to implement calculator. for (; indexCounter <= calcString.length - 1; indexCounter++) { // checks for the digits in the string. Once found, extracts the // numeric values from the input expression and pushes it into the // stack. if (calcString[indexCounter].matches("(\\+)?(\\-)?\\d+(\\.\\d+)?")) { if ((lastWasOpe == true && lastWasDigit == false)) { digits.push(Float.parseFloat(calcString[indexCounter])); lastWasDigit = true; lastWasOpe = false; } else { error = "Invalid Expression as it starts from an operator"; // System.out.println("Invalid Expression as it starts from // an operator"); // System.exit(0); return 0; } } // to extract valid operators for non numerical values using // isValidOperator function. else if (isValidOperator( calcString[indexCounter], digits.size(), lastWasDigit, lastWasOpe)) // 119 // if // condition { lastWasDigit = false; lastWasOpe = true; if (calcString[indexCounter].equals(")")) { // termination condition for recursive calls parenthesesCounter--; temp = simple_Expression_Solver(operator, digits); return temp; } else if (calcString[indexCounter].equals("(")) { // recursive call for solving sub expressions // problems indexCounter++; parenthesesCounter++; digits.push(calculator_controller(calcString)); lastWasDigit = true; lastWasOpe = false; continue; } // optNew holds the new operator that is encountered while // reading the user input string. optNew = calcString[indexCounter]; if (operator.size() > 0) { // tempOpt holds the previous // operator which is popped from the top of the // stack to compare with the current // operator. tempOpt = operator.pop(); // when the precedence of previous operator is // lower than the precedence of the present // operator, then operators are pushed back into the // stack. precedenceNumber = getPrecedence(precedence, optNew, tempOpt); if (precedenceNumber == 1) { operator.push(tempOpt); operator.push(optNew); } // if the precedence of current operator is // lower than the precedence of previous // operator, then the operation will be // performed with the higher precedence operator // and the result is pushed into the float // stack. else { // runs until the above condition is // satisfied. while (getPrecedence(precedence, optNew, tempOpt) == 2) { sFlag = 0; variable1 = digits.pop(); variable2 = digits.pop(); temp = PerformOperation(tempOpt, variable2, variable1); digits.push(temp); if (!operator.empty()) { tempOpt = operator.pop(); sFlag = 1; } else { // if the operator stack is empty, // then breaks the while loop. break; } } // to push the current and previous // operators back into the stack. if (sFlag == 1) { operator.push(tempOpt); } operator.push(optNew); } } else { operator.push(optNew); } } else { error = "Invalid input as sytex validations has failed"; } } // for terminator return simple_Expression_Solver(operator, digits); }
@Test public void canCreateEmptyMyStack() { MyStack testStack = new MyStack(); assertTrue(testStack.isEmpty()); }
// Testing Stack @Test public void testStack() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(5); testStack.push(2); testStack.push(3); assertEquals(3, testStack.size()); assertEquals(3, testStack.peek(), 0); testStack.pop(); assertEquals(2, testStack.size()); assertEquals(2, testStack.peek(), 0); testStack.pop(); assertEquals(1, testStack.size()); assertEquals(5, testStack.peek(), 0); testStack.pop(); assertEquals(0, testStack.size()); }
// creates preorder iterator public PreIter() { // if the BST has nodes, push the root to iterator if (!isEmpty()) { iterStack.push(root); } }