Untitled

Anonymous
plain_text
01/26/2026 12:32 AM
10.3 KB
13
Indexable
import java.util.*;

public class StackQueueProgram {

    // Stack class to handle push/pop with overflow prevention
    static class Stack<T> {
        private List<T> stackList;
        private int maxSize;

        public Stack(int maxSize) {
            this.maxSize = maxSize;
            this.stackList = new ArrayList<>();
        }

        public void push(T value) {
            if (stackList.size() < maxSize) {
                stackList.add(value);
                System.out.println("Pushed: " + value + " (Type: " + value.getClass().getSimpleName() + ")");
            } else {
                System.out.println("Error: Stack overflow! Cannot push more than " + maxSize + " elements.");
            }
        }

        public void pop(int times) {
            if (stackList.isEmpty()) {
                System.out.println("Stack is empty!");
                return;
            }
            for (int i = 0; i < times; i++) {
                if (stackList.isEmpty()) {
                    System.out.println("Stack is empty! No more elements to pop.");
                    return;
                }
                T poppedValue = stackList.remove(stackList.size() - 1);
                System.out.println("Popped: " + poppedValue + " (Type: " + poppedValue.getClass().getSimpleName() + ")");
            }
        }

        public void printStack() {
            System.out.println("Current Stack (size " + stackList.size() + "): " + stackList);
        }
    }

    // Queue class to handle enqueue/dequeue with overflow prevention
    static class Queue<T> {
        private LinkedList<T> queueList;
        private int maxSize;

        public Queue(int maxSize) {
            this.maxSize = maxSize;
            this.queueList = new LinkedList<>();
        }

        public void enqueue(List<T> values) {
            boolean overflowOccurred = false;

            for (T value : values) {
                if (queueList.size() < maxSize) {
                    queueList.add(value);
                    System.out.println("Enqueued: " + value + " (Type: " + value.getClass().getSimpleName() + ")");
                } else {
                    if (!overflowOccurred) {
                        System.out.println("Error: Queue overflow! Cannot enqueue more than " + maxSize + " elements.");
                        overflowOccurred = true;
                    }
                }
            }
        }

        public T dequeue() {
            if (queueList.isEmpty()) {
                System.out.println("Queue is empty!");
                return null;
            }
            T dequeuedValue = queueList.removeFirst();
            System.out.println("Dequeued: " + dequeuedValue + " (Type: " + dequeuedValue.getClass().getSimpleName() + ")");
            return dequeuedValue;
        }

        public void printQueue() {
            System.out.println("Current Queue: " + queueList);
        }
    }

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

        System.out.println("Do you want to work with a Stack or Queue? (Enter 'stack' or 'queue')");
        String choice = scanner.nextLine().toLowerCase();

        System.out.print("Enter the size of the array (n): ");
        int n = Integer.parseInt(scanner.nextLine());

        if (choice.equals("stack")) {
            Stack<Object> stack = new Stack<>(n); // Initialize stack with size limit

            while (true) {
                System.out.println("Choose operation: push, pop or end?");
                String operation = scanner.nextLine().toLowerCase();

                if (operation.equals("push")) {
                    System.out.print("Enter value(s) to push (comma-separated): ");
                    String input = scanner.nextLine();
                    String[] values = input.split(",\\s*");  // Split by comma and optional space
                    for (String value : values) {
                        Object parsedValue = getInputValue(value);
                        stack.push(parsedValue);
                    }
                } else if (operation.equals("pop")) {
                    System.out.print("How many times do you want to pop? ");
                    int times = Integer.parseInt(scanner.nextLine());
                    stack.pop(times);
                } else if (operation.equals("end")) {
                    stack.printStack();
                    break;
                } else {
                    System.out.println("Invalid operation.");
                    continue;
                }

                System.out.println("Continue? (yes or end)");
                String continueChoice = scanner.nextLine().toLowerCase();
                if (continueChoice.equals("end")) {
                    stack.printStack();
                    break;
                }
            }
        } else if (choice.equals("queue")) {
            Queue<Object> queue = new Queue<>(n); // Initialize queue with size limit

            while (true) {
                System.out.println("Choose operation: e (enqueue), d (dequeue) or end?");
                String operation = scanner.nextLine().toLowerCase();

                if (operation.equals("e")) {
                    System.out.print("Enter value(s) to enqueue (comma-separated): ");
                    String input = scanner.nextLine();
                    String[] values = input.split(",\\s*");  // Split by comma and optional space

                    // Convert string values to their appropriate types
                    List<Object> parsedValues = new ArrayList<>();
                    for (String value : values) {
                        parsedValues.add(getInputValue(value));
                    }

                    // Enqueue the parsed values
                    queue.enqueue(parsedValues);
                } else if (operation.equals("d")) {
                    queue.dequeue();
                } else if (operation.equals("end")) {
                    queue.printQueue();
                    break;
                } else {
                    System.out.println("Invalid operation.");
                    continue;
                }

                System.out.println("Continue? (yes or end)");
                String continueChoice = scanner.nextLine().toLowerCase();
                if (continueChoice.equals("end")) {
                    queue.printQueue();
                    break;
                }
            }
        } else {
            System.out.println("Invalid choice. Please enter 'stack' or 'queue'.");
        }

        scanner.close();
    }

    // Method to automatically determine the data type from input
    public static Object getInputValue(String input) {
        input = input.trim();

        if (input.startsWith("\"") && input.endsWith("\"")) {
            // Treat as String if input is surrounded by quotation marks
            return input.substring(1, input.length() - 1);
        }

        // Check for float values
        if (input.endsWith("f") || input.endsWith("F")) {
            try {
                return Float.parseFloat(input.substring(0, input.length() - 1)); // Removing 'f' and parsing as Float
            } catch (NumberFormatException e) {
                return input;  // If parsing fails, treat as a String
            }
        }

        try {
            return Integer.parseInt(input); // Try parsing as an Integer
        } catch (NumberFormatException e) {
            try {
                return Double.parseDouble(input); // Try parsing as a Double
            } catch (NumberFormatException e1) {
                if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
                    return Boolean.parseBoolean(input); // Boolean
                }
                return input; // Default case, treat as String
            }
        }
    }
}
Editor is loading...
Leave a Comment