0% found this document useful (0 votes)
11 views18 pages

Name - Reg No. - Slot - : Aryan Lunia 22BCE8809 L14+L15

The document outlines a Java program that performs various data structure operations including array, linked list, stack, queue, and binary search using a switch case menu. It provides detailed methods for creating, inserting, deleting, and displaying elements in each data structure. The program continues to prompt the user for operations until they choose to exit.

Uploaded by

aryanrocks00777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

Name - Reg No. - Slot - : Aryan Lunia 22BCE8809 L14+L15

The document outlines a Java program that performs various data structure operations including array, linked list, stack, queue, and binary search using a switch case menu. It provides detailed methods for creating, inserting, deleting, and displaying elements in each data structure. The program continues to prompt the user for operations until they choose to exit.

Uploaded by

aryanrocks00777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Name – Aryan Lunia

Reg No. – 22BCE8809


Slot – L14+L15
Exp-1
Design and develop a Java program that performs the following
operations using Switch Case
1. Array Operations (Create, Insert, Delete, Display)
2. Linked list operations (Create, Insert, Delete, Display)
3. Stack (Push, Pop, Display)
4. Queue (Enque, Deque, Display)
5. Binary Search
6. Exit
7. Repeat the menu
Ex Input: No. of elements: 10 Press :
1. Array operations
2. Linked List Operation
3.Stack Operation
4.Queue
5. Binary Search.
Answer:
Code:
import java.util.*;
public class Main {

static Scanner scanner = new Scanner(System.in);

// 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();

// Delete operation (remove last element)


if (size > 0) {
System.out.println("Deleting last element: " + arr[size - 1]);
size--; // Decrease size
}

// Display updated array


System.out.println("Updated elements in the array:");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

// Linked List Operations


static void linkedListOperations() {
LinkedList<Integer> linkedList = new LinkedList<>();

// 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();

// Delete operation (remove first element)


if (!linkedList.isEmpty()) {
System.out.println("Deleting first element: " +
linkedList.removeFirst());
}

// Display updated list


System.out.println("Updated 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());
}

// Display updated stack


System.out.println("Updated Stack:");
for (int element : stack) {
System.out.print(element + " ");
}
System.out.println();
}

// 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());
}

// Display updated queue


System.out.println("Updated Queue:");
for (int element : queue) {
System.out.print(element + " ");
}
System.out.println();
}

// Binary Search
static void binarySearch() {
System.out.print("Enter number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];

// Input elements in sorted order


System.out.println("Enter elements in sorted order:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

System.out.print("Enter element to search: ");


int key = scanner.nextInt();

int left = 0, right = n - 1;


boolean found = false;

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == key) {
found = true;
break;
}

if (arr[mid] < key)


left = mid + 1;
else
right = mid - 1;
}

if (found) {
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}

public static void main(String[] args) {


int choice;
do {
System.out.println("\nPress:");
System.out.println("1. Array Operations");
System.out.println("2. Linked List Operations");
System.out.println("3. Stack Operations");
System.out.println("4. Queue Operations");
System.out.println("5. Binary Search");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

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:

You might also like