Java generics implement a stack class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.