0% found this document useful (0 votes)
20 views11 pages

Lab Sheet 2 - 3

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)
20 views11 pages

Lab Sheet 2 - 3

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/ 11

LABSHEET 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;

public class Stack_Operations {


public static void main(String[] args) {
int operation=0;
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
while(operation != 5)
{
System.out.println("operation");
System.out.println("1.Push 2.Pop 3.Show 4.Peep,5.Exit");
System.out.println("Enter");
operation = sc.nextInt();
switch(operation)
{
case 1:
{
s.push(sc);
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
case 4:
{
s.peep();
break;
}
case 5:
{
System.out.println("Exiting");
System.exit(0);
break;
}
default:
{
System.out.println("Please Enter valid choice ");
}
};
}
}
}

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 ArrayStack(int capacity) {


this.capacity = capacity;
stack = new int[capacity];
top = -1;
}
public void push(int element) {
if (isFull()) {
System.out.println("Overflow");
return;
}
stack[++top] = element;
}

public int pop() {


if (isEmpty()) {
System.out.println("Underflow");
return -1;
}
return stack[top--];
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == capacity - 1;
}

public int peek() {


if (isEmpty()) {
System.out.println("empty");
return -1;
}
return stack[top];
}

public int size() {


return top + 1;
}
}

public class Q4 {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(10);
stack.push(1);
stack.push(2);
stack.push(3);

ArrayStack tempstack = new ArrayStack(10);

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 Stack(int size) {


stackArray = new int[size];
capacity = size;
top = -1;
}

public void push(int item) {


if (top == capacity - 1) {
System.out.println("Overflow");
return;
}
stackArray[++top] = item;
}

public int pop() {


if (isEmpty()) {
System.out.println("Underflow");
return -1;
}
return stackArray[top--];
}

public boolean isEmpty() {


return top == -1;
}

public int size() {


return top + 1;
}

public String toString() {


if (isEmpty()) {
return "[]";
}
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i <= top; i++) {
sb.append(stackArray[i]);
if (i != top) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}

public void deleteMiddle() {


if (isEmpty()) {
return;
}
int middleIndex = size() / 2;
del_middle(middleIndex);
}

private void del_middle(int middleIndex) {


if (middleIndex == 0) {
pop();
return;
}
int topElement = pop();
del_middle(middleIndex - 1);
push(topElement);
}
}

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);

System.out.println(" original" + stack);


stack.deleteMiddle();
System.out.println("del middle element " + stack);
}
}

Q2)

import java.util.Scanner;

class Q2 {
private int[] stack;
private int top;
private int capacity;

public Q2(int capacity) {


this.capacity = capacity;
stack = new int[capacity];
top = -1;
}

public void push(int element) {


if (isFull()) {
System.out.println("Overflow");
}
stack[++top] = element;
}

public int pop() {


if (isEmpty()) {
System.out.println("Underflow");
}
return stack[top--];
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == capacity - 1;
}
}

public class PostFixEvaluator {


static int evaluatePostfix(String exp) {
Q2 stack = new Q2(exp.length());

for (int i = 0; i < exp.length(); i++) {


char c = exp.charAt(i);

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();
}

public static void main(String[] args) {


Scanner inp = new Scanner(System.in);
System.out.print("Enter the postfix equation ");
String out = inp.next();
System.out.println("Postfix evaluation " + evaluatePostfix(out));
inp.close();
}
}

You might also like