A collection of Java 16-5 linkedlist simulation stack data structures

Source: Internet
Author: User

Please use LinkedList to simulate the collection of stack data structures and test
The meaning of the title is:
Your own definition of a collection class, within which the LinkedList simulation can be used.

1  Packagecn_linkedlist;2 3 Importjava.util.LinkedList;4 5  6 7 8  Public classMystack {9 Ten //define a member variable for a LinkedList class One PrivateLinkedList list =NULL; A  - /** - * Construction Method the * @list method of calling the LinkedList class - */ -  PublicMystack () { -List =NewLinkedList (); + } - /** + * Since it's a collection, there's a way to add elements A * @paramobj can add elements of any type at */ -  Public voidAdd (Object obj) { - List.addfirst (obj); - } -  - /** in * The collection also has a method of extracting elements, but here it is required to simulate the way of the stack, - * and the stack is first entered after coming out, so, to use the Removefirst () method to * Deposit a/b/c, extract c/b/a, extract it and lose it, then the next one becomes the first, and so on + */ -  Public voidget (Object obj) { the List.removefirst (); * } $ /**Panax Notoginseng * When extracting, to prevent an error in exceeding the capacity of the collection, add a method to query whether the next element exists - * @isEmpty is a Boolean type the */ +  Public BooleanIsEmpty () { A returnList.isEmpty (); the } + } -  $  $  Packagecn_linkedlist; -  - ImportJava.util.Iterator; the Importjava.util.LinkedList; - Wuyi /* the please use LinkedList to simulate the collection of stack data structures and test - */ Wu  Public classMystackdemo { -  About  Public Static voidMain (string[] args) { $  - //Create a collection -LinkedList list =NewLinkedList (); -  A //adding elements to a collection +List.addfirst ("Ah san"); theList.addfirst ("Ah si"); -List.addfirst ("Ah Wu"); $List.addfirst ("Ah VI"); theList.addfirst ("Ah Seven"); theList.addfirst ("Ah Eight"); the  the //iterate through the collection and output -Iterator it =list.iterator (); in  while(It.hasnext ()) { theString s =(String) It.next (); the System.out.println (s); About } the } the}

A collection of Java 16-5 linkedlist simulation stack data structures

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.