1 Packagecom.xt.test;2 3 /**4 * Generic implementation stack, thinking in Java example5 * 6 * @authorAdministrator7 *8 * @param<T>9 */Ten Public classLinkedtrack<t> { One Private Static classNode<u> { A U Item; -Node<u>Next; - the Node () { -item =NULL; -Next =NULL; - } + -Node (U item, node<u>next) { + This. Item =item; A This. Next =Next; at } - - BooleanEnd () { - returnitem = =NULL&& Next = =NULL; - } - } in - /** to * End Sentinel to indicate if it's at the end + */ - Privatenode<t> top =NewNode<t>(); the * /** $ * Add nodes to the node (include in)Panax Notoginseng * - * @paramItem the */ + Public voidpush (T item) { Atop =NewNode<t>(item, top); the } + - /** $ * Node Fetch node (out of) $ * - * @return - */ the PublicT Pop () { -T result =Top.item;Wuyi if(!top.end ()) thetop =Top.next; - returnresult; Wu } - About Public Static voidMain (string[] args) { $Linkedtrack<string> LT =NewLinkedtrack<string>(); - for(String s: "This is a test!". Split ("")) - Lt.push (s); - String S; A while((s = Lt.pop ())! =NULL) + System.out.println (s); the } - $}
Read the code read for a long time do not understand how to achieve, eventually in the Eclipse code to write one side, people stupid, debugging run only to realize that the original < multi-layered nested principle > (own blind to the name), the specific look as follows:
Java generics implement a stack class