Define a generic Stack<> type with a method push() that adds the object that is passed as an argument to the top of the stack, and with a method pop() that removes and returns the object that is currently at the top of the stack. The pop() method should return null when the stack is empty. Demonstrate the operation of your Stack<> implementation by storing and retrieving 10 strings and 10 Double objects in stacks of a suitable type.
This is main Class to Test Above Java Class "Stack".
Output of Above Java Program
Data pushed onto the stack in sequence is:
1.0
1.4142
1.732
2.0
2.236
2.449
2.646
2.828
3.0
3.162
Data retrieved from the stack in sequence is:
3.162
3.0
2.828
2.646
2.449
2.236
2.0
1.732
1.4142
1.0
The stack should now be empty. Popping an item produces: null
A stack is a container that stores objects in a manner indicated by its name in a vertical stack where only the object at the top of the stack is accessible. It works rather like a sprung stack of plates in a cafeteria. Only the top plate is at counter level and, therefore, is the only one you can access. When you add a plate to the stack, the existing plates are pushed down so the new plate is now the one that you can access.
public class Stack<T> { public Stack() { items = new Object[initialCapacity]; } // Push an item onto the stack public void push(T item) { if(itemCount==items.length) { Object[] newItems = new Object[items.length + increment]; for(int i = 0 ; i < items.length ; ++i) { newItems[i] = items[i]; } items = newItems; } items[itemCount++] = item; } // Pop an item off the top of the stack @SuppressWarnings("unchecked") public T pop() { if(itemCount == 0) { return null; } // Note: the annotation ensures this cast to type T will not result // in a warning from the compiler. T item = (T)items[--itemCount]; items[itemCount] = null; // Erase the object from the stack return item; } // Test for empty stack public boolean isEmpty() { return itemCount == 0; } // You cannot define an array of a parametric type T. // To store items in the stack of an arbitrary you can use type Object[] private Object[] items; // Array to store items in the stack private int itemCount; // Number of items in the stack private final static int initialCapacity = 10; // Initial stack capacity private final static int increment = 5; // Capacity increment when full }
This is main Class to Test Above Java Class "Stack".
public class TryStack { public static void main(String[] args) { // Values to be stored in stack and then retrieved double[] values = { 1.0, 1.4142 , 1.732 , 2.0, 2.236, 2.449, 2.646, 2.828, 3.0, 3.162 }; Stack<Double> data = new Stack<Double>(); // Store the values on the stack System.out.println("Data pushed onto the stack in sequence is:"); for(double v : values) { System.out.println(v); data.push(v); } double back = 0; System.out.println("\nData retrieved from the stack in sequence is:"); while(!data.isEmpty()) { System.out.println(data.pop()); } System.out.println("The stack should now be empty. Popping an item produces: " + data.pop()); } }
Output of Above Java Program
Data pushed onto the stack in sequence is:
1.0
1.4142
1.732
2.0
2.236
2.449
2.646
2.828
3.0
3.162
Data retrieved from the stack in sequence is:
3.162
3.0
2.828
2.646
2.449
2.236
2.0
1.732
1.4142
1.0
The stack should now be empty. Popping an item produces: null