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 A 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 Node<t> (item, top);
}
Public T pop () {
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.push (i);
}
Integer end;
while ([end = Lii.pop ())!= null)
SYSTEM.OUT.PRINTLN (end);
-----Integer Test end!
}
}
/* Output:
stun!
On
Phasers
*/