0% found this document useful (0 votes)
6 views7 pages

imp ADS questions

Uploaded by

mamata kolhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

imp ADS questions

Uploaded by

mamata kolhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import java.util.

Scanner;

import java.util.Stack;

public class BalancedParentheses {

public boolean isBalanced(String expr) {

Stack<Character> stack = new Stack<>();

for (char c : expr.toCharArray()) {

if (c == '(') {

stack.push(c);

} else if (c == ')') {

if (stack.isEmpty()) return false;

stack.pop();

return stack.isEmpty();

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter an expression to check for balanced parentheses:");

String expression = scanner.nextLine();

BalancedParentheses bp = new BalancedParentheses();

boolean result = bp.isBalanced(expression);

System.out.println("The parentheses are " + (result ? "balanced" : "not balanced"));

}
import java.util.Scanner;

import java.util.Stack;

public class QueueUsingStacks {

Stack<Integer> stack1 = new Stack<>();

Stack<Integer> stack2 = new Stack<>();

public void enqueue(int x) {

stack1.push(x);

public int dequeue() {

if (stack2.isEmpty()) {

while (!stack1.isEmpty()) {

stack2.push(stack1.pop());

return stack2.isEmpty() ? -1 : stack2.pop(); // Return -1 if queue is empty

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

QueueUsingStacks queue = new QueueUsingStacks();

System.out.println("Enter numbers to enqueue (type -1 to stop):");

while (true) {

int value = scanner.nextInt();

if (value == -1) break;

queue.enqueue(value);

}
System.out.println("Dequeueing elements:");

while (true) {

int dequeuedValue = queue.dequeue();

if (dequeuedValue == -1) break;

System.out.print(dequeuedValue + " ");

}
4. Binary Tree: Level Order Traversal with User Input

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

class TreeNode {

int val;

TreeNode left, right;

TreeNode(int val) {

this.val = val;

left = right = null;

public class LevelOrderTraversal {

public void levelOrder(TreeNode root) {

if (root == null) return;

Queue<TreeNode> queue = new LinkedList<>();

queue.add(root);

while (!queue.isEmpty()) {

TreeNode node = queue.poll();

System.out.print(node.val + " ");

if (node.left != null) queue.add(node.left);

if (node.right != null) queue.add(node.right);

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

TreeNode root = null;

System.out.println("Enter node values to create a binary tree (type -1 for no node):");

while (true) {

int value = scanner.nextInt();

if (value == -1) break;

// Code to insert node into the tree goes here

// (You can create a method to insert nodes)

LevelOrderTraversal lot = new LevelOrderTraversal();

System.out.println("Level order traversal of the binary tree:");

lot.levelOrder(root); // Assuming root is constructed properly

}
Hashing: First Non-Repeating Character with User Input

import java.util.HashMap;

import java.util.Scanner;

public class FirstNonRepeatingChar {

public char firstNonRepeating(String str) {

HashMap<Character, Integer> countMap = new HashMap<>();

for (char c : str.toCharArray()) {

countMap.put(c, countMap.getOrDefault(c, 0) + 1);

for (char c : str.toCharArray()) {

if (countMap.get(c) == 1) {

return c;

return '\0'; // Return null character if all are repeating

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string to find the first non-repeating character:");

String input = scanner.nextLine();

FirstNonRepeatingChar fnrc = new FirstNonRepeatingChar();

char result = fnrc.firstNonRepeating(input);

System.out.println("First non-repeating character: " + (result == '\0' ? "None" : result));

You might also like