Set Frame _ use LinkedList to implement the collection Code of stack structure, the collection of LinkedList simulation stack data structure and test case __java

Source: Internet
Author: User
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);
		// }
	}
}



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.