Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import javax.swing.*;
- public class StackApplication
- {
- public static void main (String [] args)
- {
- MyStack ballStack = new MyStack ();
- //Insert number of strings etc etc.
- String str = "(a + b)";
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '(') {
- // Push the entire string between index i and beyond onto the stack.
- //str.substring(nextPush, bastardizedStr.length());
- }
- else if (str.charAt(i) == ')') {
- // if (there is stuff in the stack)
- // Pop from the stack.
- // else
- // return false;
- }
- }
- // Check if there are things left over.
- // Return good/bad state.
- // Constructor.
- ballStack.pop(0);
- // If there is nothing in the stack...
- if (my.top)
- {
- // Return null
- return null;
- }
- // Otherwise, store the last thing we pushed in.
- return top.remove(0);
- // Then remove it from the stack.
- // And finally, return the thing we just removed.
- // Java gets a one-liner of the 3 previous commands. See the following:
- // return top.remove(0);
- ballStack.push(my, data);
- // Appends one more array element. Nevermind the odd Python syntax.
- ballStack.peek(my);
- // If there is nothing in the stack...
- if not len(my.top):
- return null;
- //// Return the last element pushed in.
- return my.top[0]
- //def empty():
- //my.top = []
- {
- // Make a brand new stack for each call to this demonstration method.
- ballStack = MyStack()
- // This for loop is the same as for (int i = 0; i < len(originalStr); i++)
- for i in range(len(stackString)):
- if (stackString[i] == '(')
- // This code doesn't matter so much. It takes the '(' and everything
- // beyond it and pushes that into the stack. The Java equivalant is
- stackString.substring(i);
- ballStack.push(stackString[i:])
- else if (stackString[i] == ')':
- // See if there is anything in the stack.
- if (ballStack.peek())
- // Something's in the stack. Remove it.
- ballStack.pop();
- else
- // Found a ')' without a '(' pair.
- return False;
- // The for loop ended. If there's nothing left in the stack, it's balanced.
- return ballStack.pop();
- // The following line is just a demonstration.
- System.out.println ("Input str: " + input)
- balanced = isBalanced(input)
- if (balanced == true)
- System.out.println ( input + " is balanced");
- else
- System.out.println ( input + " is not balanced");
- // Print a newline to separate the next input.
- System.out.println ("");
- }
- }
- Stack Balance = new Stack();
- char ch;
- String str;
- boolean balanced = true;
- while(!Balance().empty())
- {
- ch = input.charAt(index);
- if (ch =='(')
- Balance.push(ch);
- else if (ch ==')')
- {
- if (stack.NotEmpty())
- {
- stack.push();
- }
- else if (stack.empty())
- {
- return false;
- }
- }
- //public static boolean balanced (String str)
- {
- //return
- }
- }
- }
- }
- /*** // Check if the stack is empty.
- //ball = new ball();
- //ball.setBall();
- //ballStack.push(ball);***/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement