DAA Experiment - Extra Problems
DAA Experiment - Extra Problems
Experiments
Student Name: Anurag Singh UID=22BET10338
Branch: CSE ‘IT’ Section/Group: 22BET_IOT 703/B
Semester: 5th Date of Performance: 27/08/2024
Subject Name: DAA Subject Code: 22ITH-311
1. Code for enqueue, dequeue, Isfull and Isempty operation in queues using
templates.
import java.util.Scanner;
while (true) {
System.out.println("\nChoose an operation:");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Check if the queue is full");
System.out.println("4. Check if the queue is empty");
System.out.println("5. View the front item");
System.out.println("6. View the rear item");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the item to enqueue: ");
int item = scanner.nextInt();
queue.enqueue(item);
break;
case 2:
queue.dequeue();
break;
case 3:
if (queue.isFull()) {
System.out.println("The queue is full.");
} else {
System.out.println("The queue is not full.");
}
break;
case 4:
if (queue.isEmpty()) {
System.out.println("The queue is empty.");
} else {
System.out.println("The queue is not empty.");
}
break;
case 5:
Integer frontItem = queue.front();
if (frontItem != null) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.println("Front item: " + frontItem);
} else {
System.out.println("The queue is empty.");
}
break;
case 6:
Integer rearItem = queue.rear();
if (rearItem != null) {
System.out.println("Rear item: " + rearItem);
} else {
System.out.println("The queue is empty.");
}
break;
case 7:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Temporary arrays
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
System.out.println("Original array:");
printArray(array);
System.out.println("Sorted array:");
printArray(array);
}
}
Output:
class Node {
int data;
Node left, right;
// Height of the tree is the maximum of the heights of the left and right subtrees
plus 1
return Math.max(leftHeight, rightHeight) + 1;
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Code to find the ignored successor in an Binary Search Trees with complexity
Analysis.
return (node);
}
if (node == null)
return (newNode(data));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
else
{
node temp;
if (data <= node.data)
{
temp = insert(node.left, data);
node.left = temp;
temp.parent = node;
}
else
{
temp = insert(node.right, data);
node.right = temp;
temp.parent = node;
}
return node;
}
}
// Function Call
succ = inOrderSuccessor(root, temp);
if (succ != null)
System.out.printf(
"\n Inorder Successor of %d is %d ",
temp.data, succ.data);
else
System.out.printf("\n Inorder Successor doesn't exit");
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
Output: