Search code examples
javagenericsstackprimitive

Implementing generics to a primitive stack


I'm clueless as to how to implement generics in java, so I was hoping I could get some help turning my primitive implementation of a stack (below) into a program that uses generics (assuming it's possible to just change a few things, and not how to write a completely different program).

Below is my code:

import java.io.*;
import java.util.*;

public class LinkedListStack {

    public static Stack<Integer> stk = new Stack<Integer>();
    public static int min, push, top;

    public static void main(String[] args) {        

        //initialize random integer generator
        Random rand = new Random();

        System.out.println("Stack empty --> top = null, min = null");
        stackPush(rand.nextInt(50));

        //first value in the stack is the minimum until a smaller integer is pushed
        min = stk.peek();

        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPop();
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();

        if (!stk.isEmpty()) {
            System.out.print("\nFinal stack: ");
            for(int x : stk) {
                System.out.print(x + " ");
            }
        } else {
            System.out.print("\nStack is empty!");
        }

        System.out.println();
    }

    public static int stackPush(int pushInt) {
        try {
            stk.push(pushInt);
            if (stk.peek() < min) {
                min = stk.peek();
            }
            top = stk.peek();
            System.out.println("Push " + pushInt + " --> top = " + top +  ", min = " + min);

        } catch (EmptyStackException e){
            System.out.println("ERROR");
        }

        return pushInt;
    }

    public static void stackPop() {
        try {
            stk.pop();
            if (stk.peek() < min) {
                min = stk.peek();
            }
            top = stk.peek();
            System.out.println("Pop --> top = " + top +  ", min = " + min);

        } catch (EmptyStackException e) {
            System.out.println("Stack already empty!");
        }
    }
}

Solution

  • First of all, your class should not be static to accomplish this. It should also not expose its underlying implementation via public fields or via naming such as LinkListStack

    Instead, you may create a class such as

    class MyStack<E> {    
        private final Stack<E> wrapped = new Stack<E>();
    
        public void push(E element) {
            wrapped.push(e);
        }
    
        public E pop() {
            return wrapped.pop();
        }
    }
    
    
    public class Program {
    
         public static void main(String[] args) {        
    
         System.out.println("Stack empty --> top = null, min = null");
    
         MyStack<String> stack = new MyStack<>();
    
         stack.push("hello");
    
         stack.push("world");
    }
    

    Note: Java has a built-in stack and re-inventing the wheel is bad practice except when done purely for educative/exploratory programming. This built in Stack is actually used to provide the underlying wrapped value which forms the backbone of our implementation.