Think the author is too good to write, have to collect.
The understanding of this example:
Type parameters cannot be of basic type, T and u are actually the same type.
Each time the new data is the new top, the original top down the level, through the pointer to create a link.
The end sentry is both a node that is created by the default constructor that conforms to the true End ().
Copy Code code as follows:
//: Generics/linkedstack.java
//A stack implemented with an internal linked structure.
Package generics; public class Linkedstack<t> {
private static class Node<u> {
U item;
Node<u> Next;
Node () {item = null; next = null;}
Node (U item, node<u> next) {
This.item = Item;
This.next = next;
}
Boolean end () {return item = = NULL && next = null;}
}
Private node<t> top = new node<t> (),//End Sentinel
public void push (T item) {
top = new N Ode<t> (item, top);
}
Public T POPs () {
T result = Top.item
if (!top.end ())
top = Top.next;
return result;
}
public static void Main (string[] args) {
Linkedstack<string> LSS = new linkedstack<string> (),
for (String S: "Phasers on stun!".) Split (""))
Lss.push (s);
String SS;
while ((ss = Lss.pop ())!= null)
System.out.println (ss);
//-----If put integer into the LinkedlisT
linkedstack<integer> lii = new linkedstack<integer> ();
for (Integer i = 0; i < i++) {
LII.P Ush (i);
}
Integer end,
while ((end = Lii.pop ())!= null)
System.out.println (end);
//-----integer Test END!
}
/* Output:
stun!
on
phasers
*/