Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import java.util.*;
  2. import javax.swing.*;
  3.  
  4. public class StackApplication
  5. {
  6.     public static void main (String [] args)
  7.     {
  8.         MyStack ballStack = new MyStack ();
  9.         //Insert number of strings etc etc.
  10.         String str = "(a + b)";
  11.        
  12.         for (int i = 0; i < str.length(); i++) {
  13.             if (str.charAt(i) == '(') {
  14.                 // Push the entire string between index i and beyond onto the stack.
  15.                 //str.substring(nextPush, bastardizedStr.length());
  16.             }
  17.             else if (str.charAt(i) == ')') {
  18.                 // if (there is stuff in the stack)
  19.                 //     Pop from the stack.
  20.                 // else
  21.                 //     return false;
  22.             }
  23.         }
  24.  
  25.     // Check if there are things left over.
  26.     // Return good/bad state.
  27.         // Constructor.
  28.     ballStack.pop(0);
  29.         // If there is nothing in the stack...
  30.         if (my.top)
  31.         {
  32.             // Return null
  33.             return null;
  34.         }
  35.         // Otherwise, store the last thing we pushed in.
  36.         return top.remove(0);
  37.         // Then remove it from the stack.
  38.         // And finally, return the thing we just removed.
  39.         // Java gets a one-liner of the 3 previous commands. See the following:
  40.         // return top.remove(0);
  41.  
  42.      ballStack.push(my, data);
  43.         // Appends one more array element. Nevermind the odd Python syntax.
  44.        
  45.      ballStack.peek(my);
  46.         // If there is nothing in the stack...
  47.         if not len(my.top):
  48.             return null;
  49.         //// Return the last element pushed in.
  50.         return my.top[0]
  51.  
  52.     //def empty():
  53.         //my.top = []
  54.  
  55. {
  56.     // Make a brand new stack for each call to this demonstration method.
  57.     ballStack = MyStack()
  58.  
  59.     // This for loop is the same as for (int i = 0; i < len(originalStr); i++)
  60.     for i in range(len(stackString)):
  61.         if (stackString[i] == '(')
  62.             // This code doesn't matter so much. It takes the '(' and everything
  63.             // beyond it and pushes that into the stack. The Java equivalant is
  64.             stackString.substring(i);
  65.             ballStack.push(stackString[i:])
  66.         else if (stackString[i] == ')':
  67.             // See if there is anything in the stack.
  68.             if (ballStack.peek())
  69.                 // Something's in the stack. Remove it.
  70.                 ballStack.pop();
  71.             else
  72.                 // Found a ')' without a '(' pair.
  73.                 return False;
  74.     // The for loop ended. If there's nothing left in the stack, it's balanced.
  75.     return ballStack.pop();
  76.        
  77.     // The following line is just a demonstration.
  78.     System.out.println ("Input str: " + input)
  79.     balanced = isBalanced(input)
  80.     if (balanced == true)
  81.         System.out.println ( input + " is balanced");
  82.     else
  83.         System.out.println ( input + " is not balanced");
  84.     // Print a newline to separate the next input.
  85.     System.out.println ("");
  86.        
  87.     }
  88. }
  89.         Stack Balance = new Stack();
  90.         char ch;
  91.         String str;
  92.         boolean balanced = true;
  93.              
  94.         while(!Balance().empty())
  95.         {
  96.             ch = input.charAt(index);
  97.            
  98.             if (ch =='(')
  99.                 Balance.push(ch);
  100.                
  101.             else if (ch ==')')
  102.             {
  103.                 if (stack.NotEmpty())
  104.                 {
  105.                     stack.push();
  106.                 }
  107.                 else if (stack.empty())
  108.                 {
  109.                     return false;
  110.                 }
  111.                    
  112.     }
  113.  
  114.     //public static boolean balanced (String str)
  115.     {
  116.         //return
  117.     }
  118. }
  119. }
  120. }
  121.    /***     // Check if the stack is empty.
  122.        
  123.        
  124.         //ball = new ball();
  125.         //ball.setBall();
  126.         //ballStack.push(ball);***/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement