Lab 1
Write a program that uses functions to perform the following
1. Create A Singly List Of Integers
2. Delete The Given Integer From The Above Linked List
3. Display The Content Of The Ever List After Deletion
public class Lab1 {
public static void main(String args[]) {
SinglyLinkedList lst = new SinglyLinkedList();
lst.insert(10);
lst.insert(20);
lst.insert(30);
lst.insert(40);
lst.display();
lst.delete(20);
lst.display();
}
}
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class SinglyLinkedList {
Node head;
void insert(int data) {
Node n = new Node(data);
if (head == null) head = n;
else {
Node p = head;
while (p.next != null) p = p.next;
p.next = n;
}
}
void delete(int data) {
if (head == null) return;
if (head.data == data) {
head = head.next; return;
}
Node cur = head, prev = null;
while (cur != null && cur.data != data) {
prev = cur;
cur = cur.next;
}
if (cur != null) prev.next = cur.next;
}
void display() {
Node p = head;
while (p != null) {
System.out.print(p.data + " -> ");
p = p.next;
}
System.out.println("null");
}
}
Output:
10 -> 20 -> 30 -> 40 -> null
10 -> 30 -> 40 -> null
Lab 2
Write Program That Uses Functions To Perform The Following
1. Create A Doubly-Linked List Of Integers
2. Delete A Given Integer From The Above Doubly Linked List
3. Display The Content Of The Above List After Deletion
public class Lab2 {
public static void main(String args[]) {
DoublyLinkedList lst = new DoublyLinkedList();
lst.insert(10);
lst.insert(20);
lst.display();
lst.delete(20);
lst.display();
}
}
class Node {
int data;
Node next, prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
Node head;
void insert(int data) {
Node n = new Node(data);
if (head == null) head = n;
else {
Node p = head;
while (p.next != null) p = p.next;
p.next = n;
n.prev = p;
}
}
void delete(int data) {
if (head == null) return;
Node cur = head;
if (cur != null && cur.data == data) {
head = cur.next;
if (head != null) head.prev = null;
return;
}
while (cur != null && cur.data != data) cur = cur.next;
if (cur.next != null) cur.next.prev = cur.prev;
if (cur.prev != null) cur.prev.next = cur.next;
}
void display() {
Node p = head;
while (p != null) {
System.out.print(p.data + " <-> ");
p = p.next;
}
System.out.println("null");
}
}
Output:
10 <-> 20 <-> null
10 <-> null
Lab 3
Write Program That Uses Functions To Perform The Following
import java.util.*;
public class Lab3 {
static int top = -1;
static char[] stack = new char[100];
static boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}
static int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}
static void push(char c) {
stack[++top] = c;
}
static char pop() {
return stack[top--];
}
static String infixToPostfix(String expr) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < expr.length(); i++) {
char c = expr.charAt(i);
if (Character.isLetterOrDigit(c)) result.append(c);
else if (c == '(') push(c);
else if (c == ')') {
while (top != -1 && stack[top] != '(') result.append(pop());
pop();
} else if (isOperator(c)) {
while (top != -1 && precedence(stack[top]) >= precedence(c)) result.append(pop());
push(c);
}
}
while (top != -1) result.append(pop());
return result.toString();
}
public static void main(String[] args) {
String expr = "A+B*C+(D*F)-G*H";
System.out.println("Infix: " + expr + "\nPostfix: " + infixToPostfix(expr));
}
}
Output:
Infix: A+B*C+(D*F)-G*H
Postfix: ABC*+DF*+GH*-
Lab 4
Write A Program To Implement A Double Ended Queue Using Array A Doubly Linked
List Respectively
class DequeArray {
int[] arr;
int front, rear, size, capacity;
DequeArray(int capacity) {
this.capacity = capacity;
arr = new int[capacity];
front = -1;
rear = 0;
size = 0;
}
void insertFront(int key) {
if (size == capacity) return;
front = (front - 1 + capacity) % capacity;
arr[front] = key;
size++;
}
void insertRear(int key) {
if (size == capacity) return;
arr[rear] = key;
rear = (rear + 1) % capacity;
size++;
}
void deleteFront() {
if (size == 0) return;
front = (front + 1) % capacity;
size--;
}
void deleteRear() {
if (size == 0) return;
rear = (rear - 1 + capacity) % capacity;
size--;
}
int getFront() {
return size == 0 ? -1 : arr[front];
}
int getRear() {
return size == 0 ? -1 : arr[(rear - 1 + capacity) % capacity];
}
}
public class Lab4A {
public static void main(String[] args) {
DequeArray dequeArray = new DequeArray(5);
dequeArray.insertFront(10);
dequeArray.insertRear(20);
dequeArray.insertFront(5);
System.out.println(dequeArray.getFront());
System.out.println(dequeArray.getRear());
dequeArray.deleteFront();
System.out.println(dequeArray.getFront());
}
}
Output:
5
20
10
class DequeLinkedList {
class Node {
int data;
Node prev, next;
Node(int data) { this.data = data; }
}
Node front, rear;
DequeLinkedList() {
front = rear = null;
}
void insertFront(int key) {
Node node = new Node(key);
if (front == null) front = rear = node;
else {
node.next = front;
front.prev = node;
front = node;
}
}
void insertRear(int key) {
Node node = new Node(key);
if (rear == null) front = rear = node;
else {
node.prev = rear;
rear.next = node;
rear = node;
}
}
void deleteFront() {
if (front == null) return;
if (front == rear) front = rear = null;
else front = front.next;
if (front != null) front.prev = null;
}
void deleteRear() {
if (rear == null) return;
if (front == rear) front = rear = null;
else rear = rear.prev;
if (rear != null) rear.next = null;
}
int getFront() {
return front == null ? -1 : front.data;
}
int getRear() {
return rear == null ? -1 : rear.data;
}
}
public class Lab4B {
public static void main(String[] args) {
DequeLinkedList dequeLinkedList = new DequeLinkedList();
dequeLinkedList.insertFront(10);
dequeLinkedList.insertRear(20);
dequeLinkedList.insertFront(5);
System.out.println(dequeLinkedList.getFront());
System.out.println(dequeLinkedList.getRear());
dequeLinkedList.deleteFront();
System.out.println(dequeLinkedList.getFront());
}
}
Output:
5
20
10
Lab 5
Write a program for implementing the following sorting method to arrange a list of
integers in ascending order
1. Insertion sorts
2. Merge sort
class InsertionSort {
static void sort(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;
}
}
}
public class Lab5A {
public static void main(String[] args) {
int[] arr1 = {5, 2, 9, 1, 5, 6};
for (int num : arr1) System.out.print(num + " ");
InsertionSort.sort(arr1);
System.out.println();
for (int num : arr1) System.out.print(num + " ");
}
}
class MergeSort {
static void sort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left = new int[mid], right = new int[arr.length - mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
sort(left);
sort(right);
merge(arr, left, right);
}
static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) arr[k++] = left[i++];
else arr[k++] = right[j++];
}
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
}
public class Lab5B {
public static void main(String[] args) {
int[] arr2 = {5, 2, 9, 1, 5, 6};
for (int num : arr2) System.out.print(num + " ");
System.out.println();
MergeSort.sort(arr2);
for (int num : arr2) System.out.print(num + " ");
}
}
Output:
5 2 9 1 5 6
1 2 5 5 6 9
Lab 6
Write A Program To Implement In Certain Sort The Program Should Report The
Number Of Comparisons
class Lab6 {
static int comparisons = 0;
static void sort(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--;
comparisons++;
}
arr[j + 1] = key;
if (j >= 0) comparisons++;
}
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
sort(arr);
for (int num : arr) System.out.print(num + " ");
System.out.println("\nComparisons: " + comparisons);
}
}
Output:
1 2 5 5 6 9
Comparisons: 9
Lab 7
Write Program To Implement Merge Sort The Program Should Report The Number Of
Comparisons
class Lab7 {
static int comparisons = 0;
static void sort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left = new int[mid], right = new int[arr.length - mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
sort(left);
sort(right);
merge(arr, left, right);
}
static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
comparisons++;
if (left[i] <= right[j]) arr[k++] = left[i++];
else arr[k++] = right[j++];
}
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
sort(arr);
for (int num : arr) System.out.print(num + " ");
System.out.println("\nComparisons: " + comparisons);
}
}
Output:
1 2 5 5 6 9
Comparisons: 10
Lab 8
Write a program to implement heap sort the program should report the number of
comparisons
class Lab8 {
static int comparisons = 0;
static void sort(int[] arr) {
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
static void heapify(int[] arr, int n, int i) {
int largest = i, left = 2 * i + 1, right = 2 * i + 2;
if (left < n) {
comparisons++;
if (arr[left] > arr[largest]) largest = left;
}
if (right < n) {
comparisons++;
if (arr[right] > arr[largest]) largest = right;
}
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
sort(arr);
for (int num : arr) System.out.print(num + " ");
System.out.println("\nComparisons: " + comparisons);
}
}
Output:
1 2 5 5 6 9
Comparisons: 14
Lab 9
Write A Program To Implement Randomized Quicksort ( The Program Should Report
The Number Of Comparisons)
class Lab9 {
static int comparisons = 0;
static void sort(int[] arr) {
randomizedQuickSort(arr, 0, arr.length - 1);
}
static void randomizedQuickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
randomizedQuickSort(arr, low, pivotIndex - 1);
randomizedQuickSort(arr, pivotIndex + 1, high);
}
}
static int partition(int[] arr, int low, int high) {
int pivotIndex = low + (int) (Math.random() * (high - low + 1));
swap(arr, pivotIndex, high);
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
comparisons++;
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
sort(arr);
for (int num : arr) System.out.print(num + " ");
System.out.println("\nComparisons: " + comparisons);
}
}
Output:
1 2 5 5 6 9
Comparisons: 11
Lab 10
Write a program for traversal of Binary Search Tree
class Lab10 {
static class Node {
int data;
Node left, right;
Node(int data) { this.data = data; }
}
Node root;
void insert(int data) {
root = insertRec(root, data);
}
Node insertRec(Node root, int data) {
if (root == null) {
root = new Node(data);
return root;
}
if (data < root.data) root.left = insertRec(root.left, data);
else root.right = insertRec(root.right, data);
return root;
}
void inorder() { inorderRec(root); }
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}
void preorder() { preorderRec(root); }
void preorderRec(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preorderRec(root.left);
preorderRec(root.right);
}
}
void postorder() { postorderRec(root); }
void postorderRec(Node root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.data + " ");
}
}
public static void main(String[] args) {
Lab10 tree = new Lab10();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.print("Inorder: "); tree.inorder(); System.out.println();
System.out.print("Preorder: "); tree.preorder(); System.out.println();
System.out.print("Postorder: "); tree.postorder(); System.out.println();
}
}
Output:
Inorder: 20 30 40 50 60 70 80
Preorder: 50 30 20 40 70 60 80
Postorder: 20 40 30 60 80 70 50