Group B
11.
1 2 3 4 5 6 |
// Function to perform pop operation on a stack public static int popFromStack(Stack<Integer> stack) { if (stack.isEmpty()) { // Stack is empty, return some default value or throw an exception throw new IllegalStateException("Cannot pop from an empty stack"); } |
12.
1 2 3 4 5 6 7 |
// Function to perform enqueue operation in a circular queue public static void enqueue(CircularQueue queue, int data) { // Check if the queue is full if (queue.isFull()) { System.out.println("Queue is full. Cannot enqueue."); return; } |
an interface representing the linked list ADT:
1 2 3 4 5 6 7 |
public interface LinkedListADT<T> { void insert(int index, T data); void delete(int index); int search(T data); void traverse(); T retrieve(int index); } |
14.
1 2 3 4 5 6 |
// Function to perform selection sort on an array public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; |
15.
1 2 3 4 5 6 7 8 9 10 11 |
// Recursive function to find the greatest common divisor (GCD) public static int findGCD(int a, int b) { // Base case if (b == 0) { return a; } // Recursive case return findGCD(b, a % b); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Function to delete the last node of the doubly linked list void deleteLastNode() { // Check if the list is empty if (head == null) { System.out.println("The list is empty. Cannot delete."); return; } // If there is only one node in the list if (head.next == null) { head = null; return; } |
Group D