Collection framework (using LinkedList to simulate the collection of stack data structures and test cases)

Source: Internet
Author: User

Package cn.itcast_05;


Import java.util.LinkedList;


/**

* Custom Stack Collection

*

* @author Wind

* @version V1.0

*/

public class Mystack {

Private LinkedList link;//defining member variables


Public Mystack () {

link = new LinkedList ();

}


Public void Add (Object obj) {

Link.addfirst (obj);

}


Public Object get () {

return Link.getfirst ();

return Link.removefirst ();

}


Public Boolean isEmpty () {

return Link.isempty ();

}

}


Test class

Package cn.itcast_05;


/*

* Test of Mystack

*/

public class Mystackdemo {

public static void Main (string[] args) {

To create a collection object

Mystack ms = new mystack ();


adding elements

Ms.add ("Hello");

Ms.add ("World");

Ms.add ("Java");


System.out.println (Ms.get ());

System.out.println (Ms.get ());

System.out.println (Ms.get ());

Nosuchelementexception

System.out.println (Ms.get ());

The following code is for the above //System.out.println (Ms.get ()), and so on, and so on, in addition to the code to pop up the element, you can also determine whether the stack is empty

while (! ms.isempty ()) {

System.out.println (ms.get ());

}

}

}

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0010.gif "alt=" J_0010.gif "/> each knowledge point in the collection holds firm

This article from "GD" blog, reproduced please contact the author!

Collection framework (using LinkedList to simulate the collection of stack data structures and test cases)

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.