Showing posts with label stacks. Show all posts
Showing posts with label stacks. Show all posts

Reverse arrays using Stacks in java

Problem:

Create a java program that reverses an array in java using stacks.

Output:

90 78 40 9 6 3 1
10.5 6.9 3.2 2.5

Solution:

import java.util.Stack;

public class ReverseArrayApp {

 public static void main(String[] args){
  Integer[] arr1 = {1, 3, 6, 9, 40, 78, 90};
  Double[] arr2 = {2.5, 3.2, 6.9, 10.5};
  
 
  Number[] arr1_rev = reverse(arr1);
  Number[] arr2_rev = reverse(arr2);
  
  
  for(Number eltInt : arr1_rev) 
   System.out.print(eltInt + " ");
  System.out.println();
  for(Number eltDouble : arr2_rev)
   System.out.print(eltDouble + " ");
  }

 
 private static Number[] reverse(Number[] arr) {
  Stack<Number> temp = new Stack<>();
  Number [] output = new Number[arr.length];
  
  for(Number elt: arr) 
   temp.push(elt);
  
  int index = 0;
  
  while(!temp.empty()){
   output[index] = temp.pop();
   index++;
  }
  
  return output;
  
 }
 
 
 }
 
Read More

Check if a character is balanced using Stacks in Java

Problem:

Write a java method that checks if a character is balanced using stacks.

Output:

Not applicable.

Solution:

import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;

public class ParanChecker
{
  public static void main(String[] args)
  {
    //We assume that the Character is balanced from the beginning
    boolean balanced = true;
    
    String OPEN = "([{";
    String CLOSE = ")]}";
    
    Scanner scan = new Scanner(System.in);
    Stack <Character> s = new Stack<Character>();
    
    System.out.println("Enter your expression:");
    String expression = scan.nextLine();
    
    try 
    {
      int index = 0;
      
      while (balanced && index < expression.length())
      {
        char nextCh = expression.charAt(index);
        if ( OPEN.indexOf(nextCh) != -1 )
          s.push(nextCh);
        else if ( CLOSE.indexOf(nextCh) != -1)
        {
          char topCh = s.pop();
          balanced = (OPEN.indexOf(topCh) == CLOSE.indexOf(nextCh));
        }
      index++;    
      }
    }
    
    catch(EmptyStackException emptyStackException)
    {
      balanced = false;
    }
    
    if ( balanced && s.empty())
      System.out.println("The Character is balanced");
    else
      System.out.println("The Character is not balanced");
  }
}
Read More

Convert from Decimal to Binary using Stacks in Java

Problem:

To convert a number from decimal to binary, you simply divide by two until a quotient of zero is reached, then use the successive remainders in reverse order as the binary representation. Use a stack to store the remainder of the division and finally print the binary representation. The first line of input n is the number test cases. Each test case begins with an integer t representing the number of integers in the list, then t integers follow.

Output:

3
5
7
10

101
111
1010

Solution:

import java.util.Scanner;
import java.util.Stack;

public class ProblemD {

 public static void main(String[] args) {
  
  Stack<Integer> stack = new Stack<Integer>();
  
  Scanner scan = new Scanner(System.in);
  
  int numberOfExpressions = scan.nextInt();
  int[] num = new int[numberOfExpressions];
  
  for (int i = 0;i<numberOfExpressions;i++)
  {
   num[i] = scan.nextInt();
  }
  
  for (int i = 0;i<numberOfExpressions;i++)
  {
   if (num[i] == 0) {
                System.out.println("0000");
   } else if (num[i] == 1) {
                System.out.println("0001");
   } else {
                while (num[i] != 0) {
                        int a = num[i] / 2;
                        int b = num[i] % 2;
                        stack.push(b);
                        num[i] = a;
                }
        }
   
   while (!stack.empty()) {
                System.out.print(stack.pop());
        }

   System.out.println(" ");
  }

 }
}
Read More

Calculate Postfix Expressions using Queues in Java

Problem:

You are given a list of postfix expressions. Calculate the result of the expressions using a stack. The first line of input n is the number of test cases followed by n expressions. Each expression begins with an integer t representing the number of characters in the expression, and then t characters follow which are either a number or an operand.

Output:

3
3 2 3 *
5 5 4 2 + -
9 5 6 2 + * 12 4 / -

6
1
37

Solution:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class ProblemC {

 public static void main(String[] args) {
  
  Queue<Integer> q = new LinkedList<Integer>();
  String output = "";
  Scanner scan = new Scanner(System.in);
  
  int testCases = scan.nextInt();
  
  for (int cases = 0; cases < testCases; cases++) {
   
   int t = scan.nextInt();
   
   for (int i = 0; i < t; i++)
    q.offer(scan.nextInt());
   
   output +=( "Test case "+(cases+1)+":" + "\n");
   
   while (!q.isEmpty()) {
    if (q.peek()%2 == 0)
     output += (q.poll() + "\n");
    else {
     output+= q.poll()+" ";
     output += q.poll() + "\n";
    }
   }
  }
  
  System.out.println(output);
 }
}
Read More

Check if a String is a Palindrome using Stacks in Java

Problem:

You are given a list of Strings as input. Check whether each string is a palindrome or not using a stack data structure. The first line of input n is the number of test cases followed by n strings.

Output:

3
test
baab
uigui

No
Yes
Yes

Solution:

import java.util.Scanner;
import java.util.Stack;

public class ProblemA {

 public static void main(String[] args) {
  
  Stack<Character> stack = new Stack<Character>();
  
  Scanner scan = new Scanner(System.in);
  
  int numberOfExpressions = scan.nextInt();
  String[] expression = new String[numberOfExpressions];
  
  for (int i = 0;i<numberOfExpressions;i++)
  {
   expression[i] = scan.next();
  }
  
  for (int i = 0;i<numberOfExpressions;i++)
  {      
   String checker = "";
   
   for (int j = 0; j < expression[i].length(); j++)
    stack.push(expression[i].charAt(j));
   
   while (!stack.isEmpty()) 
    checker += stack.pop();
   
   if (checker.equals(expression[i])) 
    System.out.println("Yes");
   else
    System.out.println("No");
  
  }
 }
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact