Lab Sheet 2 - 3
Lab Sheet 2 - 3
Q1)
package Stack;
import java.util.Scanner;
class Stack
{
int top;
int maxsize = 10;
int[] arr = new int[maxsize];
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push (Scanner sc)
{
if(top == maxsize-1)
{
System.out.println("Overflow");
return false;
}
else
{
System.out.println("Enter Value");
int val = sc.nextInt();
top++;
arr[top]=val;
System.out.println("Item pushed");
return true;
}
}
boolean pop ()
{
if (top == -1)
{
System.out.println("Underflow");
return false;
}
else
{
top --;
System.out.println("Item popped");
return true;
}
}
void display ()
{
System.out.println("Printing stack elements");
for(int i = top; i>=0;i--)
{
System.out.println(arr[i]);
}
}
boolean peep(){
if (top == -1)
{
System.out.println("Underflow");
return false;
}
else
{
System.out.println(arr[top]);
return true;
}
}
}
package Stack;
import java.util.Scanner;
Q2)
public class Q2 {
public static void main(String[] args) {
String x = "[(])";
char[] expression = x.toCharArray();
char[] stack = new char[expression.length];
int top = -1;
for (char i : expr) {
if (i == '(' || i == '[' || i == '{') {
top++;
stack[top] = i;
} else if ((i == ')' && stack[top] == '(') || (i == ']' &&
stack[top] == '[') || (i == '}' && stack[top] == '{')) {
top--;
} else {
top++;
stack[top] = i;
}
}
if (top == -1) {
System.out.println("Balanced");
} else {
System.out.println("Not Balanced");
}
}
}
Q3)
public class Q3 {
public static void main(String[] args) {
String X = "hello world";
char[] stack = new char[x.toCharArray().length];
int top=-1;
char[] rev_x = new char[x.toCharArray().length];
for (char i:x.toCharArray()){
top++;
stack[top]=i;
}
for (int i=0;i<x.toCharArray().length;i++){
rev_x[i]=stack[top];
top--;
}
String rev_string = new String(rev_x);
System.out.println(rev_string);
}
}
Q4)
class ArrayStack {
private int[] stack;
private int top;
private int capacity;
public class Q4 {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(10);
stack.push(1);
stack.push(2);
stack.push(3);
while (!stack.isEmpty()) {
tempstack.push(stack.pop());
}
while (!tempstack.isEmpty()) {
int x = tempstack.pop();
System.out.print(x + " ");
stack.push(x);
}
}
}
Q5)
public class Q5 {
public static int top = -1;
public static int ind = -1;
public static char[] stack = new char[10];
public static char[] postfix = new char[20];
public boolean precedence(char a) {
if (top == -1) {
top++;
stack[top] = a;
} else if (a == '(') {
top++;
stack[top] = a;
} else if (a == ')') {
while (stack[top] != '(') {
ind++;
postfix[ind] = stack[top];
top--;
}
top--;
} else if ((a == '^' && stack[top] == '^') || ((a == '*' || a == '/')
&& (stack[top] == '*' || stack[top] == '/')) || ((a == '+' || a == '-') &&
(stack[top] == '*' || stack[top] == '/' || stack[top] == '+' || stack[top] ==
'-'))) {
ind++;
postfix[ind] = stack[top];
top--;
precedence(a);
} else {
top++;
stack[top] = a;
}
return true;
}
public static void main(String[] args) {
String in = "A*(B+C*D)-E";
char[] inp = in.toCharArray();
char[] op = {'+', '-', '*', '/', '^', '(', ')'};
String expression = "";
Main obj = new Main();
for (char i : inp) {
boolean isOperator = false;
for (char j : op) {
if (i == j) {
isOperator = true;
obj.precedence(i);
break;
}
}
if (!isOperator) {
ind++;
postfix[ind] = i;
}
}
while (top != -1) {
ind++;
postfix[ind] = stack[top];
top--;
}
for (char c : postfix) {
if (c != '\u0000') {
exp += c;
}
}
System.out.println(expression);
}
}
Labsheet 3
Q1)
class Stack {
private int[] stackArray;
private int top;
private int capacity;
public class Q1 {
public static void main(String[] args) {
Stack stack = new Stack(10);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
Q2)
import java.util.Scanner;
class Q2 {
private int[] stack;
private int top;
private int capacity;
if (Character.isDigit(c)) {
stack.push(c - '0');
} else {
int val1 = stack.pop();
int val2 = stack.pop();
switch (c) {
case '+':
stack.push(val2 + val1);
break;
case '-':
stack.push(val2 - val1);
break;
case '/':
stack.push(val2 / val1);
break;
case '*':
stack.push(val2 * val1);
break;
}
}
}
return stack.pop();
}