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