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

DS Labcycle 1-3 Programs

Data science lab

Uploaded by

vanukurusivasai
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)
23 views9 pages

DS Labcycle 1-3 Programs

Data science lab

Uploaded by

vanukurusivasai
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

Data Structures Lab

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;

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 Overflow! Cannot push element " + value);
return;
}
stackArray[++top] = value;
System.out.println(value + " pushed into the stack.");
}

public void pop()


{
if (top == -1)
{
System.out.println("Stack Underflow! Cannot pop from an empty stack.");
return;
}
intpoppedValue = stackArray[top--];
System.out.println("Popped element: " + poppedValue);
}

public void display()


{
if (top == -1)
{
System.out.println("Stack is empty.");
return;
}
Ch. Anil Kumar 1
Data Structures Lab

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


for (int i = 0; i <= top; i++)
{
System.out.print(stackArray[i] + " ");
}
System.out.println();
}
}

public class Main


{
public static void main(String[] args)
{
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
stack.display();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.display();
}
}

Ch. Anil Kumar 2


Data Structures Lab

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.

Ch. Anil Kumar 3


Data Structures Lab

2. Write a Java Program to create a class called Queue and implement Queue Operations

public class Queue


{
private int[] queueArray;
private int front;
private int rear;
private int size;
private int capacity;

public Queue(int capacity)


{
this.capacity = capacity;
queueArray = new int[capacity];
front = 0;
rear = -1;
size = 0;
}

public void enqueue(int value)


{
if (isFull())
{
System.out.println("Queue is full. Cannot enqueue.");
return;
}
rear = (rear + 1) % capacity;
queueArray[rear] = value;
size++;
System.out.println(value + " enqueued into the queue.");
}

public void dequeue()


{
if (isEmpty())
{
System.out.println("Queue is empty. Cannot dequeue.");
return;
}
intdequeuedValue = queueArray[front];
front = (front + 1) % capacity;
size--;
System.out.println("Dequeued element: " + dequeuedValue);
}
Ch. Anil Kumar 4
Data Structures Lab

public booleanisEmpty()
{
return size == 0;
}

public cbooleanisFull()
{
return size == capacity;
}

public void display()


{
if (isEmpty())
{
System.out.println("Queue is empty.");
return;
}
System.out.print("Queue elements: ");
int count = size;
int index = front;
while (count > 0)
{
System.out.print(queueArray[index] + " ");
index = (index + 1) % capacity;
count--;
}
System.out.println();
}

public static void main(String[] args)


{
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
queue.dequeue();
queue.dequeue();
queue.display();
queue.dequeue();
}
}

Ch. Anil Kumar 5


Data Structures Lab

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.

Ch. Anil Kumar 6


Data Structures Lab

3. Write a Java Program to convert the Infix to Postfix Expression

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

public static String infixToPostfix(String infixExpression)


{
StringBuilder postfix = new StringBuilder();
Stack<Character> stack = new Stack<>();
for (char ch : infixExpression.toCharArray())
{
if (Character.isLetterOrDigit(ch))
{
postfix.append(ch);
}

else if (ch == '(')


{
stack.push(ch);
}

else if (ch == ')')


{
while (!stack.isEmpty() &&stack.peek() != '(')
{
postfix.append(stack.pop());
}
Ch. Anil Kumar 7
Data Structures Lab

if (!stack.isEmpty() &&stack.peek() != '(')


{
return "Invalid Expression";
}

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

public static void main(String[] args)


{
String infixExpression = "a+b*(c^d-e)^(f+g*h)-i";
System.out.println("Infix Expression: " + infixExpression);
String postfixExpression = infixToPostfix(infixExpression);
System.out.println("Postfix Expression: " + postfixExpression);
}
}

Ch. Anil Kumar 8


Data Structures Lab

Output:
abcd^e-fgh*+^*+i-

Ch. Anil Kumar 9

You might also like