package generic type;
Import Java.math.BigDecimal;
Import Java.math.MathContext;
public class Stackdemoapp {public static void main (string[] args) {//Long type stack System.out.println ("Create ' long ' type stack");
Create a stack object that holds a Long data type stack<long> longstack = new stack<long> ();//This stack is the class that you created below that has the Push,pop method
System.out.println ("Press into 5");
Longstack.push (5L);//5l also OK, (long) 5 also line System.out.println ("press into 10");
Longstack.push (10L);
System.out.println ("Empty stack");
System.out.println (Longstack.pop ());
System.out.println (Longstack.pop ());
System.out.println ();
Float type stack System.out.println ("Create ' float ' type stack");
stack<float> floatstack = new stack<float> ();
System.out.println ("Press into 5.0");
Floatstack.push (5.0f);
System.out.println ("Press into 10.0");
Floatstack.push (10.0f);
System.out.println ("Empty stack");
System.out.println (Floatstack.pop ());
System.out.println (Floatstack.pop ());
System.out.println ();
Large integer type stack System.out.println ("Create ' BigDecimal ' stack"); stack<bigdecimal> BiGdecimalstack = new stack<bigdecimal> ();
System.out.println ("Press into Bigdecimal:12.5e+7");
Bigdecimalstack.push (New BigDecimal ("12.5E+7"));
System.out.println ("Press into bigdecimal:125");
Bigdecimalstack.push (New BigDecimal (125,mathcontext.decimal128));
System.out.println ("Empty stack");
System.out.println (Bigdecimalstack.pop ());
System.out.println (Bigdecimalstack.pop ());
System.out.println ();
No generic stack stack oldtypestack = new stack ();
Oldtypestack.push (10);
Oldtypestack.push ("test string");
for (int i=0;i<2;i++) {string str = (string) oldtypestack.pop ();
System.out.println (str);
"}}//Custom stack class Stack<t> {//private t[] stack =new t[5];//wrong wrong .....
Private t[] stack = (t[]) New object[5];//t can be String,integer//For example: string[] stack= (string[)) new object[5];
Float[] stack= (float[]) new object[5];
private int ptr =-1;
void push (T data)//indentation method push {ptr++;
Stack[ptr]=data;
T pop ()//Eject method {return (T) stack[ptr--];
}
}