In: Computer Science
JAVA:
Use existing Java Stack class to solve the "Balancing Symbols" problem. The symbols are (), [], and {}, and each opening symbol must have a corresponding closing symbol as well as in correct order. Ignore operands and arithmetic operators since they are not relevant to our problem. You can assume each token is separated by spaces. For example:
{ ( a + b ) * c1 } – valid
{ ( a + b ) * c1 ] – invalid
( ( a + b ) * c1 } / 15 ) – invalid
Outline a solution to handle an expression like {(a+25 )*c1}. Notice that spaces are now optional.
Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below.
Queue myQueue; // Queue myQueue = new Queue(); in
JavamyQueue.enqueue(“CPS 123”);
myQueue.enqueue(“CPS 223”);
myQueue.enqueue(“CPS 323”);
myQueue.dequeue();
myQueue.enqueue(“CPS 113”);
myQueue.enqueue(“CPS 153”);
string course = myQueue.front(); // course should be CPS 223 size =
myQueue.size(); // size should be 4
// output course and size
Please find the answer below, all the details are mentioned in the comments.
BalancingSymbols.java
import java.util.Scanner; import java.util.Stack; public class BalancingSymbols { public static void main(String[] args) { //scanner to take the input Scanner sc = new Scanner(System.in); String input; //get the string from user System.out.print("Enter the expression: "); input = sc.nextLine(); //call the method to check balance parentheses if (checkBalancedSymbols(input)) { System.out.println(input + " - valid"); } else { System.out.println(input + " - invalid"); } } //method to check balance parentheses private static boolean checkBalancedSymbols(String input) { //create a stack to hold the character values Stack<Character> stack = new Stack<Character>(); //traverse through the input string for (int i = 0; i < input.length(); i++) { //get the ith character at a time char c = input.charAt(i); //if it's opening parentheses then push it to the stack if (c == '[' || c == '(' || c == '{') { stack.push(c); } //else if it's closing parentheses then check if stack is not empty and check if the corresponding opening // parentheses exist on the top of the stack if not then return false else continue to check else if (c == ']') { if (stack.isEmpty() || stack.pop() != '[') { return false; } } else if (c == ')') { if (stack.isEmpty() || stack.pop() != '(') { return false; } } else if (c == '}') { if (stack.isEmpty() || stack.pop() != '{') { return false; } } } //in the end stack should be empty to be balanced string so will return the status of the //stack at that time return stack.isEmpty(); } }
Output:
Without space character
Queue Implementation:
//implementation of circular queue class Queue<E> { //current size of the queue private int currentSize; //elements of the queue private E[] elements; //max size of the queue private int maxSize; //rear of the queue private int rear; //front of the queue private int front; //constructor public Queue() { //assign max size this.maxSize = 5; //allocate the memory to hold elements elements = (E[]) new Object[this.maxSize]; //current size to 0 currentSize = 0; //front and rear to -1 front = -1; rear = -1; } /** * Enqueue elements to queue. */ public void enqueue(E item) { if (isFull()) { System.out.println("Circular Queue is full. Element cannot be added"); } else { rear = (rear + 1) % elements.length; elements[rear] = item; currentSize++; if (front == -1) { front = rear; } } } /** * Dequeue element from Front. */ public E dequeue() { E deQueuedElement = null; if (isEmpty()) { System.out.println("Circular Queue is empty. Element cannot be retrieved"); } else { deQueuedElement = elements[front]; elements[front] = null; front = (front + 1) % elements.length; currentSize--; } return deQueuedElement; } /** * Check if queue is full. */ public boolean isFull() { return (currentSize == elements.length); } /** * Check if Queue is empty. */ public boolean isEmpty() { return (currentSize == 0); } /** * return the front element and null if empty * @return */ public E front(){ return elements[front]; } /** * return the size of the queue * @return */ public int size() { return currentSize; } }
QueueTest.java
public class QueueTest { public static void main(String[] args) { Queue<String> myQueue = new Queue(); // Queue myQueue = new Queue(); in Java myQueue.enqueue("CPS 123"); myQueue.enqueue("CPS 223"); myQueue.enqueue("CPS 323"); myQueue.dequeue(); myQueue.enqueue("CPS 113"); myQueue.enqueue("CPS 153"); String course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 System.out.println("Front: " + course); System.out.println("Size: " + myQueue.size()); } }
Output:
Please let us know in the comments if you face any problems.
pere de representare la strada 9-15lban)Java" "C:\Program Files\Java\jdk1.8.0_151\bin\java" .. Enter the expression: { (a + b) * cl } { ( a + b) * cl } - valid
"C:\Program Files\Java\jdk1.8.0_151\bin\java" ... Enter the expression: { (a + b) * cl ] { ( a + b) * cl ] - invalid
"C:\Program Files\Java \jdk1.8.0_151\bin\java" ... Enter the expression: ( a + b) * cl } / 15 ) (( a + b ) * cl } / 15 ) - invalid
"C:\Program Files\Java \jdk1.8.0_151\bin\java" ... Enter the expression: {(a+25 ) *c1} {(a+25 ) *cl} - valid