0% found this document useful (0 votes)
19 views9 pages

DSA Assignment - 3

Uploaded by

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

DSA Assignment - 3

Uploaded by

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

DSA Assignment - 3

Thopireddy Aryan Reddy


23BCE8252
1Q.Write a Program to implement stack opera ons using arrays.
class Stack {

private int maxSize;

private int[] stackArray;

private int top;

public Stack(int size) {

maxSize = size;

stackArray = new int[maxSize];

top = -1;

public void push(int value) {

if (top == maxSize - 1) {

System.out.println("Stack is full. Cannot push "


+ value);

} else {

stackArray[++top] = value;

System.out.println("Pushed " + value);

}
ti
public int pop() {

if (top == -1) {

System.out.println("Stack is empty. Cannot


pop.");

return -1;

} else {

return stackArray[top--];

public int peek() {

if (top == -1) {

System.out.println("Stack is empty.");

return -1;

} else {

return stackArray[top];

public boolean isEmpty() {

return top == -1;

public boolean isFull() {

return top == maxSize - 1;

}
public void display() {

if (top == -1) {

System.out.println("Stack is empty.");

} else {

System.out.println("Stack elements:");

for (int i = top; i >= 0; i--) {

System.out.println(stackArray[i]);

public class StackDemo {

public static void main(String[] args) {

Stack stack = new Stack(5);

stack.push(10);

stack.push(20);

stack.push(30);

stack.display();

System.out.println("Popped: " + stack.pop());

System.out.println("Peek: " + stack.peek());

stack.display();

stack.push(40);

stack.push(50);
stack.push(60);

stack.push(70);

stack.display();

2Q.Write a Program to implement queue


operations using linked list.

class Node {

int data;

Node next;

Node(int data) {

this.data = data;

this.next = null;

class Queue {

private Node front, rear;

public Queue() {

this.front = this.rear = null;

}
public void enqueue(int data) {

Node newNode = new Node(data);

if (rear == null) {

front = rear = newNode;

return;

rear.next = newNode;

rear = newNode;

public int dequeue() {

if (front == null) {

System.out.println("Queue is empty.");

return -1;

int value = front.data;

front = front.next;

if (front == null) {

rear = null;

return value;

public int peek() {

if (front == null) {

System.out.println("Queue is empty.");

return -1;
}

return front.data;

public boolean isEmpty() {

return front == null;

public void display() {

if (front == null) {

System.out.println("Queue is empty.");

return;

Node temp = front;

while (temp != null) {

System.out.print(temp.data + " -> ");

temp = temp.next;

System.out.println("null");

public class QueueDemo {

public static void main(String[] args) {

Queue queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);

queue.enqueue(30);

queue.display();

System.out.println("Dequeued: " + queue.dequeue());

System.out.println("Peek: " + queue.peek());

queue.display();

queue.enqueue(40);

queue.enqueue(50);

queue.display();

3Q.Write a Program for Evaluation given


postfix expression.

import java.util.Stack;

public class PostfixEvaluation {

public static int evaluatePostfix(String expression) {

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

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

char ch = expression.charAt(i);
if (Character.isDigit(ch)) {

stack.push(ch - '0');

} else {

int val1 = stack.pop();

int val2 = stack.pop();

switch (ch) {

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


String expression = "231*+9-";

System.out.println("Postfix Expression: " +


expression);

System.out.println("Evaluation Result: " +


evaluatePostfix(expression));

You might also like