DS Labcycle 1-3 Programs
DS Labcycle 1-3 Programs
1. Write a Java Program to create a class called Stack and implement Stack Operations
class Stack
{
private intmaxSize;
private int top;
private int[] stackArray;
Output:
10 pushed into the stack.
20 pushed into the stack.
30 pushed into the stack.
40 pushed into the stack.
50 pushed into the stack.
Stack Overflow! Cannot push element 60
Stack elements: 10 20 30 40 50
Popped element: 50
Popped element: 40
Popped element: 30
Popped element: 20
Popped element: 10
Stack is empty.
2. Write a Java Program to create a class called Queue and implement Queue Operations
public booleanisEmpty()
{
return size == 0;
}
public cbooleanisFull()
{
return size == capacity;
}
Output:
10 enqueued into the queue.
20 enqueued into the queue.
30 enqueued into the queue.
Queue elements: 10 20 30
Dequeued element: 10
Dequeued element: 20
Queue elements: 30
Queue is empty. Cannot dequeue.
import java.util.Stack;
public class InfixToPostfixConverter
{
private static int precedence(char operator)
{
switch (operator)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}
else
{
stack.pop();
}
}
else
{
while (!stack.isEmpty() && precedence(ch) <= precedence(stack.peek()))
{
postfix.append(stack.pop());
}
stack.push(ch);
}
}
while (!stack.isEmpty())
{
if (stack.peek() == '(')
{
return "Invalid Expression";
}
postfix.append(stack.pop());
}
returnpostfix.toString();
}
Output:
abcd^e-fgh*+^*+i-