Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 47 additions & 29 deletions Data-Structures/HashTable.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import java.util.Scanner;

public class HashTable {
static int size;
static Dataitem[] hasharray;
static final Dataitem DELETED = new Dataitem(-1, -1);
public class HashTable {
static int size;
static Dataitem[] hasharray;
static final Dataitem DELETED = new Dataitem(-1, -1);
private static final String INVALID_INT_MESSAGE = "Invalid input. Please enter an integer.";

static class Dataitem {
int data;
Expand All @@ -15,12 +16,31 @@ static class Dataitem {
}
}

static void initialise() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the Hash Table: ");
size = sc.nextInt();
hasharray = new Dataitem[size];
}
static void initialise(Scanner sc) {
size = readPositiveInt(sc, "Enter the size of the Hash Table: ");
hasharray = new Dataitem[size];
}

static int readInt(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
if (sc.hasNextInt()) {
return sc.nextInt();
}
System.out.println(INVALID_INT_MESSAGE);
sc.next();
}
}

static int readPositiveInt(Scanner sc, String prompt) {
while (true) {
int value = readInt(sc, prompt);
if (value > 0) {
return value;
}
System.out.println("Invalid input. Please enter an integer greater than 0.");
}
}

static int hashcode(int key) {
return Math.floorMod(key, size);
Expand Down Expand Up @@ -69,22 +89,20 @@ static void display() {
}
}

public static void main(String[] args) {
initialise();
Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of Insertions: ");
int n = sc.nextInt();

for (int i = 0; i < n; i++) {
System.out.print("Enter the key: ");
int key = sc.nextInt();
System.out.print("Enter the element: ");
int element = sc.nextInt();
insert(key, element);
}

System.out.println("Insertion done!");
display();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
initialise(sc);

int n = readPositiveInt(sc, "Enter the number of Insertions: ");

for (int i = 0; i < n; i++) {
int key = readInt(sc, "Enter the key: ");
int element = readInt(sc, "Enter the element: ");
insert(key, element);
}

System.out.println("Insertion done!");
display();
sc.close();
}
}
74 changes: 45 additions & 29 deletions Data-Structures/Queue/CircularQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,34 @@ public boolean isFull() {
}
}

public class CircularQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter Queue size: ");
int capacity = sc.nextInt();

Queue queue = new Queue(capacity);
int choice;
public class CircularQueue {
private static int readInt(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
if (sc.hasNextInt()) {
return sc.nextInt();
}
System.out.println("Invalid input. Please enter an integer.");
sc.next();
}
}

private static int readPositiveInt(Scanner sc, String prompt) {
while (true) {
int value = readInt(sc, prompt);
if (value > 0) {
return value;
}
System.out.println("Invalid input. Please enter an integer greater than 0.");
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int capacity = readPositiveInt(sc, "Enter Queue size: ");

Queue queue = new Queue(capacity);
int choice;

do {
System.out.println("\n--- Circular Queue Operations ---");
Expand All @@ -94,18 +113,15 @@ public static void main(String[] args) {
System.out.println("3. Peek");
System.out.println("4. Display");
System.out.println("5. Is Full?");
System.out.println("6. Is Empty?");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");

choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter element to Enqueue: ");
int element = sc.nextInt();
queue.enqueue(element);
break;
System.out.println("6. Is Empty?");
System.out.println("7. Exit");
choice = readInt(sc, "Enter your choice: ");

switch (choice) {
case 1:
int element = readInt(sc, "Enter element to Enqueue: ");
queue.enqueue(element);
break;

case 2:
queue.dequeue();
Expand All @@ -127,14 +143,14 @@ public static void main(String[] args) {
System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty.");
break;

case 7:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 7);
case 7:
System.out.println("Exiting...");
break;

default:
System.out.println("Invalid choice. Please enter a valid menu option.");
}
} while (choice != 7);

sc.close();
}
Expand Down
86 changes: 51 additions & 35 deletions Data-Structures/Queue/Queue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.util.Scanner;

class Queue {
class Queue {
private int[] arr;
private int front;
private int rear;
Expand Down Expand Up @@ -74,17 +74,36 @@ public boolean isEmpty() {
return rear == -1;
}

public boolean isFull() {
return rear == capacity - 1;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter Queue size: ");
int capacity = sc.nextInt();

Queue queue = new Queue(capacity);
public boolean isFull() {
return rear == capacity - 1;
}

private static int readInt(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
if (sc.hasNextInt()) {
return sc.nextInt();
}
System.out.println("Invalid input. Please enter an integer.");
sc.next();
}
}

private static int readPositiveInt(Scanner sc, String prompt) {
while (true) {
int value = readInt(sc, prompt);
if (value > 0) {
return value;
}
System.out.println("Invalid input. Please enter an integer greater than 0.");
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int capacity = readPositiveInt(sc, "Enter Queue size: ");

Queue queue = new Queue(capacity);

while (true) {
System.out.println("\n--- Queue Operations ---");
Expand All @@ -93,18 +112,15 @@ public static void main(String[] args) {
System.out.println("3. Peek");
System.out.println("4. Display");
System.out.println("5. Is Full?");
System.out.println("6. Is Empty?");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");

int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter element to Enqueue: ");
int element = sc.nextInt();
queue.enqueue(element);
break;
System.out.println("6. Is Empty?");
System.out.println("7. Exit");
int choice = readInt(sc, "Enter your choice: ");

switch (choice) {
case 1:
int element = readInt(sc, "Enter element to Enqueue: ");
queue.enqueue(element);
break;

case 2:
queue.dequeue();
Expand All @@ -126,14 +142,14 @@ public static void main(String[] args) {
System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty.");
break;

case 7:
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice!");
}
}
}
}
case 7:
System.out.println("Exiting...");
sc.close();
return;

default:
System.out.println("Invalid choice. Please enter a valid menu option.");
}
}
}
}
Loading
Loading