Dsa Lab File
Dsa Lab File
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class SinglyLinkedList {
Node head;
if (head.data == data) {
head = head.next; return;
}
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
class Node {
int data;
Node next, prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
Node head;
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.*;
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);
}
}
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 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];
}
}
Output:
5
20
10
class DequeLinkedList {
class Node {
int data;
Node prev, next;
Node(int data) { this.data = data; }
}
DequeLinkedList() {
front = rear = null;
}
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;
}
}
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;
}
}
}
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);
}
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;
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;
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;
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;
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;
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