Package cn.itcast_05;
* * * mystack Test
/public class Mystackdemo {public
static void Main (string[] args) {
//Create collection Object
Mystack ms = new Mystack ();
add element
ms.add ("Hello");
Ms.add ("World");
Ms.add ("Java");
Get
//System.out.println (Ms.get ());
System.out.println (Ms.get ());
System.out.println (Ms.get ());
Nosuchelementexception
//System.out.println (Ms.get ());
while (!ms.isempty ()) {
System.out.println (Ms.get ());
}
}}
Package cn.itcast_05;
Import java.util.LinkedList;
/**
* Custom Stack Collection * *
@author Wind Qing yang
* @version V1.0/public
class Mystack {
public LinkedList link;
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 ();
}
}
Package cn.itcast_05;
Import Java.util.Iterator;
Import java.util.LinkedList;
*
* Please use the LinkedList simulation stack data structure of the collection, and test
* *
title means:
* you have defined a collection class, within this collection class can use LinkedList simulation.
*
/public class Linkedlistdemo {public
static void Main (string[] args) {
//A: LinkedList Unique Add function AddFirst ()
//B: Stack features advanced after/
//Create Set object//
LinkedList LL = new LinkedList ();
////
add element
//Ll.addfirst ("Hello");
Ll.addfirst ("World");
Ll.addfirst ("Java");
////
Traverse collection
//iterator it = Ll.iterator ();
while (It.hasnext ()) {
//String s = (string) it.next ();
System.out.println (s);
// }
}
}