Import Java. lang. reflect. array;/*** generic stack ** @ Param <t> */public class Stack <t> {private class <t> type; // Private int size of the class to which the stack element belongs; // stack depth private T [] arr; // use an array to store private int top; // The subscript public stack (class <t> type, int size) of the top element of the stack {This. type = type; this. size = size; arr = createarray (size); Top =-1;}/*** create an array * @ Param size * @ return */@ suppresswarnings ("unchecked ") private T [] createarray (INT size) {r Eturn (T []) array. newinstance (type, size);}/*** pressure stack * @ Param T */Public void push (t) {top ++; arr [Top] = T ;} /*** output stack ** @ return */Public t POP () {T = arr [Top]; top --; return t ;} /*** get the top element of the stack * @ return */Public t PEEK () {return arr [Top];} /*** determine whether the stack is empty * @ return */Public Boolean isempty () {return Top =-1 ;} /*** determine whether the stack is full * @ return */Public Boolean isfull () {return Top = (size-1);} public St Atic void main (string [] ARGs) {stack <string> S = new stack <string> (string. class, 100); S. push ("with perseverance as a good friend"); S. push ("experience-based adviser"); S. push ("be careful as Brother"); S. push ("as expected"); While (! S. isempty () {string STR = S. Pop (); system. Out. println (STR );}}}
Output:
Hope as the Sentinel, be careful as the brother, use experience as the adviser, and take perseverance as a good friend