Name - Reg No. - Slot - : Aryan Lunia 22BCE8809 L14+L15
Name - Reg No. - Slot - : Aryan Lunia 22BCE8809 L14+L15
// Array Operations
static void arrayOperations() {
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
// Insert elements
System.out.println("Enter the elements:");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
// Display elements
System.out.println("Elements in the array:");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
// Insert elements
System.out.println("Enter the elements to insert into the Linked List:");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
linkedList.add(scanner.nextInt());
}
// Display elements
System.out.println("Elements in the Linked List:");
for (int element : linkedList) {
System.out.print(element + " ");
}
System.out.println();
// Stack Operations
static void stackOperations() {
Stack<Integer> stack = new Stack<>();
// Push elements
System.out.println("Enter the elements to push onto the Stack:");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
stack.push(scanner.nextInt());
}
// Display stack
System.out.println("Elements in the Stack:");
for (int element : stack) {
System.out.print(element + " ");
}
System.out.println();
// Pop operation
if (!stack.isEmpty()) {
System.out.println("Popped element: " + stack.pop());
}
// Queue Operations
static void queueOperations() {
Queue<Integer> queue = new LinkedList<>();
// Enqueue elements
System.out.println("Enter the elements to enqueue into the Queue:");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
queue.add(scanner.nextInt());
}
// Display queue
System.out.println("Elements in the Queue:");
for (int element : queue) {
System.out.print(element + " ");
}
System.out.println();
// Dequeue operation
if (!queue.isEmpty()) {
System.out.println("Dequeued element: " + queue.poll());
}
// Binary Search
static void binarySearch() {
System.out.print("Enter number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
if (arr[mid] == key) {
found = true;
break;
}
if (found) {
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}
switch (choice) {
case 1:
arrayOperations();
break;
case 2:
linkedListOperations();
break;
case 3:
stackOperations();
break;
case 4:
queueOperations();
break;
case 5:
binarySearch();
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice, try again.");
}
} while (choice != 6);
}
}
Output: