Generic_stack
Tue May 28 2024 15:56:56 GMT+0000 (Coordinated Universal Time)
Saved by @adsj
class ArrayStack<T> { private T[] array; private int top; private int capacity; // Constructor @SuppressWarnings("unchecked") public ArrayStack(int capacity) { this.capacity = capacity; this.array = (T[]) new Object[capacity]; // Create a generic array this.top = -1; } // Push method public void push(T value) { if (top == capacity - 1) { throw new StackOverflowError("Stack is full"); } array[++top] = value; } // Pop method public T pop() { if (top == -1) { throw new IllegalStateException("Stack is empty"); } return array[top--]; } // Peek method public T peek() { if (top == -1) { throw new IllegalStateException("Stack is empty"); } return array[top]; } // Check if stack is empty public boolean isEmpty() { return top == -1; } // Check if stack is full public boolean isFull() { return top == capacity - 1; } } public class ArrayStackDemo { public static void main(String[] args) { ArrayStack<Integer> intStack = new ArrayStack<>(5); intStack.push(1); intStack.push(2); intStack.push(3); System.out.println("Top of Integer stack: " + intStack.peek()); System.out.println("Pop from Integer stack: " + intStack.pop()); ArrayStack<Double> doubleStack = new ArrayStack<>(5); doubleStack.push(1.1); doubleStack.push(2.2); doubleStack.push(3.3); System.out.println("Top of Double stack: " + doubleStack.peek()); System.out.println("Pop from Double stack: " + doubleStack.pop()); ArrayStack<String> stringStack = new ArrayStack<>(5); stringStack.push("Hello"); stringStack.push("World"); System.out.println("Top of String stack: " + stringStack.peek()); System.out.println("Pop from String stack: " + stringStack.pop()); } } class LinkedListStack<T> { private static class Node<T> { private T data; private Node<T> next; public Node(T data) { this.data = data; } } private Node<T> top; // Push method public void push(T value) { Node<T> newNode = new Node<>(value); newNode.next = top; top = newNode; } // Pop method public T pop() { if (top == null) { throw new IllegalStateException("Stack is empty"); } T value = top.data; top = top.next; return value; } // Peek method public T peek() { if (top == null) { throw new IllegalStateException("Stack is empty"); } return top.data; } // Check if stack is empty public boolean isEmpty() { return top == null; } } public class LinkedListStackDemo { public static void main(String[] args) { LinkedListStack<Integer> intStack = new LinkedListStack<>(); intStack.push(1); intStack.push(2); intStack.push(3); System.out.println("Top of Integer stack: " + intStack.peek()); System.out.println("Pop from Integer stack: " + intStack.pop()); LinkedListStack<Double> doubleStack = new LinkedListStack<>(); doubleStack.push(1.1); doubleStack.push(2.2); doubleStack.push(3.3); System.out.println("Top of Double stack: " + doubleStack.peek()); System.out.println("Pop from Double stack: " + doubleStack.pop()); LinkedListStack<String> stringStack = new LinkedListStack<>(); stringStack.push("Hello"); stringStack.push("World"); System.out.println("Top of String stack: " + stringStack.peek()); System.out.println("Pop from String stack: " + stringStack.pop()); } }
Comments