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)