Thinking Analysis: Since it is a generic implementation of the stack structure, then you can not use the JDK with the stack package, you need to define a stack structure, such as LinkedList.
The code is as follows:
Stack.java:
Copy Code code as follows:
Package cn.edu.xidian.crytoll;
Import java.util.LinkedList;
public class Stack<t> {
Private linkedlist<t> container = new linkedlist<t> ();
public void push (T-t) {
Container.addfirst (t);
}
Public T pop () {
return Container.removefirst ();
}
public Boolean empty () {
return Container.isempty ();
}
}
Stacktest.java:
Copy Code code as follows:
Package cn.edu.xidian.crytoll;
public class Stacktest {
public static void Main (string[] args) {
stack<string> stack = new stack<string> ();
System.out.println ("Add string to Stack:");
System.out.println ("Video learning Java");
System.out.println ("Java");
System.out.println ("Java from Getting Started to mastering (2nd edition)");
Stack.push ("Video learning Java"); Add a string to the stack
Stack.push ("Java"); Add a string to the stack
Stack.push ("Java from Getting Started to mastering (2nd edition)"); Add a string to the stack
SYSTEM.OUT.PRINTLN ("Fetch the string from the stack:");
while (!stack.empty ()) {
System.out.println ((String) Stack.pop ());//Delete all elements of the stack and output
}
}
}