0% found this document useful (0 votes)
2 views

Fat Lab Programs List

The document provides a series of Java programs that implement various data structures and algorithms, including stack and queue operations using both arrays and linked lists, as well as searching and sorting algorithms like binary search, linear search, quicksort, mergesort, insertion sort, and selection sort. Additionally, it includes programs for evaluating postfix expressions and converting infix expressions to postfix. Each program is presented with its respective class structure and methods for performing the specified operations.

Uploaded by

svsxerox999
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)
2 views

Fat Lab Programs List

The document provides a series of Java programs that implement various data structures and algorithms, including stack and queue operations using both arrays and linked lists, as well as searching and sorting algorithms like binary search, linear search, quicksort, mergesort, insertion sort, and selection sort. Additionally, it includes programs for evaluating postfix expressions and converting infix expressions to postfix. Each program is presented with its respective class structure and methods for performing the specified operations.

Uploaded by

svsxerox999
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/ 12

1. Write a Program to implement stack operations using arrays.

Ans: class StackArray {

int top = -1;

int[] stack = new int[5];

void push(int data) {

if (top == 4) {

System.out.println("Stack Overflow");

return;

stack[++top] = data;

void pop() {

if (top == -1) {

System.out.println("Stack Underflow");

return;

top--;

}
void display() {

for (int i = 0; i <= top; i++)

System.out.print(stack[i] + " ");

System.out.println();

2. Write a Program to implement stack operations using Linked list.

ans class StackLL {

class Node {

int data;

Node next;

Node(int data) { this.data = data; }

Node top;

void push(int data) {

Node node = new Node(data);

node.next = top;

top = node;

}
void pop() {

if (top == null) {

System.out.println("Stack Underflow");

return;

top = top.next;

void display() {

Node temp = top;

while (temp != null) {

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

temp = temp.next;

System.out.println();

3. Write a Program to implement queue operations using arrays

Ans: class QueueArray {

int front = 0, rear = 0;


int[] queue = new int[5];

void enqueue(int data) {

if (rear == 5) {

System.out.println("Queue Overflow");

return;

queue[rear++] = data;

void dequeue() {

if (front == rear) {

System.out.println("Queue Underflow");

return;

front++;

void display() {

for (int i = front; i < rear; i++)

System.out.print(queue[i] + " ");


System.out.println();

4. Write a Program to implement Queue operations using linked list

Ans: class QueueLL {

class Node {

int data;

Node next;

Node(int data) { this.data = data; }

Node front, rear;

void enqueue(int data) {

Node node = new Node(data);

if (rear == null) front = rear = node;

else {

rear.next = node;

rear = node;

}
void dequeue() {

if (front == null) {

System.out.println("Queue Underflow");

return;

front = front.next;

if (front == null) rear = null;

void display() {

Node temp = front;

while (temp != null) {

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

temp = temp.next;

System.out.println();

5. Write a Program to implement Binary Search using recursion

Ans: int binarySearch(int[] arr, int left, int right, int target) {
if (left > right) return -1;

int mid = (left + right) / 2;

if (arr[mid] == target) return mid;

else if (arr[mid] > target)

return binarySearch(arr, left, mid - 1, target);

else

return binarySearch(arr, mid + 1, right, target);

6. Write a Program to implement Linear Search using recursion

Ans: int linearSearch(int[] arr, int index, int target) {

if (index >= arr.length) return -1;

if (arr[index] == target) return index;

return linearSearch(arr, index + 1, target);

7. Write a program to implement quicksort.

Ans: void quickSort(int[] arr, int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}
}

int partition(int[] arr, int low, int high) {

int pivot = arr[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (arr[j] <= pivot) {

i++;

int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp;

return i + 1;

8. Write a program to implement Merge sort.

Ans: void mergeSort(int[] arr, int l, int r) {

if (l < r) {

int m = (l + r) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}

void merge(int[] arr, int l, int m, int r) {

int[] left = Arrays.copyOfRange(arr, l, m + 1);

int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);

int i = 0, j = 0, k = l;

while (i < left.length && j < right.length)

arr[k++] = left[i] <= right[j] ? left[i++] : right[j++];

while (i < left.length) arr[k++] = left[i++];

while (j < right.length) arr[k++] = right[j++];

9. Write a program to implement Insertion sort.

Ans: void insertionSort(int[] arr) {

for (int i = 1; i < arr.length; i++) {

int key = arr[i], j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;
}

10. Write a program to implement Selection sort.

Ans: void selectionSort(int[] arr) {

for (int i = 0; i < arr.length - 1; i++) {

int minIdx = i;

for (int j = i + 1; j < arr.length; j++)

if (arr[j] < arr[minIdx])

minIdx = j;

int temp = arr[minIdx]; arr[minIdx] = arr[i]; arr[i] = temp;

11. Write a Program for the Evaluation of a given postfix expression

Ans: int evaluatePostfix(String expr) {

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

for (char c : expr.toCharArray()) {

if (Character.isDigit(c)) stack.push(c - '0');

else {

int b = stack.pop();

int a = stack.pop();

switch (c) {
case '+': stack.push(a + b); break;

case '-': stack.push(a - b); break;

case '*': stack.push(a * b); break;

case '/': stack.push(a / b); break;

return stack.pop();

12. Write a program to implement the Infix to Postfix expression

Ans: String infixToPostfix(String expr) {

StringBuilder result = new StringBuilder();

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

Map<Character, Integer> precedence = Map.of('+', 1, '-', 1, '*', 2, '/', 2, '^', 3);

for (char c : expr.toCharArray()) {

if (Character.isLetterOrDigit(c)) result.append(c);

else if (c == '(') stack.push(c);

else if (c == ')') {

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

result.append(stack.pop());
stack.pop();

} else {

while (!stack.isEmpty() && precedence.getOrDefault(c, 0) <=

precedence.getOrDefault(stack.peek(), 0))

result.append(stack.pop());

stack.push(c);

while (!stack.isEmpty()) result.append(stack.pop());

return result.toString();

You might also like